您好我在以下方法中使用Async And Wait有问题。 基本上我使用Neevia转换器转换我的文档,我想等待我的方法,直到转换完成。
你能帮忙吗?
public void AsyncAndWaitTillExecutionMethod(string fileName) {
CallForConverter(fileName);
}
private async void CallForConverter(string fileName) {
var result = await CallForConverterAsync(fileName);
}
private Task<string> CallForConverterAsync(string fileName) {
string fromLocation;
string toLocation;
// For data source
string fileFullName = fileName;
fromLocation = "abc";
toLocation = "xyz";
return Task.Factory.StartNew(() => ConvertUsingNeevia(fromLocation, toLocation));
}
private string ConvertUsingNeevia(string from, string to) {
string success = "false";
int TimeOut = 3000;
try {
Neevia.docConverter NVDC = new Neevia.docConverter();
int Res = NVDC.convertFile(from, to, TimeOut);
}
catch (Exception ex) {
throw new Exception("Exception ", ex);
}
success = "True";
return success;
}
它从CallForConverter()方法给出了以下错误。 此时无法启动异步操作。异步操作只能在异步处理程序或模块中启动,或者在MVC页面生命周期中的某些事件中启动......
答案 0 :(得分:4)
只需从代码中删除所有async
,await
和(可怕的)StartNew
:
public void AsyncAndWaitTillExecutionMethod(string fileName) {
string fromLocation;
string toLocation;
// For data source
string fileFullName = fileName;
fromLocation = "abc";
toLocation = "xyz";
ConvertUsingNeevia(fromLocation, toLocation);
}