Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB
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.
Posted
Updated 7-Jul-12 16:58pm
v4
Comments
Sergey Alexandrovich Kryukov 7-Jul-12 21:27pm    
The problem here is that your "application freezes until..." make no sense. There is not such strict concept as "freeze". Of course, if the form is not ready do be shown, it is not shown. What is "open form in the background"? There is no such thing. There is no even such thing as "form open". Where is it? You create it first, then show. Whatever you do, the form is shown in the UI thread only, and this is never a background thread.

Understand better what exactly you want to achieve and how do you want to spend the time why something is prepared in the background and then you will know what to do.
--SA
JoThousand 7-Jul-12 21:36pm    
What I mean by the form.show event is creating the form and displaying to the user (so for my example IN THE CODE it's 'cal.show' or 'prefs.show'). The application hangs even though I'm using the BackgroundWorker.

I'm pretty sure my question makes plenty of sense... I even provided the proper code.
[no name] 7-Jul-12 22:21pm    
Well I will step on this land mine....

"form.show event is creating the form".. no it does not. The Show() event does exactly that. It does not create the form to show, it shows an already created form.

What is not clear is why you think that there is a connection between your application hanging and the BackgroundWorker.

"my question makes plenty of sense... I even provided the proper code"... no... you provided the code for the BackgroundWorker completed event. There is nothing in this snippet that would lead anyone to believe that your application is hanging up anywhere. Your "question" might make perfect sense to you but to those of us that cannot see your code, run your project in a debugger or read your mind, your narrative does not make too much sense. For example you say that you use "AppQueue to tell the BackgroundWorker what to do" when in your code clearly you do not. AppQueue is being used in the BackgroundWorker completed event. How can it tell your BackgroundWorker what to do when it's done working?
JoThousand 7-Jul-12 22:57pm    
I apologize, I should have explained that a bit better.

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 itself. I'll rewrite my question.

Also, the Show() event is what I meant to write, and I do understand that it shows an already created form.

Again, so sorry. I honestly didn't read what I wrote to make sure it made sense. I normally reread what I write many times before posting so I don't know why I didn't this time.
[no name] 7-Jul-12 23:02pm    
Not a problem. I think that written communication is fine for novels but for things like this it is a poor medium. Too much room for misunderstanding.

1 solution

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.
 
Share this answer
 
v2
Comments
JoThousand 9-Jul-12 17:31pm    
Thanks very much for your help :)
This actually was a much simpler solution than what I was originally going to do.
Sander Rossel 9-Jul-12 17:38pm    
Glad I could be of help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900