C#异步调用实例小结
程序员文章站
2023-11-16 22:23:40
本文实例讲述了c#异步调用的方法。分享给大家供大家参考。具体如下:
using system;
using system.collections.generic...
本文实例讲述了c#异步调用的方法。分享给大家供大家参考。具体如下:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.threading; using system.windows.forms; namespace cw { public partial class asyncdemo : form { public asyncdemo() { initializecomponent(); } private void delgate_load(object sender, eventargs e) { } /// <summary> /// 实现委托的方法 /// </summary> /// <param name="icalltime"></param> /// <param name="iexecthread"></param> /// <returns></returns> string longrunningmethod(int icalltime, out int iexecthread) { thread.sleep(icalltime); iexecthread = appdomain.getcurrentthreadid(); return "mycalltime was " + icalltime.tostring(); } delegate string methoddelegate(int icalltime, out int iexecthread); #region 示例 1: 同步调用方法#region 示例 1: 同步调用方法 /// <summary> /// 示例 1: 同步调用方法 /// </summary> public void demosynccall() { string s; int iexecthread; // create an instance of a delegate that wraps longrunningmethod. methoddelegate dlgt = new methoddelegate(this.longrunningmethod); // call longrunningmethod using the delegate. s = dlgt(3000, out iexecthread); messagebox.show(string.format ("the delegate call returned the string: {0}, and the thread id {1}", s, iexecthread.tostring() ) ); } #endregion #region 示例 2: 通过 endinvoke() 调用模式异步调用方法 /// <summary> /// 示例 2: 通过 endinvoke() 调用模式异步调用方法 /// </summary> public void demoendinvoke() { methoddelegate dlgt = new methoddelegate(this.longrunningmethod); string s; int iexecthread; // initiate the asynchronous call. iasyncresult ar = dlgt.begininvoke(5000, out iexecthread, null, null); // do some useful work here. this would be work you want to have // run at the same time as the asynchronous call. // retrieve the results of the asynchronous call. s = dlgt.endinvoke(out iexecthread, ar); messagebox.show(string.format ("the delegate call returned the string: {0}, and the number {1}", s, iexecthread.tostring() ) ); } #endregion #region 示例 3: 异步调用方法并使用 a waithandle 来等待调用完成 /// <summary> /// 示例 3: 异步调用方法并使用 a waithandle 来等待调用完成 /// </summary> public void demowaithandle() { string s; int iexecthread; methoddelegate dlgt = new methoddelegate(this.longrunningmethod); // initiate the asynchronous call. iasyncresult ar = dlgt.begininvoke(3000, out iexecthread, null, null); // do some useful work here. this would be work you want to have // run at the same time as the asynchronous call. // wait for the waithandle to become signaled. ar.asyncwaithandle.waitone(); // get the results of the asynchronous call. s = dlgt.endinvoke(out iexecthread, ar); messagebox.show(string.format ("the delegate call returned the string: {0}, and the number {1}", s, iexecthread.tostring() ) ); } #endregion #region 示例 4: 异步调用方法通过轮询调用模式 /// <summary> /// 示例 4: 异步调用方法通过轮询调用模式 /// </summary> public void demopolling() { methoddelegate dlgt = new methoddelegate(this.longrunningmethod); string s; int iexecthread; // initiate the asynchronous call. iasyncresult ar = dlgt.begininvoke(3000, out iexecthread, null, null); // poll iasyncresult.iscompleted while (ar.iscompleted == false) { thread.sleep(10); // pretend to so some useful work } s = dlgt.endinvoke(out iexecthread, ar); messagebox.show(string.format ("the delegate call returned the string: {0}, and the number {1}", s, iexecthread.tostring() ) ); } #endregion #region 示例 5: 异步方法完成后执行回调 /// <summary> /// 示例 5: 异步方法完成后执行回调 /// </summary> public void democallback() { methoddelegate dlgt = new methoddelegate(this.longrunningmethod); int iexecthread; // create the callback delegate. asynccallback cb = new asynccallback(myasynccallback); // initiate the asynchronous call passing in the callback delegate // and the delegate object used to initiate the call. iasyncresult ar = dlgt.begininvoke(5000, out iexecthread, cb, dlgt); } public void myasynccallback(iasyncresult ar) { string s; int iexecthread; // because you passed your original delegate in the asyncstate parameter // of the begin call, you can get it back here to complete the call. methoddelegate dlgt = (methoddelegate)ar.asyncstate; // complete the call. s = dlgt.endinvoke(out iexecthread, ar); messagebox.show(string.format("the delegate call returned the string: {0}, and the number {1}", s, iexecthread.tostring())); //console.writeline(string.format ("the delegate call returned the string: "{0}", and the number {1}", s, iexecthread.tostring() ) ); } #endregion private void button1_click(object sender, eventargs e) { //demosynccall() ; //demoendinvoke(); //demowaithandle(); //demopolling(); democallback(); } } }
希望本文所述对大家的c#程序设计有所帮助。
下一篇: 改进性能和样式的24个ASP 技巧