C# 如何把异步调用模拟成同步调用?

wangping06 2010-01-07 09:07:28
如题,有两个异步调用方法,现在想在异步调用方法1完成之后再执行方法2,请问有哪些比较好的解决方法!
...全文
887 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangping06 2010-01-07
  • 打赏
  • 举报
回复
在控制台能运行,但是在Silverlight 中不能运行,用新线程打开也不行
wangping06 2010-01-07
  • 打赏
  • 举报
回复
IAsyncResult ar = synchronousDelegate.BeginInvoke(asyncCallback, synchronousDelegate);

//这句出错, Specified method is not supported
wangping06 2010-01-07
  • 打赏
  • 举报
回复

public delegate void SynchronousDelegate();

private void LoadData()
{
SynchronousDelegate synchronousDelegate=new SynchronousDelegate(Function);
AsyncCallback asyncCallback = new AsyncCallback(MyAsyncCallback);
IAsyncResult ar = synchronousDelegate.BeginInvoke(asyncCallback, synchronousDelegate); //这句出错, Specified method is not supported.
}

public void MyAsyncCallback(IAsyncResult asyncResult)
{
// Because you passed your original delegate in the asyncState parameter
// of the Begin call, you can get it back here to complete the call.
SynchronousDelegate synchronousDelegate = (SynchronousDelegate)asyncResult.AsyncState;
// Complete the call.
synchronousDelegate.EndInvoke(asyncResult);

}

public void Function()
{}

在silverlight中,为啥用这个有错,
aa1018 2010-01-07
  • 打赏
  • 举报
回复
用插入线程,如线程1调用方法1,线程2掉用方法2,只要将线程1插入到线程2,这样就先执行线程1完了才执行线程2
  • 打赏
  • 举报
回复
而方法2的重构出来的第一部分只是设置环境参数,将回调Delegate注册到方法1,然后就直接退出了。
  • 打赏
  • 举报
回复
呵呵,Delegate回调深入人心啊。

确实是这样:方法2重构设计成两部分,方法1完成才回调方法2重构出来的第二部分。
kobe082005 2010-01-07
  • 打赏
  • 举报
回复
如果你想在这个函数结束时做一些事情,你可以写在这个回调函数里.
wangping06 2010-01-07
  • 打赏
  • 举报
回复

serviceSoapClient.GetDataSetToStringCompleted += new EventHandler<SilverlightApplication1.WebSevices.GetDataSetToStringCompletedEventArgs>(serviceSoapClient_GetTaskTotal);
serviceSoapClient.GetDataSetToStringAsync(className, methodName, new ArrayOfAnyType() { type });
}

private void serviceSoapClient_GetTaskTotal(object sender, WebSevices.GetDataSetToStringCompletedEventArgs e)
{
//想问一下,写在这样的回调函数里?
}


mzjmicrosoft 2010-01-07
  • 打赏
  • 举报
回复

/*

* 本节, 中示例提供对 BeginInvoke() 函数, 异步调用完成后系统执行回调委托。
* 回调调用 EndInvoke() 并处理异步调用的结果。
* 如果启动异步调用线程不需要处理结果是调用此调用模式很有用。
* 异步调用完成后系统调用线程以外启动线程上调。
* 若使用此调用模式, 作为第二到最后 - BeginInvoke() 函数的参数必须传递 AsyncCallback 类型的委托。
* BeginInvoke() 还有最后参数键入 对象 到您可以将任何对象。 当它调用该对象可用于您回调函数。
* 为此参数一个重要用途是以传递用于初始化调用该委托。
* 回调函数然后使用与该委托 EndInvoke() 函数来完成调用。 此调用模式是所示。
* */
//// <summary>
/// 示例 5: 异步方法完成后执行回调
/// </summary>
public void DemoCallback()
{
MethodDelegate dlgt = new MethodDelegate (this.LongRunningMethod) ;
string s ;
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(3000, 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) ;

Console.WriteLine(string.Format ("The delegate call returned the string: "{0}", and the number {1}", s, iExecThread.ToString() ) );
}
zhujiazhi 2010-01-07
  • 打赏
  • 举报
回复
异步没有回调函数的嘛?
phommy 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wangping06 的回复:]
引用 1 楼 angel6709 的回复:
在方法1结束之后(方法1的}内),invoke方法2

我想做的是
Function 1
Function 2
两个是独立的,就是Function 1中不包含任何调用Function 2的

相当于
C# code
Function//这里面怎么写{
Function1
Function2
}
[/Quote]

在Function 1的CallBack里直接调用Function2就可以
白头老汉 2010-01-07
  • 打赏
  • 举报
回复
用同步机制,参考
http://blog.csdn.net/forlinux/archive/2008/08/08/2787756.aspx
michaelnami 2010-01-07
  • 打赏
  • 举报
回复
等Function 1return 然后Function 2
或者Function 1 sleep();
再或者Function 1 {...;Function 2}
wangping06 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 angel6709 的回复:]
在方法1结束之后(方法1的}内),invoke方法2
[/Quote]
我想做的是
Function 1
Function 2
两个是独立的,就是Function 1中不包含任何调用Function 2的

相当于

Function//这里面怎么写
{
Function 1
Function 2
}
angel6709 2010-01-07
  • 打赏
  • 举报
回复
在方法1结束之后(方法1的}内),invoke方法2

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧