- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序使用 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/
我的代码需要范围守卫,但是我是否必须手动 Dismiss() 从函数正常退出时所有范围守卫?即 void Deleter(MyClass* obj) { delete obj; } MyCla
当 UIPopoverController 被 UIPopoverController 本身的矩形之外的任何地方触摸解除时,是否有一个方法被调用? 状态: firstView 有一个 navBar。它
为调用实现 twilio,我尝试使用广播接收器而不是 twilio 逻辑的 Activity 。我的广播接收器应该通过 onReceive() 捕捉到 Intent ,但实际上并没有。听到了呼唤!但无
在我的应用程序中,我希望在多个 View 中显示 alertview。所以我所做的只是在实用程序类中编写了一个 alertview 并在任何地方使用它。这工作正常。 我什至尝试设置 但徒劳无功。 实用
我想使用具有以下行为/功能的 PopupWindow: 它是可聚焦的(在内部有交互式控件,例如按钮) 弹出窗口“下”的 View 必须正确使用弹出窗口外的触摸 .. 但弹出窗口必须保留在屏幕上,即使在
我正在为 tvOS 开发应用程序,但在取消 UIAlertController 时遇到问题。我在普通 UIViewController 之上呈现一个 UIAlertController。当我按下电视
我有一个 Controller A,还有一个 UIButton,单击它我将呈现一个新的 Controller B。但问题是 Controller B 首先嵌入了 NAV。所以最终我将介绍 UINavi
当我单击从 Web 服务打开所单击项目的数据的列表项时,我想打开一个 ProgressDialog。ProgressDialog 需要出现,直到被点击项目的 WebContent 被打开。 我知道使用
任何人都可以帮助我防止 alertview 在其按钮单击事件中被解雇吗?? 我有一个 textview 作为我的 alertView 的 subview ,如果 textview 的值为 nil,我不
这是按钮操作: @IBAction func readyPressed(sender: NSButton) { self.dismissController(Hardness)
我正在尝试防止当用户点击弹出窗口外部时弹出窗口被关闭。我似乎对此有其他问题/答案,他们似乎都建议使用 modalInPopover 作为 View 。正如我所看到的那样,我已经在 viewDidApp
我正在将 SFSafariViewController 推送到 UITabBarController 中。但单击 Safari 完成按钮会关闭 UITabBarController。如何预防? 最佳答
我有 2 个 viewcontroller,第一个 VC 包含 backButton。 firstVC.h @protocol DVDelegate -(void)DVViewControllerD
这是我的设置: 导航 Controller :ViewcontrollerA -> ViewControllerB ViewcontrollerB 显示一个 imagePickerController
我使用 ProgressDialog 向用户显示他必须等待,并在用户必须等待时使我的应用程序表面“不可触摸”。我向 progressDialog 添加了一个按钮,如果某些条件为真,它应该开始一些操作。
我目前正在尝试实现移动和导出模式的功能。我所做的就是让用户导航到他们想要将文件移动/导出到的目录,然后在他们选择的目录上调用 dismissGrantingAccessToURL,并附加原始文件的文件
我是一名优秀的程序员,十分优秀!