.net statusstrip进度条启动进度
我有一个解析函数,它解析一个文件并需要很长时间。 我想在解析文件的函数时显示进度条。 我添加了toolstripprogressbar 并设置了它的属性。 但是我不知道它们是如何同步运行的。
我想做这样的事情:
// progressBar start progress
parseFile();
// progressBar finish progress
我搜索了一下,for循环和线程用于此目的,但它们对我没有帮助。 我该怎么做?
I have a parsing function, It parse a file and take a long time.
I want to show a progress bar while the function parsing the file.
I add toolstripprogressbar and set its properties.
However I dont know how they are run synchronously.
I want to do something like this:
// progressBar start progress
parseFile();
// progressBar finish progress
I searched about it, for loop and thread are used for this, but they are not helpful for me.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确实需要在后台线程中运行长时间运行的操作,该线程可以定期向 GUI 线程报告其运行情况(并在此过程中更新进度条)。
我强烈建议您阅读有关后台工作人员的信息,这里 http://www.dotnetperls.com/backgroundworker
MSDN 页面:http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
您还可以在这里看到一个非常类似的问题:C# Winform ProgressBar 和BackgroundWorker
You really need to run the long running operation in a background thread, which can periodically report back to the GUI thread about how it's doing (and, in the process, update your progress bar).
I'd urge you to read about Background Worker, here http://www.dotnetperls.com/backgroundworker
MSDN Page here : http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
You can also see a very similar SO question here: C# Winform ProgressBar and BackgroundWorker
如果您想显示进度,那么您必须以某种方式将长流程划分为阶段或已处理的数据间隔 - 并在处理完每个阶段/间隔后增加进度条位置。
3 个阶段的示例:
将 ProgressBar 最大值设置为 3。
第 1 阶段)打开文件并将其读取到内存 - 完成此操作后将 ProgressBar.Value 设置为 1
第 2 阶段)解析文件 - 之后将 ProgressBar.Value 设置为 2
第 3 阶段)附加处理 ? - 之后将 ProgressBar.Value 设置为 3 = Max = 流程完成。
文件大小示例:
假设您可以测量到目前为止已解析的文件部分(以字节为单位)。然后首先将 ProgressBar.Max 设置为文件大小(以字节为单位)。然后,在文件中处理每个字节块之后,您应该将 ProgressBar.Value 设置为处理的字节数。
第二种方法通常更精确,进度条运行更流畅,但并不总是可以使用它。
If you want to show the progress then you have to somehow divide your long-process on either stages or processed data intervals - and increase progressbar position after eash stage/interval have been processed.
Example for 3 stages:
Set ProgressBar maximum value to 3.
Stage 1) Opening and reading file to memory - set progressBar.Value to 1 after finishing this
Stage 2) Parsing file - set progressBar.Value to 2 after that
Stage 3) Additional processing ? - after that set progressBar.Value to 3 = Max = process completed.
Example for file size:
Let's imagine you can measure what part of file you've parsed so far, in bytes. Then first you set progressBar.Max to file size in bytes. Then after every processed block of bytes from file you should set progressBar.Value to processed bytes count.
Second approach is usually more precise and progressBar runs much more smoothly, but it's not always possible to use it.