Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.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
C# IAsyncResult实例上的FromAsync方法:";不包含对';fromsync'&引用;_C#_.net_Asynchronous_Download_.net Framework Version - Fatal编程技术网

C# IAsyncResult实例上的FromAsync方法:";不包含对';fromsync'&引用;

C# IAsyncResult实例上的FromAsync方法:";不包含对';fromsync'&引用;,c#,.net,asynchronous,download,.net-framework-version,C#,.net,Asynchronous,Download,.net Framework Version,我需要使用一个类,它允许我在标题上使用“Range”,同时更新progressbar。 我看到一篇帖子,有人发布了以下代码,这应该有助于HttpWebRequest实现progressbar: public static void CopyToStreamAsync(this Stream source, Stream destination, Action<Stream, Stream, Exception> completed,

我需要使用一个类,它允许我在标题上使用“Range”,同时更新progressbar。 我看到一篇帖子,有人发布了以下代码,这应该有助于HttpWebRequest实现progressbar:

public static void CopyToStreamAsync(this Stream source, Stream destination, Action<Stream, Stream, Exception> completed, 
                                         Action<uint> progress, uint bufferSize, uint? maximumDownloadSize, TimeSpan? timeout)
    {
        byte[] buffer = new byte[bufferSize];

        Action<Exception> done = exception =>
        {
            if (completed != null)
            {
                completed(source, destination, exception);
            }
        };

        int maxDownloadSize = maximumDownloadSize.HasValue ? (int)maximumDownloadSize.Value : int.MaxValue;
        int bytesDownloaded = 0;
        IAsyncResult asyncResult = source.BeginRead(buffer, 0, new[] { maxDownloadSize, buffer.Length }.Min(), null, null);
        Action<IAsyncResult, bool> endRead = null;
        endRead = (innerAsyncResult, innerIsTimedOut) =>
        {
            try
            {
                int bytesRead = source.EndRead(innerAsyncResult);
                if (innerIsTimedOut) done(new TimeoutException());

                int bytesToWrite = new[] { maxDownloadSize - bytesDownloaded, buffer.Length, bytesRead }.Min();
                destination.Write(buffer, 0, bytesToWrite);
                bytesDownloaded += bytesToWrite;

                if (progress != null && bytesToWrite > 0) progress((uint)bytesDownloaded);

                if (bytesToWrite == bytesRead && bytesToWrite > 0)
                {
                    asyncResult = source.BeginRead(buffer, 0, new[] { maxDownloadSize, buffer.Length }.Min(), null, null);
                    asyncResult.FromAsync((ia, isTimeout) => endRead(ia, isTimeout), timeout);

                }
                else
                {
                    done(null);
                }
            }
            catch (Exception exc)
            {
                done(exc);
            }
        };

        asyncResult.FromAsync((ia, isTimeout) => endRead(ia, isTimeout), timeout);
    }
public static void CopyToStreamAsync(此流源、流目标、操作已完成、,
操作进度、uint缓冲区大小、uint最大下载大小、TimeSpan超时)
{
字节[]缓冲区=新字节[bufferSize];
操作完成=异常=>
{
如果(已完成!=null)
{
已完成(来源、目的地、异常);
}
};
int maxDownloadSize=maximumDownloadSize.HasValue?(int)maximumDownloadSize.Value:int.MaxValue;
int字节=0;
IAsyncResult asyncResult=source.BeginRead(buffer,0,new[]{maxDownloadSize,buffer.Length}.Min(),null,null);
Action endRead=null;
endRead=(innerAsyncResult,InnerIstimeOut)=>
{
尝试
{
int bytesRead=source.EndRead(innerAsyncResult);
如果(InnerIstimeOut)完成(new TimeoutException());
int bytesToWrite=new[]{maxDownloadSize-bytesloadded,buffer.Length,bytesRead}.Min();
写入(缓冲区,0,bytesToWrite);
bytesDownloaded+=bytesToWrite;
如果(progress!=null&&bytesToWrite>0)progress((uint)bytes下载);
if(bytesToWrite==bytesRead&&bytesToWrite>0)
{
asyncResult=source.BeginRead(缓冲区,0,新[]{maxDownloadSize,buffer.Length}.Min(),null,null);
asyncResult.FromAsync((ia,isTimeout)=>endRead(ia,isTimeout),超时);
}
其他的
{
完成(空);
}
}
捕获(异常exc)
{
完成(exc);
}
};
asyncResult.FromAsync((ia,isTimeout)=>endRead(ia,isTimeout),超时);
}
问题是我甚至无法测试它,因为“asyncResult.FromAsync”返回“不包含'FromAsync'的定义,并且没有扩展方法'FromAsync'”错误

有人能帮我理解我应该使用哪些引用或.NET框架才能使代码正常工作吗


谢谢

如果您仔细查看,您会发现该接口上没有FromAsync方法。也许它是一个自定义扩展,隐藏了?也许。事实上,我试图验证它,但超时的使用让我困惑。