Monotouch Webclient.CancelAsync ThreadInterruptedException

时间:2012-05-21 17:34:33

标签: multithreading xamarin.ios webclient interrupted-exception webclient-download

我使用WebClient.DownloadDataAsync()从网上下载数据。

WebClient webClient;
void WebClientDownload(MediaInfo media)
{
    if (webClient == null)
    {
        webClient = new WebClient();
        webClient.DownloadProgressChanged += (sender, e) => {
            // use e.ProgressPercentage for change UIProgressView.Progress value by InvokeOnMainThread
        };
        webClient.DownloadDataCompleted += (sender, e) => {
            if (e.Cancelled == false && e.Error == null)
            {
                // saveing data
            }
        };
    }

    webClient.DownloadDataAsync(new Uri("http://..."));
}

但是当我使用webClient.CancelAsync();取消下载时,我会遇到以下异常:

System.Threading.ThreadInterruptedException: Thread interrupted
  at (wrapper managed-to-native) System.Threading.WaitHandle:WaitOne_internal (System.Threading.WaitHandle,intptr,int,bool)
  at System.Threading.WaitHandle.WaitOne (Int32 millisecondsTimeout, Boolean exitContext) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/WaitHandle.cs:376
  at System.Net.WebAsyncResult.WaitUntilComplete (Int32 timeout, Boolean exitContext) [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebAsyncResult.cs:164
  at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnectionStream.cs:327
  at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x0010d] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:909
  at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x0000a] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:246

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是known bug,应该在2.10中修复。