Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
Posted 7-Jul-12 16:11pm
Edited 7-Jul-12 17:58pm
v4
Comments
Sergey Alexandrovich Kryukov at 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 at 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.
Wes Aday at 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 at 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.
Wes Aday at 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.
DaveAuld at 8-Jul-12 1:45am
   
You shouldn't be using any forms directly from the background worker. Forms run in the primary GUI main thread. You should pass information back to the main thread using the reportprogress to display in the forms which will be owned by the parent.
JoThousand at 8-Jul-12 9:02am
   
OK. I will try this. Thanks.
JoThousand at 8-Jul-12 9:31am
   
DaveAuld, your solution is not working. Now none of the forms show up at all, although the freezing has stopped.
Naerling at 9-Jul-12 1:25am
   
I've posted a possible solution to your problem. Although you're making a best effort at explaining the problem I'm afraid the code doesn't really help. So I hope my answer correctly addresses the problem and provides a satisfactory solution.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
JoThousand at 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.
Naerling at 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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100