public static void Main() { Console.WriteLine("Main begins on thread {0}.", Thread.CurrentThread.ManagedThreadId); int threadId; AsyncMethodDelegate caller = Program.TestMethod; //1: Initiate the asychronous call of TestMethod (on a thread from the thread pool). AsyncResult asyncResult = (AsyncResult)caller.BeginInvoke(2000, out threadId, null, null); int i = 1; while (!asyncResult.IsCompleted) // loop as long as thread with Testmethod() is running { if (i % 30000000 == 0) { Console.WriteLine("Main counting. Reached " + i); } i++; } Console.WriteLine("Main stopped counting."); Console.WriteLine(); //2: Call EndInvoke to wait for the asynchronous call to complete. string returnValue = caller.EndInvoke(out threadId, asyncResult); Console.WriteLine("TestMethod was executed on thread {0}. ", threadId); Console.WriteLine("Value returned from TestMethod: {0}. ", returnValue); Console.WriteLine("Main ends."); Console.ReadLine(); }
public static void Main() { Console.WriteLine("Main begins on thread {0}.", Thread.CurrentThread.ManagedThreadId); int threadId; AsyncMethodDelegate caller = Program.TestMethod; //1: Initiate the asychronous call of TestMethod (on a thread from the thread pool). AsyncResult asyncResult = (AsyncResult)caller.BeginInvoke(6000, out threadId, null, null); // A sychronous call would be: caller.Invoke(3000, out threadId); // or simply: caller(3000, out threadId); // but that would not give any concurrent execution Console.WriteLine("Main does some work."); Thread.Sleep(1800); Console.WriteLine("Main does some more work."); Thread.Sleep(900); Console.WriteLine("Main waits for TestMethod to finish."); Console.WriteLine(); //2: Call EndInvoke to wait for the asynchronous call to complete. string returnValue = caller.EndInvoke(out threadId, asyncResult); Console.WriteLine("TestMethod was executed on thread {0}.", threadId); Console.WriteLine("Value returned from TestMethod: {0}.", returnValue); Console.WriteLine("Main ends."); Console.ReadLine(); }
static void Main(string[] args) { AsyncMethodDelegate am = Metodus; IAsyncResult ia = am.BeginInvoke(2000, "metodus", null, null); Console.WriteLine("mar elinditottam a metodust"); System.Console.WriteLine("IsCompleted: " + ia.IsCompleted); // blokkolas /*DateTime vege = am.EndInvoke(ia); * Console.WriteLine(vege.ToString());*/ // polling /*while (!ia.IsCompleted) * { * Console.WriteLine("varakozok..."); * // itt lehet valamit csinalni * Thread.Sleep(200); * } * DateTime vege = am.EndInvoke(ia); * Console.WriteLine(vege.ToString());*/ // wait megoldas while (!ia.AsyncWaitHandle.WaitOne(300)) { Console.WriteLine("most is varakozok..."); } DateTime vege = am.EndInvoke(ia); Console.WriteLine(vege.ToString()); Console.WriteLine("tobbszoros pelda"); IAsyncResult ia1 = am.BeginInvoke(3000, "metodus-1", null, null); IAsyncResult ia2 = am.BeginInvoke(2500, "metodus-2", null, null); IAsyncResult ia3 = am.BeginInvoke(1500, "metodus-3", null, null); WaitHandle[] handles = new WaitHandle[] { ia1.AsyncWaitHandle, ia2.AsyncWaitHandle, ia3.AsyncWaitHandle }; WaitHandle.WaitAll(handles); Console.WriteLine("befejezodtek"); Console.WriteLine(am.EndInvoke(ia1)); Console.WriteLine(am.EndInvoke(ia2)); Console.WriteLine(am.EndInvoke(ia3)); // callback am.BeginInvoke(3000, "callback pelda", FeldolgozoCallback, am); Console.ReadLine(); }
static void Main(string[] args) { AsyncMethodDelegate am = Metodus; IAsyncResult ia = am.BeginInvoke(2000, "methodus", null, null); Console.WriteLine("Elindult a metódus"); Console.WriteLine("IsCompleted: {0}", ia.IsCompleted); ////polling //while (!ia.IsCompleted) //{ // Console.WriteLine("Varakozok..."); // Thread.Sleep(200); //} ////blokkolas //DateTime vege = am.EndInvoke(ia); //Console.WriteLine("Invoke befejeződött: {0}",vege.ToLongTimeString()); //wait //while (!ia.AsyncWaitHandle.WaitOne(300)) //{ // Console.WriteLine("Varakozok..."); //} //// Többszörös példa //IAsyncResult ia1 = am.BeginInvoke(2000, "methodus-1", null, null); //IAsyncResult ia2 = am.BeginInvoke(2500, "methodus-2", null, null); //IAsyncResult ia3 = am.BeginInvoke(1500, "methodus-3", null, null); //WaitHandle[] handles = new WaitHandle[]{ // ia1.AsyncWaitHandle,ia2.AsyncWaitHandle,ia3.AsyncWaitHandle //}; //WaitHandle.WaitAll(handles); //Console.WriteLine("Befejeződtek"); //Console.WriteLine(am.EndInvoke(ia1)); //Console.WriteLine(am.EndInvoke(ia2)); //Console.WriteLine(am.EndInvoke(ia3)); //callback am.BeginInvoke(3000, "callbask pelda", FeldolgozoCallback, am); Console.ReadLine(); }
/// <summary> /// StartAsync /// </summary> /// <param name="MyDbCombo"></param> protected void StartAsync(DbCombo MyDbCombo) { AsyncMethodDelegate func = new AsyncMethodDelegate(AsyncMethod); IAsyncResult iar = func.BeginInvoke(MyDbCombo, null, null); }
public static void FeldolgozoCallback(IAsyncResult result) { AsyncMethodDelegate am = (AsyncMethodDelegate)result.AsyncState; Console.WriteLine("Feldolgozas megtortent: {0}", am.EndInvoke(result)); }