客户端请求某一方法,该方法需要引用win32或者console的exe,计算量较大,在新的进程里面处理。
public ActionResult Test()
{
process.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
}
public string p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
//新的线程会将处理结果返回到这里 ,如何能返回给客户端?
}
mvc 4.0 及更高版本下可以直接用 async/await:
public async Task<ActionResult> Test() { var tcs = new TaskCompletionSource<TDataType>(); process.OutputDataReceived += (sender, e) => tcs.SetResult(e.Data); var data = await tcs.Task; //现在有数据了,按你的需求处理吧 return View(data); }
能简单的理解为异步吗
上面的叫:异步等待