does AsyncCallback get invoked when there is exception thrown at the worker thread

不想你离开。 提交于 2019-12-11 17:35:43

问题


I'm new to .net asynchronous programming(Asynchronous Programming Model), just have two questions on the relationship of worker thread and exceptions. Below is an example:

private void OnPerformSearch(object sender, RoutedEventArgs e)
{
    WebRequest req = WebRequest.Create("http://www.google.com/#q=weather");
    req.BeginGetResponse(new AsyncCallback(Callback), req);  // here the main thread calls BeginGetResponse() to enqueues the work in threadpool then a worker thread A will get the job done
    ...// do other stuff 
}

private void Callback(IAsyncResult iar)
{
   WebRequest req = (WebRequest) iar.AsyncState;
   try
   {
      WebResponse resp = req.EndGetResponse(iar);
      ProcessResponse(resp);
   }
   catch(Exception x)
   {
      LogError(x);
   }
}

Q1-We know that BeginGetResponse method enqueues the work in threadpool and returns immediately, and the caller can now get on with other work, and then a worker thread (let's it is thread A) in the thread pool will get the job done. My question is, when the thread A finished the work, is the threadpool going to assign a different worker thread(thread B) to invoke the completion callback? or the thread A will continue to invoke the callback?

Q2-we know that the completion callback that gets invoked when the async operation completes. But if there is error when the worker thread A was doing the job, will the work be deemed as "completed" so that the callback can be invoked? or the process just get stuck forever until we restart the application?

来源:https://stackoverflow.com/questions/57841920/does-asynccallback-get-invoked-when-there-is-exception-thrown-at-the-worker-thre

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!