Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用进度报告方法会显著降低WindowsPhone7上的C#文件下载速度吗?_C#_.net_Silverlight_Windows Phone 7_Webclient - Fatal编程技术网

调用进度报告方法会显著降低WindowsPhone7上的C#文件下载速度吗?

调用进度报告方法会显著降低WindowsPhone7上的C#文件下载速度吗?,c#,.net,silverlight,windows-phone-7,webclient,C#,.net,Silverlight,Windows Phone 7,Webclient,我有一个C语言的WindowsPhone7(7.1)方法,它以字符串形式给出一个URL,然后将该URL的内容下载到一个文件中(见下面的代码)。从代码中可以看到,我分配了一个DownloadProgressChanged()事件处理程序。在该处理程序中,如果调用者提供了一个IProgress对象,我将对该对象调用Report()方法。考虑到用户的网络连接可能很慢,我想确保下载速度尽可能快。在WebClient的DownloadProgressChanged()回调中调用IProgress.Repo

我有一个C语言的WindowsPhone7(7.1)方法,它以字符串形式给出一个URL,然后将该URL的内容下载到一个文件中(见下面的代码)。从代码中可以看到,我分配了一个DownloadProgressChanged()事件处理程序。在该处理程序中,如果调用者提供了一个IProgress对象,我将对该对象调用Report()方法。考虑到用户的网络连接可能很慢,我想确保下载速度尽可能快。在WebClient的DownloadProgressChanged()回调中调用IProgress.Report()方法会大大降低下载速度吗

我对IProgress.Report()不够熟悉,不知道它是在当前线程还是在调用线程上执行。它是否在调用线程上执行?我担心重复的线程切换会使事情陷入困境。我可能会将对该方法的调用包装在Task.Run()调用中,以使UI线程满意。但为了以防万一,我会问我的代码是否有任何潜在的问题,因为UI线程陷入困境

对于本规范中与结构或性能相关的任何其他意见,我们将不胜感激。注意,我在这个应用程序中使用的是Microsoft.Bcl.Async

更新(稍后添加):关于线程切换,显然DownloadProgressChanged()事件是在UI线程上引发的,而不是在下载线程上引发的,因此在这种情况下,不需要使用Dispatcher之类的工具来进行UI更新。至少根据本规范项目条款:

publicstaticvoidurltofile(stringstrurl,stringstrdestfilename,IProgress progress,int-iNumSecondsToWait=30)
{
strUrl=strUrl.Trim();
if(String.IsNullOrWhiteSpace(strUrl))
抛出新ArgumentException(((Misc::URLToFile)URL为空。“);
strDestFilename=strDestFilename.Trim();
if(String.IsNullOrWhiteSpace(strDestFilename))
抛出新ArgumentException(((Misc::URLToFile)目标文件名为空。”);
如果(iNumSecondsToWait<1)
抛出新ArgumentException(((Misc::URLToFile)等待的秒数小于1。”);
//创建隔离存储文件。
StreamWriter sw=openIsoStorFileAsStreamWriter(streamFileName);
//如果流编写器为空,则无法创建文件。
如果(sw==null)
抛出新的System.IO.IOException(((Misc::URLToFile)错误创建或写入名为“+strDestFilename”的文件;
//异步下载。注意,Silverlight版本的WebClient*不*实现
//可识别的。
WebClient wc=新的WebClient();
尝试
{
//创建下载进度更改处理程序,以便我们可以传递进度
//如果调用者提供了进度报告对象,则向调用者报告。
wc.DownloadProgressChanged+=(s,e)=>
{
//我们有进度报告处理程序吗?
如果(进度!=null)
//是的,叫它。
进度报告(如进度百分比);
};
//为“已完成”处理程序使用Lambda表达式
//将内容写入文件的。
wc.OpenReadCompleted+=(s,e)=>
e、 结果.CopyTo(软件基流);
//现在打电话下载文件。
DownloadStringAsync(新Uri(strUrl));
}
最后
{
//确保溪流已清理干净。
sw.Flush();
sw.Close();
//确保StreamWriter已关闭。
sw.Dispose();
}//尝试/最后
//CancellationTokenSource srcCancelToken=新的CancellationTokenSource();
//srcCancelToken.CancelAfter(TimeSpan.FromSeconds(iNumSecondsToWait));
}//公共静态void URLToFile()

,下载进度检查不一定会影响下载速度。主要是因为选中的项目本身没有下载。当您开始下载时,您会得到一个大小声明(内容长度)-该声明用作完整文件的引用。然后,检查本地(下载的)字节内容的大小,并根据这两个值建立一个比率


注意:不适用于流媒体,原因很明显,因为没有最终的大小估计。

谢谢。您知道对IProgress.Report()的调用是在调用线程上还是在创建IProgress对象的线程上发生的吗?WebClient下载和相关进度报告是在UI线程上完成的。
    public static void URLToFile(string strUrl, string strDestFilename, IProgress<int> progress, int iNumSecondsToWait = 30)
    {
        strUrl = strUrl.Trim();

        if (String.IsNullOrWhiteSpace(strUrl))
            throw new ArgumentException("(Misc::URLToFile) The URL is empty.");

        strDestFilename = strDestFilename.Trim();

        if (String.IsNullOrWhiteSpace(strDestFilename))
            throw new ArgumentException("(Misc::URLToFile) The destination file name is empty.");

        if (iNumSecondsToWait < 1)
            throw new ArgumentException("(Misc::URLToFile) The number of seconds to wait is less than 1.");

        // Create the isolated storage file.
        StreamWriter sw = openIsoStorFileAsStreamWriter(strDestFilename);

        // If the stream writer is NULL, then the file could not be created.
        if (sw == null)
            throw new System.IO.IOException("(Misc::URLToFile) Error creating or writing to the file named: " + strDestFilename);

        // Asynchronous download.  Note, the Silverlight version of WebClient does *not* implement 
        //  IDisposable.
        WebClient wc = new WebClient();

        try
        {
            // Create a download progress changed handler so we can pass on progress
            //  reports to the caller if they provided a progress report object.
            wc.DownloadProgressChanged += (s, e) =>
            {
                // Do we have a progress report handler?
                if (progress != null)
                    // Yes, call it.
                    progress.Report(e.ProgressPercentage);
            };

            // Use a Lambda expression for the "completed" handler
            //  that writes the contents to a file.
            wc.OpenReadCompleted += (s, e) =>
                e.Result.CopyTo(sw.BaseStream);

            // Now make the call to download the file.
            wc.DownloadStringAsync(new Uri(strUrl));
        }
        finally
        {
            // Make sure the stream is cleaned up.
            sw.Flush();
            sw.Close();

            // Make sure the StreamWriter is diposed of.
            sw.Dispose();
        } // try/finally

        // CancellationTokenSource srcCancelToken = new CancellationTokenSource();
        // srcCancelToken.CancelAfter(TimeSpan.FromSeconds(iNumSecondsToWait));
    } // public static void URLToFile()