gpt4 book ai didi

c# - BackgroundWorker OnProgressChanged 在 RunWorkerCompleted 被解雇后仍然被解雇

转载 作者:行者123 更新时间:2023-11-30 20:07:57 25 4
gpt4 key购买 nike

我的应用程序使用 BackgroundWorker 将文件上传到 FTP 服务器。一切正常,但 OnProgressChanged 事件似乎无法正常工作。

虽然 OnProgressChanged 将在 RunWorkerCompleted 事件触发后完全完成,但事实并非如此。

在我的例子中,虽然 RunWorkerComplete 已被触发,但 OnProgressChanged 事件仍在触发。显然,当我的文件已经完全发送到 ftp 服务器时,我的进度条仍在移动。

我在 Debug模式下进行了测试,我发现在 RunWorkerCompleted 触发后,OnPorgressChanged 仍在工作。

我的代码在这里。

 void FTP_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
try
{
string filename = e.Argument.ToString();
if (filename != string.Empty)
{
FileInfo fileInf = new FileInfo(filename);
FtpWebRequest reqFTP;
if (!IsFolderExist(_defaultDir))
{
MakeDefaultDir(_defaultDir);
}

reqFTP = GetRequest(this._host, this._port, GetDirName(_defaultDir) + "/" + fileInf.Name, this._user, this._pass);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;

long FileSize = fileInf.Length;
string FileSizeDescription = GetFileSize(FileSize);



int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
int BytesRead = 0;


using (Stream requestStream = reqFTP.GetRequestStream())
{

using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
BytesRead = fs.Read(Buffer, 0, ChunkSize); // read the first chunk in the buffer
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;

requestStream.Write(Buffer, 0, BytesRead);


SentBytes += BytesRead;

// Here is progress information
string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
}
}
catch (Exception ex)
{
if (OnFTPError != null)
{
OnFTPError(this, "Error was handled in Replaced File Uploading :" + ex.Message);
}
}
}

关于这个问题有什么想法吗?谢谢大家

最佳答案

这很可能是由 Vista 更新为 native 进度条组件引入的工件引起的,该组件也出现在 Windows 7 中。要查看它,请启动一个新的 Winforms 项目并在窗体上放置一个进度条和一个按钮。双击按钮并使 Click 事件处理程序如下所示:

    private void button1_Click(object sender, EventArgs e) {
if (progressBar1.Value == progressBar1.Maximum) progressBar1.Value = progressBar1.Minimum;
else progressBar1.Value = progressBar1.Maximum;
}

按 F5 并单击按钮。请注意条形图是如何动画的,它从 0 平滑地移动到 100。大约需要一秒钟。

也许你现在明白了,这个动画产生了滞后。换句话说,可见值总是小于编程值,除非你给它足够的时间来 catch 。你不需要,你会不断地使用 ProgressChanged 事件处理程序更新值。

不幸的是,他们忘记提供关闭此动画的选项。然而,有一个技巧,默认情况下动画是禁用的。您可以做的是将 Value 属性设置两次,首先是 value+1,然后是 value。条立即跳到编程值。唯一的缺陷是您无法轻松跳到 100%。

关于c# - BackgroundWorker OnProgressChanged 在 RunWorkerCompleted 被解雇后仍然被解雇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com