C# 限制任务时间并在获得结果或xamarin超时时返回
我有一个任务需要超时,它应该在60秒内从SOAP服务接收结果 想法是:«SOAP,你有60秒的时间给我结果;要么他们及时到达,要么我无法接收他们,必须提醒用户出了问题。» 英语很容易,但我已经尝试了一些c#和NoGo的选项…: 到目前为止,我试过: 任务。等待(60000)C# 限制任务时间并在获得结果或xamarin超时时返回,c#,xamarin,soap,xamarin.forms,task,C#,Xamarin,Soap,Xamarin.forms,Task,我有一个任务需要超时,它应该在60秒内从SOAP服务接收结果 想法是:«SOAP,你有60秒的时间给我结果;要么他们及时到达,要么我无法接收他们,必须提醒用户出了问题。» 英语很容易,但我已经尝试了一些c#和NoGo的选项…: 到目前为止,我试过: 任务。等待(60000) 任务延迟(60000) Device.StartTimer(TimeSpan.frommins(1),()=>{tcs.TrySetResults(results);返回true;}) 我们还是一无所有。它要么等待整个60秒
任务延迟(60000)
Device.StartTimer(TimeSpan.frommins(1),()=>{tcs.TrySetResults(results);返回true;}) 我们还是一无所有。它要么等待整个60秒,要么如果出现问题,它永远不会停止 我对这种手术还不熟悉,所以我知道我很可能做了一些非常愚蠢的事情,或者做了一些非常简单的事情,但不管怎样我都迷路了 我在Xamarin形式中使用c# 需要帮忙吗 提前谢谢 编辑:对soap请求进行编码
ObservableCollection<ItemDto> LoadList(string searchString)
{
TaskCompletionSource<ObservableCollection<ItemDto>> tcs = new TaskCompletionSource<ObservableCollection<ItemDto>>();
ObservableCollection<ItemDto> results = new ObservableCollection<ItemDto>();
try
{
MobileClient clientMobile = new MobileClient(new BasicHttpBinding(), new EndpointAddress(_endpointUrl));
clientMobile.FindItemsCompleted += (object sender, FindItemsCompletedEventArgs e) =>
{
_error = (e.Error != null) ? e.Error.Message : ((e.Cancelled) ? "Cancelled" : string.Empty);
if (string.IsNullOrWhiteSpace(_error) && e.Result.Count() > 0)
{
results = SetAdList(e.Result);
tcs.TrySetResult(results);
}
};
clientMobile.FindItemsAsync(SetSearchParam(searchString, 100));
}
catch (Exception ex)
{
Debug.WriteLine("Failed to Load List. Ex: {0}", ex.ToString());
return new ObservableCollection<ItemDto>();
}
return tcs.Task.Result;
}
如我所见,您使用的是BasicHTTPClient。您可以尝试以下方法:
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.OpenTimeout = new TimeSpan(0, 1, 0);
myBinding.CloseTimeout = new TimeSpan(0, 1, 0);
myBinding.SendTimeout = new TimeSpan(0, 1, 0);
MobileClient clientMobile = new MobileClient(myBinding, new EndpointAddress(_endpointUrl));
另一方面,您应该在LoadList方法中返回一个Task>(参见此示例)。然后你可以启动一个并行计时器(从另一个)
如我所见,您使用的是BasicHTTPClient。您可以尝试以下方法:
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.OpenTimeout = new TimeSpan(0, 1, 0);
myBinding.CloseTimeout = new TimeSpan(0, 1, 0);
myBinding.SendTimeout = new TimeSpan(0, 1, 0);
MobileClient clientMobile = new MobileClient(myBinding, new EndpointAddress(_endpointUrl));
另一方面,您应该在LoadList方法中返回一个Task>(参见此示例)。然后你可以启动一个并行计时器(从另一个)
发布发出soap请求的代码。
tcs.TrySetResults(结果)
是否有该方法的异步版本可用(带或不带CancellationToken
选项)?不知道,这是TaskCompletionSource类,不是我的自定义类:sDoes FindItemsAsync return task?不,只是返回结果或在Api端引发异常…:sPost发出soap请求的代码。tcs.TrySetResults(结果)
是否有该方法的异步版本可用(带或不带CancellationToken
选项)?不知道,这是TaskCompletionSource类,不是我的自定义类:sDoes FindItemsAsync return task?不,只是返回结果或在Api端引发异常…:s
BasicHttpBinding myBinding = new BasicHttpBinding();
myBinding.OpenTimeout = new TimeSpan(0, 1, 0);
myBinding.CloseTimeout = new TimeSpan(0, 1, 0);
myBinding.SendTimeout = new TimeSpan(0, 1, 0);
MobileClient clientMobile = new MobileClient(myBinding, new EndpointAddress(_endpointUrl));
int timeout = 60000;
var task = SomeOperationAsync();
if (await Task.WhenAny(task, Task.Delay(timeout)) == task) {
// task completed within timeout
} else {
// timeout logic
}