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
apiI进度
和任务
的组合效果很好。@Damien\u不相信BackgroundWorker
类已被(通常)任务取代,但它们都不提供进度报告。如果需要进度报告,可以在Windows RT或IProgress中查找IAsyncOperationWithProgress