C # Basic Learning--Asynchronous Programming article (II)

Source: Internet
Author: User

The event-based asynchronous Pattern is a more advanced asynchronous programming model than the IAsyncResult pattern, and is also used in more contexts. For relatively simple applications that can be conveniently implemented directly with the new BackgroundWorker component of. Net 2.0, For more complex asynchronous applications, you need to implement a class that conforms to the event-based asynchronous Pattern. Both of these are new things to me, first from the simple start, the next chapter I will try the implementation of complex class model

Schema overview

Classes that support event-based asynchronous schemas have several Methodnameasync methods that represent the beginning of an asynchronous operation and have corresponding methodnamecompleted events. Classes may also have CancelAsync or Methodnameasynccancel methods for canceling asynchronous operations and can have progresschanged or methodnameprogresschanged events to track execution progress. Explain the following separately

The Methodnameasync method can have two overloads: Monotonic and multiple invocations, and multiple invocations have an additional state object parameter userState. The UserState parameter is used to differentiate between asynchronous operations so that we can invoke multiple invocation forms multiple times without waiting for the completion of any asynchronous operation (I use the state object as a condition for passing the callback method when learning the IAsyncResult mode). This may not be the case when using patterns, but it is questionable to use the state object as the unique identifier for an exception invocation when implementing the schema. The monotone method of using the form will throw a InvalidOperationException exception if the previous call has not completed

If you have more than one asynchronous method, you should use the CancelAsync method to cancel the pending operation, and you can use UserState to cancel the specified pending task. If you have only one asynchronous method, you can use the Methodnameasynccancel method

In addition, MSDN says that a method that supports only one pending operation at a time, such as Method1Async (string param), is not canceled. I have not understood this sentence, it is not possible to say that the monotonous asynchronous method can not be canceled, it is backgroundworker to do so

Let's go ahead and look at the ProgressChanged event. It has a ProgressChangedEventArgs parameter that the event handler obtains as a percentage of the task completion by checking the Progresspercentage property of the parameter. If more than one asynchronous operation is suspended, the operation can also be distinguished by examining the userstate property of the parameter. If you need to report incremental results with the ProgressChanged event, you can save the results in a class that is derived from ProgressChangedEventArgs and use in an event handler

BackgroundWorker

BackgroundWorker is well matched with the event asynchronous operation mode. It has two overloaded versions of the RunWorkerAsync method (all in monotonic form) and runworkercompleted events, with CancelAsync methods and processchanged events. The difference is that BackgroundWorker increases the DoWork event, which occurs when the RunWorkerAsync method is called, to achieve the purpose of separating the starting method from the actual execution with the BackgroundWorker. You also need to mention the Workerreportsprocess property and the Reportprocess method, which indicates whether you can report progress updates, which raises the Processchanged event, which will be used in the next Demo

Try

Because usually to deal with dozens of trillion of text files, this Demo is to do a read file and show the progress of the console program. First look at the Class name and field

class BackgroundWorkerDemo
   {
     private BackgroundWorker m_bw;
     string m_FilePath;
   }

The constructor receives the file path as a parameter, sets the file path and initializes the BackgroundWorker

public BackgroundWorkerDemo(string filePath)
     {
       m_FilePath = filePath;
  m_bw = new BackgroundWorker();
       m_bw.WorkerReportsProgress = true;
       m_bw.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
       m_bw.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
       m_bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);
     }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.