こんにちは人々私は私のftpダウンロードプログラムにダウンロード進行状況バーを入れようとしていますが、それはちょっと動作しますが、進行状況バーはダウンロードまたはダウンロード後にいっぱいになりませんが、コードは以下のダウンロード完了メッセージを受け取ります;
public void yap(object o)
{
(o as Label).Text = "DOWNLOADING";
}
private void button1_Click(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
ParameterizedThreadStart p = new ParameterizedThreadStart(yap);
Thread t = new Thread(p);
t.Start(label2);
BackgroundWorker _backgroundWorker = new BackgroundWorker();
_backgroundWorker.DoWork += _backgroundWorker_DoWork;
_backgroundWorker.WorkerReportsProgress = true;
_backgroundWorker.ProgressChanged +=
new ProgressChangedEventHandler(_backgroundWorker_ProgressChanged);
_backgroundWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);
_backgroundWorker.RunWorkerAsync();
}
void _backgroundWorker_RunWorkerCompleted
(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Download Completed");
}
void _backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Form1_Load(object sender, EventArgs e)
{
AutoSizeMode = AutoSizeMode.GrowAndShrink;
progressBar1.Maximum = 100;
}
void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Dispatcher.CurrentDispatcher.Invoke
(System.Windows.Threading.DispatcherPriority.Background,new Action(delegate()
{
string yol = Environment.CurrentDirectory;
FtpWebRequest FTP;
try
{
FileStream SR = new FileStream(yol + "\\list.gz", FileMode.Create);
FTP = (FtpWebRequest)FtpWebRequest.Create(new Uri
("ftp://" + textBox1.Text + "/" + "/usr/valapp/etc/list.gz"));
FTP.Credentials = new NetworkCredential("username", "password");
FTP.Method = WebRequestMethods.Ftp.DownloadFile;
FTP.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)FTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
SR.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
SR.Close();
response.Close();
MessageBox.Show("File Downloaded!");
for (int i = 0; i <= 100; i++)
{
backgroundWorker1.ReportProgress(i);
System.Threading.Thread.Sleep(20);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
}));
}
}
}
コードは複雑に見えるかもしれませんが、進行状況バーだけがいっぱいにならないと言ったので、完全に機能します。