c# - What is AsyncCallback? -


What is the use of async callback and why should we use it?

When async method processing ends, asyncCallback method is automatically called, where post processing statement can be executed. There is no need to wait for election or waiting to complete the async thread with this technique.

Callback model: Callback model requires that we call the callback method And specify any such condition in the callback method to complete the call. The callback model can be seen in the following example:

  static byte [] buffer = new byte [100]; Static zero testCallbackAPM () {string filename = System.IO.Path.Combine (System.Environment.CurrentDirectory, "mfc71.pdb"); FileStream strm = new filestream (File name, FileMode.Open, FileAccess.Read, FileShare Reid, 1024, FileOptions.Asynchronous); // asynchronous call iAsyncResult result = strm.BeginRead (create buffer, 0, buffer length, new AsyncCallback (FullRead), strm); }  

In this model, we are creating a new AsyncCallback reporter, which specifies a method to call upon operation (on another thread) to complete the operation Are there. In addition, we are specifying some objects, which we may need to call the call. For this example, we are sending stream objects because we will need to call EndRead and close the stream.

The method that we are asked to make will call something like this:

  Fixed zero full-round (IASINCR result result) {console. Published ("Read full "); FileStream strm = (FileStream) Results AsyncState; // is finished, so we can call Android and it will return without blocking int namb bytes = stem endREAD (result); Do not forget to turn off stream strm //. Close (); Console.light line ("read {0} bytes", nanbaytes); Console.WriteLine (BitConverter.ToString (buffer)); }  

Other techniques are working-up and voting .

Wait-to-Doon Model Waiting-paused model allows you to start asynchronous calls and perform other tasks. Once the other work is done, you can try ending the call and it will be blocked until the asynchronous call is completed.

  // create asynchronous call strm. Reed (buffer, 0, buffer lang); IAsyncResult result = strm.BeginRead (buffer, 0, buffer length, null, zero); // When you wait, do some work // Calling endread will be blocked until the async work is completed numbytes = strm.EndRead (results);  

Or you can use the wait handle.

  Results AsyncWaitHandle.WaitOne ();  

Voting model is the same as the voting method, with the exception that the code selects IAsyncResult to see if It is complete.

Make the asynchronous call IAsyncResult result = strm.BeginRead (buffer, 0, buffer, length, blank, blank); // To complete the pole testing, to see that (complete! The result is the ISI suffixes). // Make more calls, if the call is not complete, the thread is complete. Sleep (100); }

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -