VB.Net:了解 Application.Run() 的工作方式
Hans Passant 在此处给了我一个很好的答案,因此我想询问更多详细信息,以尝试了解应用程序的方式。 Run() 有效。
据我从文档中了解到,似乎 Application.Run()
在当前线程上启动一个消息循环,这反过来又使其能够处理用户输入(对吗?)。重载版本 Application.Run(Form)
的作用基本相同,只是在表单关闭时存在,并且默认显示表单。
这就提出了几个问题:
Main()
子函数调用一个可以与用户通信(消息框等)并等待其退出的函数?ShowDialog
可以工作,除非您不希望表单在启动时立即显示(例如,如果您有一个最小化到系统托盘启动的 for)- 基本上,情况如下:子 `Main` 有一个要在 20mn 内执行的任务列表,并有一个系统托盘图标告诉用户该程序将在 20mn 内运行。计时器在 20 分钟后计时,并且必须执行大约 20 分钟。 15个任务一一完成,每次都会创建一个进度对话框的实例,最初隐藏在任务栏中。
- `ShowDialog` 会显示表单,这是不需要的;所以我的方法是将进度对话框传递给启动下一个任务的函数的回调。但这不会在第二个进度表单退出之前退出第一个进度表单,不是吗?这意味着最终将打开 15 个表单...
- 因此解决方案可能是调用(begininvoke?)主应用程序循环上的回调...只是,我不知道该怎么做,因为我不知道有一个与循环关联的表单来调用回调...
我希望我的问题很清楚(我可能会混淆很多事情,抱歉),
谢谢,
金融理财师。
Hans Passant gave me a great answer here, so I thought of asking for more details to try to understand the way Application.Run()
works.
As far as I understand from the docs, it seems that Application.Run()
starts a message loop on the current thread, which in turns enables it to process user input (Is that right?). The overloaded version Application.Run(Form)
basically does the same, only it exists when the form closes, and it shows the form by default.
That raises a few questions:
Main()
sub a function that can communicate with the user to (message boxes and so on) and wait for it to exit?ShowDialog
could work, unless you don't want the form to display immediately when launched (eg. if you have a for that's launched minimized to the system tray)- Basically, the situation would be as follows: sub `Main` has a list of tasks to execute in 20mn, with a system tray icon telling the user that the program will operate in 20mn. A timer ticks after 20mns, and has to execute say approx. 15 tasks one by one, every time creating an instance of a progress dialog, initially hidden in the taskbar.
- `ShowDialog` would display the form, which is not wanted; so the way I would do it would be to pass the progress dialog a callback to a function that starts the next task. But that wouldn't exit the first progress form before the second has exited, would it? Which means 15 forms would end up being opened...
- So the solution may be to invoke (begininvoke?) the callback on the main application loop... Only, I don't know how to do this, because I don't have a form associated with the loop to invoke the callback on...
I hope my questions are clear (I might confuse many things, sorry),
Thanks,
CFP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 Timer、ProgressBar 和 BackgroundWorker 放到表单上。您要做的第一件事是防止程序启动时窗体可见。将此代码粘贴到表单类中:
使用计时器开始作业。设置其 Interval 和 Enabled 属性,添加 Tick 事件处理程序:
这使得表单在作业启动并启动后台工作程序时可见。将 BGW 的 WorkerReportsProgress 属性设置为 True 并添加 3 个事件处理程序:
由您来填写 DoWork 事件处理程序的代码。让它完成这 15 项工作,请务必调用 BackgroundWorker1.ReportProgess,以便更新进度条。这就是 ProgressChanged 事件处理程序的作用。 RunWorkerCompleted 事件处理程序再次隐藏表单。
您可以在 NotifyIcon 的上下文菜单项事件中调用 Show() 方法,以便用户可以使您的表单再次可见。在上下文菜单项中调用 Application.Exit() ,允许用户退出您的应用程序。确保在 BGW 运行时禁用它。或者实施一种彻底停止工作的方法。
Drop a Timer, ProgressBar and a BackgroundWorker on the form. First thing you'll want to do is to prevent the form from getting visible when the program is started. Paste this code into the form class:
Use the timer to get the job started. Set its Interval and Enabled properties, add the Tick event handler:
That makes the form visible when the job is started and starts the background worker. Set the BGW's WorkerReportsProgress property to True and add the 3 event handlers:
It is up to you to fill in the code for the DoWork event handler. Have it do those 15 jobs, be sure to call BackgroundWorker1.ReportProgess so that the progress bar gets updated. Which is what the ProgressChanged event handler does. The RunWorkerCompleted event handler hides the form again.
You can call the Show() method in the context menu item event for the NotifyIcon so that the user can make your form visible again. Call Application.Exit() in the context menu item that allow the user to quit your app. Make sure you disable that when the BGW is running. Or implement a way to cleanly stop the job.