正确使用.Net web引用客户端的多线程
我必须使用一个.NET2.0时代的.asmxWeb服务,这不是我构建的,也无法控制。但是,该服务具有高可用性,可以处理此问题 我需要的帮助是客户端,并从Concurence的角度正确使用VisualStudio生成的客户端代理。目前我正在使用Web引用,但如果需要,可以切换到服务引用。不过我对WCF一无所知 我读得越多,就越迷惑自己 我的具体问题: 给定一个visual studio生成的soap客户端代理,该代理生成以下方法:正确使用.Net web引用客户端的多线程,.net,multithreading,soap,task-parallel-library,webservice-client,.net,Multithreading,Soap,Task Parallel Library,Webservice Client,我必须使用一个.NET2.0时代的.asmxWeb服务,这不是我构建的,也无法控制。但是,该服务具有高可用性,可以处理此问题 我需要的帮助是客户端,并从Concurence的角度正确使用VisualStudio生成的客户端代理。目前我正在使用Web引用,但如果需要,可以切换到服务引用。不过我对WCF一无所知 我读得越多,就越迷惑自己 我的具体问题: 给定一个visual studio生成的soap客户端代理,该代理生成以下方法: Foo(); FooAsync(); FooCompleted;
Foo();
FooAsync();
FooCompleted;
Bar();
BarAsync();
BarCompleted;
Foo()
(您必须自己决定是否调用)。它们运行(就像你建议的那样)就像它们并行运行一样Foo()
生成了一个线程,那么为fooancy()
生成一个线程是多余的,但这取决于您正在做什么或想要做什么。通常,您会从UI线程调用Foo()
,而UI线程又会在单独的线程上启动fooancy()
private void Foo()
{
// Get TaskScheduler to facilitate manipulation of GUI on UI Thread.
TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
// Cancellation support.
CancellationTokenSource cancelSource = new CancellationTokenSource();
CancellationToken token = cancelSource.Token;
// Spin-off onto background thread.
Task<bool> asyncFooTask = null;
asyncFooTask = Task.Factory.StartNew<bool>(() => asyncFoo(uiScheduler, token, _dynamic), token);
// Continuation/call-back/error-handling.
asyncTask.ContinueWith(task =>
{
// Check task status.
switch (task.Status)
{
// Handle any exceptions to prevent UnobservedTaskException.
case TaskStatus.RanToCompletion:
if (asyncTask.Result)
// Success. Do stuff.
else
// Failed. Do stuff.
break;
case TaskStatus.Canceled:
if (task.Exception != null)
// Cancelled with exception.
else
// User cancelled.
break;
case TaskStatus.Faulted:
if (task.Exception != null)
// AggregateException thrown by antecident.
else
// Task failed...
break;
}
return;
}, TaskScheduler.FromCurrentSynchronizationContext());
}
private void Foo()
{
//获取TaskScheduler以便于在UI线程上操作GUI。
TaskScheduler uiScheduler=TaskScheduler.FromCurrentSynchronizationContext();
//取消支持。
CancellationTokenSource cancelSource=新的CancellationTokenSource();
CancellationToken token=cancelSource.token;
//剥离到背景线程上。
任务AsyncFootTask=null;
AsyncFootTask=Task.Factory.StartNew(()=>asyncFoo(uiScheduler,token,_dynamic),token);
//继续/回拨/错误处理。
asyncTask.ContinueWith(任务=>
{
//检查任务状态。
开关(任务状态)
{
//处理任何异常以防止未观察到的异常。
案例任务状态.RANTO完成:
if(asyncTask.Result)
//成功,做事。
其他的
//失败了,做点什么。
打破
案例任务状态。已取消:
if(task.Exception!=null)
//例外地取消了。
其他的
//用户已取消。
打破
案例任务状态。出现故障:
if(task.Exception!=null)
//antecident引发的AggregateException。
其他的
//任务失败。。。
打破
}
返回;
},TaskScheduler.FromCurrentSynchronizationContext());
}
fooancy()
和BarAsync()
完成后运行'foopleted()和'BarCompleted()
。在这种情况下,我想说EPL模式正是您想要的
我希望这有帮助