简体   繁体   中英

Starting an executable Task vs Process in C#

I am building a client-side utility program(Application Upgrader) in C# which periodically checks with the server if the upgrade is available and downloads it if it is so else it does nothing. Currently, I have two approaches in mind - Process-based and Task-based. In both approaches, I am starting the Upgrader from the Main method (of console application). Which of these two approaches should be used? provided the requirements -

  • the Upgrader utility should run asynchronously (without blocking the Main)
  • and once the upgrade is downloaded, it should prompt the user to install it right away or later

Secondly, how to schedule the periodic execution of "upgrader.exe" from the Main() in the code below? Also, is there a better way to achieve this functionality of asynchronous download? I envision a sort of upgrades that we see happening on mobile apps in the background and then a prompt is there asking for installation.

class Program
{
    static async Task Main(string[] args)
    {
        string filename = @"C:\Users\abc\upgrader.exe";
        Console.WriteLine(FiggleFonts.Standard.Render("Hello World"));
        Console.WriteLine("About to start the upgrader process now...");

        // Approch 1: DirectlyProcess based
        //using(Process myprocess = new Process())
        //{
        //    myprocess.StartInfo.FileName = filename;
        //    myprocess.StartInfo.CreateNoWindow = true;
        //    myprocess.Start();
        //}

        // Approach 2: Task based
        await RunProcessAsync(filename).ContinueWith(taskres => Console.WriteLine($"taskresult is: {taskres}"));
        Console.WriteLine("This code runs after RunProcessAsyncFinishes. Pop up a windows to ask install now or later");
    }

    static Task<int> RunProcessAsync(string filename)
    {
        var tcs = new TaskCompletionSource<int>();
        var upgraderprocess = new Process
        {
            StartInfo =
            {
                FileName = filename
            },
            EnableRaisingEvents = true
        };

        upgraderprocess.Exited += (sender, args) =>
        {
            tcs.SetResult(upgraderprocess.ExitCode);
            upgraderprocess.Dispose();
        };

        upgraderprocess.Start();
        return tcs.Task;
    }
}

For your first question, while tasks share the same memory space and should run under another process, processes have their own memory space and they can run independently from any other process. This feature has ups and downs. The good thing is you can run a process and kill the main process which runs it at the first place, the second process will still be alive. But the bad thing is if you need to share some properties or memory space with another process, it may require some workarounds.

For your second question, you may think to use Scheduled Tasks for Windows Server. Or you can register this functionality as a Service (easiest way to do it is using TopShelf, https://github.com/Topshelf/Topshelf ) and set a timer in it to create some interval for execution.

Just look these 2 options and select best one which suits with your requirements. For any other questions, drop me a line in comment.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM