2017-09-01 12 views
1

私は自分のuploadProgressプログレスバーのツールヒントに現在のアップロードされた量を書き込むようにしているので、ユーザーがマウスオーバーして進行状況バーを表示すると、ファイルサイズ。 私が上にマウスを置いて、ファイルがアップロードされるまで、私はこれまで私に "ビジー"アイコンを与え、ダウンロードされた量とファイルサイズを表示するコードです。C#FtpWebRequestプログレスバーアップロードされた量のツールチップ更新

誰かが私を助けてくれますか?

private void uploadFile() 
{ 
    try 
    { 
     richTextBox1.AppendText("\n\nStarting file upload"); 
     FtpWebRequest request = 
      (FtpWebRequest)WebRequest.Create("ftp://ftpsite.com/public_html/test.htm"); 

     request.Credentials = new NetworkCredential("username", "password"); 

     request.UsePassive = true; 
     request.UseBinary = true; 
     request.KeepAlive = true; 

     request.Method = WebRequestMethods.Ftp.UploadFile; 

     using (Stream fileStream = File.OpenRead(@"C:\path\testfile.UPLOAD")) 
     using (Stream ftpStream = request.GetRequestStream()) 
     { 
      uploadProgress.Invoke(
       (MethodInvoker)delegate { 
        uploadProgress.Maximum = (int)fileStream.Length; }); 

      byte[] buffer = new byte[10240]; 
      int read; 
      while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0) 
      { 
       ftpStream.Write(buffer, 0, read); 
       uploadProgress.Invoke(
        (MethodInvoker)delegate { 
         uploadProgress.Value = (int)fileStream.Position; 

         toolTip1.SetToolTip(
          uploadProgress, string.Format("{0} MB's/{1} MB's\n", 
          (uploadProgress.Value/1024d/1024d).ToString("0.00"), 
          (fileStream.Length/1024d/1024d).ToString("0.00"))); 
        });  
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

+0

私たちに 'uploadFile'をどのように呼び出すかを教えてください。 –

答えて

1

あなたのコードは、私の作品のおかげ。参照は(すでにそのリンクを知っているが)もHow can we show progress bar for upload with FtpWebRequest

private void button1_Click(object sender, EventArgs e) 
{ 
    Task.Run(() => uploadFile()); 
} 

enter image description here


をあなたはあまりにもツールチップを更新しますように、あなたがバックグラウンドスレッドでuploadFileを実行すると仮定すると、しばしば、それはちらつきます。

+0

ありがとうMartin、それは 'task.Run'行を試して、それは動作します...しかし、私は持っているrichtextboxへの書き込みを削除しなければなりませんでした。作成されたスレッド以外のスレッドからアクセスする 'richtextbox1'を制御します。とにかくこれを解決するにはありますか? –

+0

'uploadProgress'と' toolTip1'にアクセスするのと同じ方法です。 'Control.Invoke'を使います。 –

+0

心配しないで、https://stackoverflow.com/questions/8738767/backgroundworker-cross-thread-operation-not-validは私の問題を解決します。再度、感謝します –

関連する問題