在没有任何用户输入的情况下触发表单事件? [英] Trigger a form event without any user input?

查看:65
本文介绍了在没有任何用户输入的情况下触发表单事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用GUI包装非交互式维护程序. GUI的目的是向用户提供有关维护活动状态的信息(它是否仍在运行?任务是否成功?等等).理想情况下,我希望它像这样工作:

1)显示的GUI仅包含标签(文本类似于正在运行.请稍候".)
2)执行维护代码.
3)根据维护代码的结果,GUI上的标签文本会更改(指示成功或失败).

我遇到的问题是,在离开表单构造函数上下文之前,GUI不会出现.如果我将维护代码直接放在构造函数中,或者从构造函数中以单独的方法调用维护代码,则在构造函数终止之前,GUI最终不会出现在显示器上.结果,直到维护代码完成,用户才完全看不到GUI.

因此,总而言之,我需要做的是能够在构造GUI之后触发事件,而无需用户单击任何控件.看起来这应该是相对简单的事情,但是我真的很沮丧.任何帮助将不胜感激.

Hello all,

I am trying to wrap a non-interactive maintenance program with a GUI. The purpose of the GUI is to provide the user with information about the status of the maintenance activity (Is it still running? Did the task succeed? Etc). Optimally, I''d like it to work like this:

1) The GUI appears containing only a label (text is something like "Running. Please stand by".)
2) The maintenance code is executed.
3) Based on the result of the maintenance code, the label text on the GUI changes (indicating success or failure).

The problem I have run into is that the GUI will not appear until I''ve left the form constructor context. If I place the maintenance code directly in the constructor or call it from the constructor in a separate method, the GUI will not finally appear on the display until the constructor has terminated. As a result, the user does not see the GUI at all until the maintenance code has finished.

So, to sum up, what I need to do is to be able to trigger an event after the GUI has been constructed without the user having to click on any controls. It seems like this should be relatively simple to do, but I am really stumped. Any help would be greatly appreciated.

推荐答案

您不应将此类活动放置在构造函数中.您破坏了表单应用程序的生命周期.
做一个工作者 [youtube示例 [ ^ ]).
让框架创建并显示表单,然后从启动工作程序加载 [ ^ ]事件.
由于您的表单和工作人员将是不同的线程,因此您不能简单地从工作人员修改表单,但这实际上并不复杂.请参阅本文: ProgressForm:链接到BackgroundWorker的简单表单 [ ^ ].实际上,您可以使用 ProgressChanged [ Control.invoke [
You should not place such activity in the constructor. You break the lifecycle of the forms application.
Make a worker[^] (youtube sample[^]) from the activity itself.
Let the framework create and display the form, and than start the worker from the Load[^] event.
As your form and your worker will be different threads, you can not simply modify the form from the worker, but it is really not complicated. See this article: ProgressForm: A simple form linked to a BackgroundWorker[^]. Actually you can use the ProgressChanged[^] event handler to update the Form directly, or use the Control.invoke[^] method to call a delegate.


您的意思是Application.DoEvents之类的东西?如果它是一个非常简单的应用程序,则应注意,DoEvents的影响可能是不可预测的.这是一些替代方案的讨论: Application.DoEvents替代方案 [ ^ ].
Do you mean something like Application.DoEvents? If it''s a very simple application you should be okay though note that the effects of DoEvents can be unpredictable. Here is a discussion with some alternatives: Application.DoEvents alternative[^].


这实际上并不像人们想象的那么容易.
Windows Forms开始UI处理,然后继续对该线程进行处理.
如果尝试在同一线程上调用UI函数(例如Paint),则需要等待该线程上的上一个过程完成.

尝试使用BeginInvoke()强制UI标签涂上状态信息.
BeginInvoke发生了什么事情? [ https://jamesrossiter.wordpress.com/2008/08 /22/updating-gui-from-other-threads-in-c/ [ http://kristofverbiest.blogspot.in/2007/02/avoid-invoke-prefer- begininvoke.html [ ^ ]
This is actually not as easy as one would think it is.
Windows Forms starts UI processing and then continues processing on that thread.
If you try and call a UI function, say Paint, on the same thread, you will need to wait until the previous process on that thread completes.

Try using BeginInvoke() to force the UI label to paint with the status information.
What''s up with BeginInvoke?[^] neatly explains the reasoning behing BeginInvoke().

Here are a couple of other links that should set you up.
https://jamesrossiter.wordpress.com/2008/08/22/updating-gui-from-other-threads-in-c/[^]
http://kristofverbiest.blogspot.in/2007/02/avoid-invoke-prefer-begininvoke.html[^]


这篇关于在没有任何用户输入的情况下触发表单事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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