检测Webclient.DownloadFileAsync何时因动态IP地址的更改而挂起

时间:2016-12-13 15:45:51

标签: c# winforms download ip webclient

我正在使用webclient进行大型文件下载。

在我的国家,每12个小时,Ip更改,所以如果用户正在下载文件和他的ipChanges,下载挂起,但我没有得到反馈,它被绞死..

我想在Windows窗体中检测到这一点,c#。

public void DownloadFile(string urlAddress, string location)
{
    try
    {
        // The stopwatch which we will be using to calculate the download speed
        using (webClient = new WebClient())
        {                 
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            // The variable that will be holding the url address (making sure it starts with http://)
            Uri URL = urlAddress.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ? new Uri(urlAddress) : new Uri("http://" + urlAddress);

            // Start the stopwatch which we will be using to calculate the download speed
            sw.Start();

            // Start downloading the file
            webClient.DownloadFileAsync(new Uri(urlAddress), location);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

我也试过调用它来检测IP地址更改但是在手动重置路由器时它没有被触发。

public Form1()
{ 
    NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback);
}

private void AddressChangedCallback(object sender, EventArgs e)
{
    //Code here...
}

0 个答案:

没有答案