Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use asynchronous programming model which allowing some portion of code to be executed on a separate threads.</p> <p>There are three style of Programming with APM</p> <ol> <li><p>Wait Until Done Model</p></li> <li><p>Polling Model</p></li> <li><p>Callback Model</p></li> </ol> <p>Based on your requirement and result you can choose the model which is more appropriate.</p> <p>For instance, let us say you can to read the file and wait until done and sample code is</p> <pre><code> byte[] buffer = new byte[100]; string filename = string.Concat(Environment.SystemDirectory, "\\mfc71.pdb"); FileStream strm = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.Asynchronous); // Make the asynchronous call strm.Read(buffer, 0, buffer.Length); IAsyncResult result = strm.BeginRead(buffer, 0, buffer.Length, null, null); // Do some work here while you wait // Calling EndRead will block until the Async work is complete int numBytes = strm.EndRead(result); // Don't forget to close the stream strm.Close(); Console.WriteLine("Read {0} Bytes", numBytes); Console.WriteLine(BitConverter.ToString(buffer)); </code></pre> <p>But creating your threads are not necessary or suggesting, .NET supports a built in thread pool that can be used in many situation where you thinking to create your own threads. Sample code</p> <pre><code>static void WorkWithParameter(object o) { string info = (string) o; for (int x = 0; x &lt; 10; ++x) { Console.WriteLine("{0}: {1}", info, Thread.CurrentThread.ManagedThreadId); // Slow down thread and let other threads work Thread.Sleep(10); } } </code></pre> <p>Instead of creating a new thread and controlling it, we use the ThreadPool to this work by using its QueueWorkItem method</p> <pre><code>WaitCallback workItem = new WaitCallback(WorkWithParameter)); if (!ThreadPool.QueueUserWorkItem(workItem, "ThreadPooled")) { Console.WriteLine("Could not queue item"); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload