任务.运行以上载和更新进度返回错误

本文关键字:返回 错误 更新 运行 上载 任务 | 更新日期: 2023-09-27 18:00:55

我有以下代码,可以将文件上传到服务器,并在栏中更新上传进度。

private void UploadButton_Click(object sender, EventArgs e)
{
    Cursor = Cursors.WaitCursor;
    try
    {
        // get some info about the input file
        System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileTextBox.Text);
        UploadDocument(fileInfo);
        // show start message
        LogText("Starting uploading " + fileInfo.Name);
        LogText("Size : " + fileInfo.Length);
    }
    catch (Exception ex)
    {
        LogText("Exception : " + ex.Message);
        if (ex.InnerException != null) LogText("Inner Exception : " + ex.InnerException.Message);
    }
    finally
    {
        Cursor = Cursors.Default;
    }
}
private async void UploadDocument(System.IO.FileInfo fileInfo)
{
    var someTask = await Task.Run<bool>(() =>
    {
        // open input stream
        using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read))
        {
            using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
            {
                uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;
                // start service client
                FileTransferWCF.FileTransferServiceClient client = new FileTransferWCF.FileTransferServiceClient();
                //FileTransferClient.FileTransferServiceClient client = new FileTransferClient.FileTransferServiceClient();
                // upload file
                client.UploadFile(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);
                LogText("Done!");
                // close service client
                client.Close();
            }
        }
        return true;
    });
}
void uploadStreamWithProgress_ProgressChanged(object sender, StreamWithProgress.ProgressChangedEventArgs e)
{
    if (e.Length != 0)
        progressBar1.Value = (int)(e.BytesRead * 100 / e.Length);
}

我得到错误:"跨线程操作无效:控件'progressBar1'是从创建它的线程以外的线程访问的。"行:

progressBar1.Value = (int)(e.BytesRead * 100 / e.Length);

也许我做错了。我是中任务库的新手。网

有线索吗?

任务.运行以上载和更新进度返回错误

我建议阅读我的async/await简介。async编程的指导原则之一是避免async void;也就是说,除非您正在编写事件处理程序,否则请使用async Task而不是async void

此外,一旦您开始使用async,请尝试在任何地方使用它。它确实简化了代码。

因此,您的代码可以这样更改(假设StreamWithProgress使用EAP约定(:

private async void UploadButton_Click(object sender, EventArgs e)
{
  UploadButton.Enabled = false;
  Cursor = Cursors.WaitCursor;
  try
  {
    // get some info about the input file
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileTextBox.Text);
    var task = UploadDocument(fileInfo);
    // show start message
    LogText("Starting uploading " + fileInfo.Name);
    LogText("Size : " + fileInfo.Length);
    await task;
    LogText("Done!");
  }
  catch (Exception ex)
  {
    LogText("Exception : " + ex.Message);
    if (ex.InnerException != null) LogText("Inner Exception : " + ex.InnerException.Message);
  }
  finally
  {
    Cursor = Cursors.Default;
    UploadButton.Enabled = true;
  }
}
private async Task UploadDocument(System.IO.FileInfo fileInfo)
{
  // open input stream
  using (System.IO.FileStream stream = new System.IO.FileStream(FileTextBox.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read, 4096, true))
  { 
    using (StreamWithProgress uploadStreamWithProgress = new StreamWithProgress(stream))
    {
      uploadStreamWithProgress.ProgressChanged += uploadStreamWithProgress_ProgressChanged;
      // start service client
      FileTransferWCF.FileTransferServiceClient client = new FileTransferWCF.FileTransferServiceClient();
      // upload file
      await client.UploadFileAsync(fileInfo.Name, fileInfo.Length, uploadStreamWithProgress);
      // close service client
      client.Close();
    }
  }
}
void uploadStreamWithProgress_ProgressChanged(object sender, StreamWithProgress.ProgressChangedEventArgs e)
{
  if (e.Length != 0)
    progressBar1.Value = (int)(e.BytesRead * 100 / e.Length);
}

您需要对UI线程进行UI更新。

progressBar1.Invoke(new Action(() => 
    { progressBar1.Value = (int)(e.BytesRead * 100 / e.Length); }));

或者(如果你不想阻止,直到方法返回(

progressBar1.BeginInvoke(new Action(() => 
    { progressBar1.Value = (int)(e.BytesRead * 100 / e.Length); }));

很抱歉语法错误,我现在无法访问VS。