Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# 如何在长时间运行的方法中返回AsyncResult_C# - Fatal编程技术网

C# 如何在长时间运行的方法中返回AsyncResult

C# 如何在长时间运行的方法中返回AsyncResult,c#,C#,我有一个方法可以创建一个随机大小的文件,并希望输出文件创建进度的状态进度。以下是使用的方法: public IAsyncResult CreateRandomFile(string destPath, int bytesUsed) { byte[] dataArray = new byte[bytesUsed]; new Random().NextBytes(dataArray); using (FileStream d

我有一个方法可以创建一个随机大小的文件,并希望输出文件创建进度的状态进度。以下是使用的方法:

   public IAsyncResult CreateRandomFile(string destPath, int bytesUsed)
    {

        byte[] dataArray = new byte[bytesUsed];
        new Random().NextBytes(dataArray);


            using (FileStream destStream = new FileStream(destPath, FileMode.Create))
            {
                for (int i = 0; i < dataArray.Length; i++)
                {
                    destStream.WriteByte(dataArray[i]);


                    /// I am not sure how to return or use IAsyncResult such that
                    /// I will be able to access the status of how many bytes have 
                    /// been copied

                    IAsyncResult++;
                }
            }



        return ias;
    }
public IAsyncResult CreateRandomFile(使用字符串destPath、int字节)
{
字节[]数据数组=新字节[ByteUsed];
新的Random().NextBytes(dataArray);
使用(FileStream destStream=newfilestream(destPath,FileMode.Create))
{
for(int i=0;i

如何正确返回
IASyncResult

是否有理由在
任务
上使用
IASyncResult
IASyncResult
异步是一种非常古老的模式,已经被(通常)
任务
所取代,但它们都不提供进度报告。如果您需要进度报告,您可能会转而寻找一名
BackgroundWorker
@Damien\u非信徒不需要糟糕的
BackgroundWorker
api
I进度
任务
的组合效果很好。@Damien\u不相信
BackgroundWorker
类已被(通常)任务
取代,但它们都不提供进度报告。如果需要进度报告,可以在Windows RT或
IProgress中查找
IAsyncOperationWithProgress