/// <summary> /// 轮询异步调用完成 /// </summary> private void Test2() { //输出主线程ID Console.WriteLine("主线程ID:{0}", Thread.CurrentThread.ManagedThreadId); //创建委托 AsyncDelegate asyncDel = new AsyncDelegate(TestMethod); int nThreadID = 0; //异步执行TestMethod方法 IAsyncResult result = asyncDel.BeginInvoke(3000, out nThreadID, null, null); //轮询异步执行状态 while (true) { if (result.IsCompleted) { break; } Thread.Sleep(1000); } //终止异步调用,通过返回值取得调用结果 string returnValue = asyncDel.EndInvoke(out nThreadID, result); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", nThreadID, returnValue); }
public static void CallbackMethod(IAsyncResult ar) { // Retrieve the delegate. AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState; // Call EndInvoke to retrieve the results. PushPayload ret = dlgt.EndInvoke(ar); }
private void CreateSession() { AsyncDelegate andl = new AsyncDelegate(CreateSessionWithUAServerDelegate);//声明一个AsyncDelegate类型的对象andl,并让他指向ad对象的TestMethod方法 IAsyncResult ar = andl.BeginInvoke(null, null); if (andl.EndInvoke(ar)) { //MessageBox.Show("Create session successfully."); //AddSubscriptionBtn.Enabled = true; IsConnected = true; if (_Session != null) { _Session.SubscriptionRequestKeepaliveCount = 5; _Session.PublishNotification += new OnNotification(onSessionPublishNotification); try { //subscription name: s1 Subscr = _Session.AddSubscription("s1", 500); Subscr.PublishStatusChanged += new EventHandler(onSubscrPublishStatusChanged); //subscr.DataChangeCallback += onSubscrDataChangeNotification; } catch (Exception ex) { //MessageBox.Show(ex.Message, "Add Subscription failed."); } } } else { IsConnected = false; } }
private static void CallbackMethod(IAsyncResult ar) { AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState; // Call EndInvoke to retrieve the results. string ret = dlgt.EndInvoke(out threadId, ar); Console.WriteLine("The call executed on thread zhangshuai"); }
internal void OnMethodCallback(IAsyncResult asyncResult) { AsyncDelegate asyncDelegate = (AsyncDelegate)asyncResult.AsyncState; asyncDelegate.EndInvoke(asyncResult); if (methodCallback != null) { methodCallback(); } }
public void CB(IAsyncResult ar) { // this will be executed in another thread t.Tim = ar.ToString(); ad.EndInvoke(ar); syncContext.Post(delegate(object state) { // This will be executed again in form thread lbl_time.Text = t.Tim; }, null); }
/// <summary> /// Finishes asynchronous execution of a SQL statement. /// </summary> /// <param name="asyncResult">The <see cref="IAsyncResult"/> returned by the call /// to <see cref="BeginExecuteNonQuery()"/>.</param> /// <returns></returns> public int EndExecuteNonQuery(IAsyncResult asyncResult) { asyncResult.AsyncWaitHandle.WaitOne(); AsyncDelegate c = Caller; Caller = null; if (thrownException != null) { throw thrownException; } return((int)c.EndInvoke(asyncResult)); }
/// <summary> /// Finishes asynchronous execution of a SQL statement, returning the requested /// <see cref="MySqlDataReader"/>. /// </summary> /// <param name="result">The <see cref="IAsyncResult"/> returned by the call to /// <see cref="BeginExecuteReader()"/>.</param> /// <returns>A <b>MySqlDataReader</b> object that can be used to retrieve the requested rows. </returns> public MySqlDataReader EndExecuteReader(IAsyncResult result) { result.AsyncWaitHandle.WaitOne(); AsyncDelegate c = Caller; Caller = null; if (thrownException != null) { throw thrownException; } return((MySqlDataReader)c.EndInvoke(result)); }
private static void MSMQCallbackMethod(IAsyncResult ar) { AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState; bool returnValue = dlgt.EndInvoke(ar); //傳送失敗 if (!returnValue) { //do some thing here } }
void msgHandle(IAsyncResult ar) { AsyncDelegate dlgt = (AsyncDelegate)ar.AsyncState; int count = dlgt.EndInvoke(ar); string str = ""; for (int i = 0; i < count && i < 5; i++) { str += CConsLog.mLogInfo[i] + " 时间:" + CConsLog.mLogTime[i].ToShortTimeString() + "\t\t"; } labelRoll1.Invoke(new EventHandler(delegate { labelRoll1.Text = str; })); //labelRoll1.Refresh(); }
/// <summary> /// 使用 EndInvoke阻塞调用线程,直到异步调用结束 /// </summary> private void Test() { //输出主线程ID Console.WriteLine("主线程ID:{0}", Thread.CurrentThread.ManagedThreadId); //创建委托 AsyncDelegate asyncDel = new AsyncDelegate(TestMethod); int nThreadID = 0; //异步执行TestMethod方法 IAsyncResult result = asyncDel.BeginInvoke(3000, out nThreadID, null, null); //阻塞调用线程 asyncDel.EndInvoke(out nThreadID, result); this.BeginInvoke(_delegate, new object[] { nThreadID.ToString() }); }
public void DoOldApm() { try { AsyncDelegate asyncDelegate = AsyncWaitThenReturn; IAsyncResult asyncResult = asyncDelegate.BeginInvoke(123, new TimeSpan(0, 0, 1), ar => { var result = asyncDelegate.EndInvoke(ar); Console.WriteLine("result=" + result); }, null); SpinWait.SpinUntil(() => asyncResult.IsCompleted); Console.WriteLine("Async Delegate Finished."); } catch (PlatformNotSupportedException ex) { Console.WriteLine("PlatformNotSupportedException=" + ex); } }
public void AsyncCallCompleted(IAsyncResult ar) { Console.WriteLine("AsyncCallCompleted执行线程ID:{0}", Thread.CurrentThread.ManagedThreadId); //获取委托对象 System.Runtime.Remoting.Messaging.AsyncResult result = (System.Runtime.Remoting.Messaging.AsyncResult)ar; AsyncDelegate asyncDel = (AsyncDelegate)result.AsyncDelegate; //获取BeginInvoke传入的的state参数 string strState = (string)ar.AsyncState; Console.WriteLine("传入的字符串是{0}", strState); //结束异步调用 int nThreadID; string strResult = asyncDel.EndInvoke(out nThreadID, ar); this.BeginInvoke(_delegate, new object[] { nThreadID.ToString() }); Console.WriteLine("\nThe call executed on thread {0}, with return value \"{1}\".", nThreadID, strResult); }
/// <summary> /// 使用 WaitHandle 等待异步调用 /// </summary> private void Test1() { //输出主线程ID Console.WriteLine("主线程ID:{0}", Thread.CurrentThread.ManagedThreadId); //创建委托 AsyncDelegate asyncDel = new AsyncDelegate(TestMethod); int nThreadID = 0; //异步执行TestMethod方法 IAsyncResult result = asyncDel.BeginInvoke(3000, out nThreadID, null, null); //阻塞调用线程 WaitHandle handle = result.AsyncWaitHandle; handle.WaitOne(); //其他操作 //终止异步调用,通过返回值取得调用结果 string returnValue = asyncDel.EndInvoke(out nThreadID, result); Console.WriteLine("The call executed on thread {0}, with return value \"{1}\".", nThreadID, returnValue); }
// Метод по сигнатуре подходит под делегат EndEventHandler public void OnEnd(IAsyncResult ar) { _delegate.EndInvoke(ar); _data += "Задача завершена " + DateTime.Now.Ticks.ToString() + " "; }
private void AsyncCallback(System.IAsyncResult result) { AsyncDelegate d = (AsyncDelegate)result.AsyncState; d.EndInvoke(null); }