C# -Four Patterns in Asynchronous execution

前端 未结 3 2011
终归单人心
终归单人心 2020-12-04 06:08

I heard that there are four patterns in asynchronous execution.

There are four patterns in async delegate execution: Polling, Waiting for Completion,

3条回答
  •  渐次进展
    2020-12-04 06:53

    This code is Polling:

    while (!ar.IsCompleted)
    

    That's the key, you keep checking whether or not it's completed.

    THis code doesn't really support all four, but some code does.

    Process fileProcess = new Process();
    // Fill the start info
    bool started = fileProcess.Start();
    

    The "Start" method is Asynchronous. It spawns a new process.

    We could do each of the ways you request with this code:

    // Fire and forget
    // We don't do anything, because we've started the process, and we don't care about it
    
    // Completion Notification
    fileProcess.Exited += new EventHandler(fileProcess_Exited);
    
    // Polling
    while (fileProcess.HasExited)
    {
    
    }
    
    // Wait for completion
    fileProcess.WaitForExit();
    

提交回复
热议问题