0

I created a simple app with a button in the navbar that calls a function in the HomeController.

The function executes a exe process in the server.

I would like to show something in the navbar that is showing if the function or process is still running or not.

The button calling the function is in the _Layout.cshtml:

<span class="navbar-text">
    <a class="btn btn-outline-success" type="button" 
       asp-controller="Home" asp-action="StartParser">Update Info</a>
</span>

In the HomeController I created the function:

public IActionResult StartParser()
{
    Process p = new Process();
    p.StartInfo.FileName = @"C:\Parser\bin\Debug\Parser.exe";
    p.StartInfo.Arguments = "Web";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;

    p.OutputDataReceived += (sender, data) => Console.WriteLine(data.Data);
    p.ErrorDataReceived += (sender, data) => Console.WriteLine(data.Data);

    p.Start();

    // p.WaitForExit();
    return RedirectToAction("Index");
}

I thought about letting the process update a value in the db like a "isRunning" and then read it in the app but I don't like this solution because if the process gets terminated for some reason then the value will not be updated correctly.

Should I modify the function to be async and uncomment the p.WaitForExit();?

In this scenario how can I "refresh" the Navbar (For example adding a text: "Process Running") when the function starts and refresh it again at the end?

Any other ideas are welcome!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael R.
  • 55
  • 5
  • If you're doing a full redirect here, this could be as simple as using "onclick" event handler to run JS to update the button to "Running...." (Or add a DIV that covers the whole page and displays a spinner) When the redirect happens, it'll load a new page. https://stackoverflow.com/questions/17064304/onclick-text-change – pcalkins Aug 25 '22 at 22:49
  • For a true display of progress, you need something a little more complicated. – pcalkins Aug 25 '22 at 22:52
  • Thanks. I guess that can work when I click on the button using the script in the same page of the navbar... but then when the function ends how can I tell him to change the button back to the original text from the controller code? – Michael R. Aug 25 '22 at 22:57
  • The redirect will load a fresh page so the button (if it exists on the redirect page) will update to default. You'll probably also want to disable the button when you update the text to prevent double-submits... You might also consider switching this to a POST instead of a GET. Seems like this is state changing. – pcalkins Aug 25 '22 at 23:11
  • Did you had a chance to check `why process gets terminated `? Is it because some other process started then current one stopped or why? – Md Farid Uddin Kiron Aug 29 '22 at 08:04

0 Answers0