提供的事件一次"执行其所有内部例程,而不是按我打算的步骤执行... 如何使例程一次可见?
The provided event executes all its internal routines "at once", not in steps as i intend... How to make the routines visible, one at a time ?
private void button1_Click(object sender, EventArgs e) { LabelProcessing.Visible = true; searchForFiles(); //while this is executing,(takes ~15seconds)the label does not appear. LabelProcessing.Visible = false; }推荐答案
尝试: private void button1_Click(object sender, EventArgs e) { label1.Visible = true; label1.Update(); // label1.Refresh() will work also string abcd; string efgh; // just some stuff to waste some time ... for (Int64 x = 0; x < Int64.MaxValue; x++) { abcd = "abcd" + x.ToString(); for (Int64 y = 0; y < Int64.MaxValue; y++) { efgh = abcd + "efgh" + y.ToString(); } } }
在标签上使用"Update"或"Refresh"方法,该视图立即可见:不使用"Update"或"Refresh"方法:直到填充"它才可见已经完成. 注释:按钮中花费较长时间的代码Click EventHandler可能应该在单独的线程中运行,以便UI可以保持响应状态:除非,当然,除非您希望它保持响应状态.在这种情况下:您可能不希望Label在长时间运行的代码完成之前消失":但这是否意味着您希望UI的其余所有部分保持不响应,甚至想要挂起末尾,用户是否可以通过TitleBar来移动表单? 要考虑的替代方法:(imho,弱)在执行长时间运行的代码以指示正在发生的事情时禁用Label;并且(imho,更好)是一个可见的进度条"或其他指示器,向用户反映您的长时间运行的代码中的情况正在烹饪". 可能需要重新设计代码.
With using the ''Update, or ''Refresh method on the Label, it is instantly visible: without the use of the ''Update or ''Refresh method: it does not become visible until the "stuff" has completed. Comment: the code that takes a long time in your button Click EventHandler should probably be run in a separate thread so the UI can stay responsive: unless, of course, you want it to to stay un-responsive. In this case: you may not want the Label to "disappear" until the long-running code is done: but does that mean you want all the rest of the UI to stay un-responsive, that you want to suspend even the end-user''s ability to move the Form around by the TitleBar ? Alternatives to consider: (imho, weak) disable the Label while long-running code is executing to indicate something is happening; and, (imho, much better) a visible "progress bar," or other indicator, that reflects to the user that things are "cooking" in your long-running code. That may require re-design of your code.
请参阅我对问题的评论.显然,您还没有真正了解面向偶数的UI的工作原理.不用担心... 您需要使用线程.您的15秒方法应在单独的线程中运行;并且用户界面将在此期间保持响应状态.在处理开始和完成时,应该通知UI隐藏和显示标签. 问题是:您不能从非UI线程调用与UI相关的任何东西.相反,您需要使用System.Windows.Threading.Dispatcher的Invoke或BeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms). 在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例: Control.Invoke()与Control.BeginInvoke() [ ^ ], Treeview扫描仪和MD5的问题 [ ^ ]. 另请参阅有关线程的更多参考资料: 如何获取keydown事件在vb中的不同线程上操作 [ ^ ], 启用禁用+多线程后控件事件不会触发 [ ^ ].
我假设从上下文中您希望标签在searchForFiles方法处理过程中显示,然后在完成时隐藏. 对于这种行为,您可能想尝试使用 DoEvents [ ^ ] I assume from the context you want the label to show while the searchForFiles method is processing then be hidden when it completes. For this behavior you may want to try using DoEvents[^] private void button1_Click(object sender, EventArgs e) { LabelProcessing.Visible = true; Application.DoEvents(); searchForFiles(); LabelProcessing.Visible = false; }
而不是等待按钮单击事件完成更新标签之前的处理,这将导致标签事件在该方法仍在执行时进行处理. 顺便说一句,正如您所指出的,编码是一种业余爱好,而不是一种职业,方法名称的例外标准是使用Camel大小写,例如SearchForFiles,而不是Pascal大小写,例如searchForFiles.
Rather than waiting for button click event to finish processing before updating the label this will cause the label events to process while the method is still executing. BTW, as you have indicated coding is a hobby not a profession, the excepted standard for method names is to use Camel case, such as SearchForFiles, rather than the Pascal case as you have, searchForFiles.
更多推荐
如何使例程一次可见?
发布评论