使用BackgroundWorker显示表单 [英] Show Forms using BackgroundWorker

查看:113
本文介绍了使用BackgroundWorker显示表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



可以这么说,我的程序有"applets",所以我使用了一个名为AppQueue的字符串来告诉BackgroundWorker该怎么做.

我正在使用BackgroundWorker控件在后台打开新表单(使主应用程序表单保持响应状态),因为其中一些加载时间很长.

我在RunWorkerCompleted事件中调用 FormName. Show()(其中 FormName 是表单的名称),但是整个应用程序冻结,直到表单显示为止.如果我使用DoWork事件,则我要打开的表单将冻结并且无法使用,而应用程序的其余部分将继续响应.我读了一个说使用RunWorkerCompleted事件的教程,但是它无法正常工作,而是导致整个应用程序冻结,直到加载AppQueue字符串中的表单为止.

代码如下:

Hi,

My program has ''applets'', so to speak, so I use a string called AppQueue to tell the BackgroundWorker what to do.

I''m using the BackgroundWorker control to open new forms in the background (keeping the main application form responsive) as some of them take a long time to load.

I call FormName.Show() (where FormName is the form''s name) in the RunWorkerCompleted event, but the entire application freezes until the forms show. If I use the DoWork event, the form that I want to open freezes and cannot be used, while the rest of the application continues to respond. I read a tutorial that said to use the RunWorkerCompleted event, but it''s not working and instead causes the entire application to freeze until the form that''s in the AppQueue string loads.

Here''s the code:

Private Sub BkgApps_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BkgApps.RunWorkerCompleted
        
If AppQueue = "Prefs" Then
            Prefs.Show()
            Prefs.Focus()
            Prefs.BringToFront()
            Prefs.WindowState = FormWindowState.Normal

ElseIf AppQueue = "Calendar" Then
            cal.Show()
            cal.Focus()
            cal.BringToFront()
            cal.WindowState = FormWindowState.Normal

End If



如果有什么我可以改善的问题,请告诉我.



If there''s anything I can do to improve the question please let me know.

Thanks.

推荐答案

表格 [ ^ ] 加载 [已显示 [事件 [
也许您可以将某些长时间运行的操作从Form中取出,并带入另一个在工作线程上工作的Class中,并将其结果传达给您的Form.

希望有帮助.
The BackgroundWorker.RunWorkerCompleted[^] runs on the main thread. If you''re opening up your Form[^] on the main thread and the Load[^] or Shown[^] Event[^] takes up some time to complete then your application is waiting for the Load or Shown Event to do its work.
Opening your Form on a worker thread is not an option, as you''ve already noticed.

When you say your ''applets'' are taking a long time to load, perhaps then that''s the problem you should be looking at. Unless you make multiple calls to a database, fetching thousands of records, making difficult calculations, I would say loading an ''applet'' shouldn''t take that long...

Perhaps you could take some of that long-running operation out of the Form and into another Class that works on a worker thread and communicates its result to your Form.

Hope that helps.


这篇关于使用BackgroundWorker显示表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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