是否有一个状态栏可以用代码及时更新? [英] Is there a status bar that updates RESPONSIVELY with the code?

查看:76
本文介绍了是否有一个状态栏可以用代码及时更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在寻找要添加到表单中的组件,该组件将用作状态指示器以显示我在代码处理过程中所走的距离.
我有一个使用过的代码

Hi guys, I''m looking for a component to add to my form that''ll serve as a status indicator to show how far i am in the process of the code.
I have a code that used

StatusBarMainMenu.Panels(1).Text = "Reading serial number.." & PdSN
StatusBarMainMenu.Panels(1).Text= " Reading something else.." & strSthElse
StatusBarMainMenu.Panels(1).Text = "Sending unlock command.."


...在VB6中.表单底部的状态栏也可以刷新.
(通过刷新,我的意思是它会随时更改文本,具体取决于我将这些行放在代码中的位置)
我将这些代码升级到.NET时出现问题.例如,.net不再具有StatusBar组件.上面的代码成为


...in VB6. And the status bar at the bottom of the form refreshes just fine.
(by refresh i mean it changes the Text whenever it''s supposed to, depending on where i put these lines in the code)
My problem arises when i upgrade these code into .NET. For one, .net doesn''t have StatusBar component anymore. The code above become

StatusBarMainMenu.Items.Item(0).Text = ...


其中StatusBarMainMenu为"StatusStrip".没什么大不了的.但是在运行时,状态栏刷新不足.它跳过了一些本应显示的文本更改.我尝试使用:


with StatusBarMainMenu being a "StatusStrip" . That''s no biggy. but during runtime the status bar doesn''t refresh enough. It skipped some of the text changes that''re supposed to show. I tried to use:

ToolStripStatusLabel1.Text = ..

,但没有帮助.

我认为原因可能是我使用的任何状态组件?或者当代码进行一些计算或在循环内时,表单有时也会冻结.在这种情况下,将执行一堆DoEvents().有任何建议吗?

but it didn''t help.

I think the reason could be whatever status component i use? or it could be that the form is also occasionally freezing when the code''s doing some calculation or inside a loop. in that case a bunch of DoEvents() will do. Any suggestions?

推荐答案

它的响应速度非常快,但是您可以通过某种方式解决它.解决问题的一种方法是使用DoEvents.要提出修复建议,您需要更多有关代码的详细信息.请了解,由于假期的原因,我对您的硬盘驱动器的访问受到一定限制. :-)

对于循环内的计算",应始终在将UI方法调用到UI线程的过程中使用线程.调用DoEvents不能代替它,通常很危险.在极少数情况下,可以使用DoEvents修改仅几个事件的事件处理顺序,一个事件取决于另一个事件,并且在某些类似情况下要格外小心使用.如果您不完全了解它的作用,则可能使情况变得更糟.

对于将UI与非UI线程一起使用,请参见我过去的回答:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
It is perfectly responsive, but you could screw up it somehow. One of the way to screw up it is using DoEvents. To suggest a fix, more detail on your code is needed. Please understand that, due to the vacation season, my access to your hard drive is somewhat limited. :-)

For "calculations inside a loop", you should always use threading with invocation of the UI methods to the UI thread. The call to DoEvents cannot replace it and is generally dangerous. There are very limited number of cases when you can use DoEvents to modify the order of event handling for just a couple of events, one depending on another, and some similar cases, to be used with great care. If you do not perfectly understand what it does, you can make the situation much worse.

For using UI with the non-UI thread, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于是否有一个状态栏可以用代码及时更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆