Chrome上的WebClient CancelAsync()无法按预期工作 [英] WebClient CancelAsync() on Chrome does not work as expected

查看:124
本文介绍了Chrome上的WebClient CancelAsync()无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WebClient.OpenReadAsync下载文件。  当我使用WebClient.CancelAsync()请求取消异步下载时,OnProgress事件将停止,并且我会按预期获得状态为args.Canceled == true的OpenReadCompleted事件。
 但是,在Chrome上我注意到(使用n / w监控工具或Chrome-> Tools-> DeveloperTools->网络)文件继续在后台下载,即使在调用  CancelAsync()咀嚼有价值的n / w带宽。
 终止TCP流程的唯一方法是关闭Chrome标签页。 

I'm trying to download a file using WebClient.OpenReadAsync.  When I cancel the async download using WebClient.CancelAsync() request, the OnProgress events are stopped and I get a OpenReadCompleted event with status args.Canceled==true as expected.  However,  on Chrome I  noticed (using n/w monitoring tool or Chrome->Tools->DeveloperTools->Network) that the file continues to get downloaded in the background even after calling  CancelAsync() chewing valuable n/w bandwidth.  The only way to terminate the TCP flow is by closing the Chrome tab. 

我在Safari和IE上运行的应用正常运行,如预期的那样,WebClient .CancelAsync()导致客户端关闭底层TCP连接。

My app running  on Safari and IE work fine as expected where WebClient.CancelAsync() results in the client closing the underlying TCP connection.

有人知道这是Chrome上Silverlight插件的已知问题吗?  有办法解决这个问题吗?

Does anyone know if this is a know issue with the Silverlight plugin on Chrome?   Any ways to workaround this?

Chrome上的我的插件版本是Silverlight   -   版本:   5.1.20513.0

My plugin version on Chrome is Silverlight Version: 5.1.20513.0

//示例代码

公共类页面:UserControl {
$


    WebClient客户端;

    long bytesDownloaded = 0;

public class Page: UserControl {

    WebClient client;
    long bytesDownloaded = 0;

   公共页面(){

        InitializeComponent();

    }


    private void startDownloadOn_MouseLeftButtonUp(object sender,MouseButtonEventArgs e){
$


        client = new WebClient(); //创建WebClient; 

        client.OpenReadCompleted + = new OpenReadCompletedEventHandler(client_OpenReadCompleted);



        client.DownloadProgressChanged + = new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);

        client.OpenReadAsync(new Uri(" http://mirror.internode.on.net/pub/test/1000meg.test",null));

    }


    private void startCancelOn_MouseRightButtonUp(object sender,MouseButtonEventArgs e){

        //取消下载

        if((client!= null)&&(client.isBusy)){

            client.CancelAsync()

        }¥b $ b    }


    void client_OpenReadCompleted(object sender,OpenReadCompletedEventArgs args){

    public Page() {
        InitializeComponent();
    }

    private void startDownloadOn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) {

        client = new WebClient(); // create WebClient; 
        client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
        client.OpenReadAsync(new Uri("http://mirror.internode.on.net/pub/test/1000meg.test", null));
    }

    private void startCancelOn_MouseRightButtonUp(object sender, MouseButtonEventArgs e) {
        // cancel download
        if ((client != null) && (client.isBusy)) {
            client.CancelAsync()
        }
    }

    void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs args) {

        if(!args.Cancelled){

            MessageBox.Show(" Download completed" + args.ToString()+" length =" + args.Result.Length);

        }其他{

            MessageBox.Show(" Download canceled:" +" Bytes Downloaded =" + bytesDownloaded);

        }¥b $ b    }


    void client_DownloadProgressChanged(object sender,DownloadProgressChangedEventArgs args){

        bytesDownloaded = args.BytesReceived;

    }

        if (!args.Cancelled) {
            MessageBox.Show("Download completed " + args.ToString() + " length = " + args.Result.Length);
        } else {
            MessageBox.Show(" Download canceled : " + " Bytes Downloaded = " + bytesDownloaded);
        }
    }

    void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs args) {
        bytesDownloaded = args.BytesReceived;
    }




推荐答案

您好,

您提到的代码看起来不错。

The code you mentioned looks fine.

  ; http://msdn.microsoft.com/en-us/library/system .net.webclient.cancelasync.aspx 说:

如果某个操作处于暂挂状态,则此方法调用 中止 $ b WebRequest

当您致电 CancelAsync ,您的应用程序仍会收到与该操作关联的完成事件。例如,当您拨打
CancelAsync 取消 < a href ="http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadstringasync.aspx"> DownloadStringAsync 操作,如果您为 指定了一个事件
处理程序 DownloadStringCompleted
事件,您的事件处理程序会收到操作已结束的通知。要
了解操作是否成功完成,请检查
已取消 属性 DownloadDataCompletedEventArgs

When you call CancelAsync, your application still receives the completion event associated with the operation. For example, when you call CancelAsync to cancel a DownloadStringAsync operation, if you have specified an event handler for the DownloadStringCompleted event, your event handler receives notification that the operation has ended. To learn whether the operation completed successfully, check the Cancelled property on the base class of DownloadDataCompletedEventArgs in the event data object passed to the event handler.

如果取消操作已执行,则取消的属性会显示。

The Cancelled property manifests if the cancel operation executed or not.

另外,我想知道哪个网络中的项目让您知道该文件继续下载。

additionally, I want to know which item in Network let you know the file continues to get downloaded.

#评估Chrome中的网络效果

# Evaluating network performance in Chrome

https://developers.google.com/chrome-developer-tools/docs/network

还请尝试将Silverlight更新为最新版本并检查它是否适用于Chrome。

Please also try to update your Silverlight to latest version and check if it works in Chrome.

希望这有帮助,谢谢。


这篇关于Chrome上的WebClient CancelAsync()无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆