Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regaining focus after thread terminates

Status
Not open for further replies.

Charis

Programmer
Jul 27, 2000
11
AU
1. As soon as my application starts I display a splash screen on a different thread whilst the application sets up the environment for the user to login.

2. Now, suppose I have three open applications opened, with their respective icons showing on the task bar.

3. After I've finished the background work, I terminate the splash screen thread and display the logon screen.

4. The problem I am currently facing is that the focus is no longer on my application but on one of the open applications in the task bar.

5. The way to force the focus back to the application is by clicking on the login screen or by clicking the application icon in the task bar.

My question: how can I return the focus to my application after I've finished with the Splash screen thread, so the user can type his/her loging details without manually having to make the application active?

I have tried to set the IsBackground property of the splash screen thread to True. Unfortunately it didn't make any difference.

Thank you very much for any help.
 
Have you tried the Activate method?

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks Chiph.

Your suggestion works when I run the application from the IDE. However when I run it from the desktop after having installed, this is what happens: (Assume I have 2 apps in the task bar, ie 2 icons on the system tray bar)

Please note I use Windows XP.

1. Splash screen comes up.
2. Splash screen disappears (thread finished).
3. The Login form of my application appears on the task bar and flashes for a couple of seconds. That's when I would expect my login form to gain focus.
4. Now the first icon in the task bar (or tray icon bar) gets focus. If I start typing the user id, nothing happens. However if I click on the user id text box or click on the form or click on the tray icon for my application then I get focus.
There's got to be a way to automatically return focus to my starting app when the splash screen thread ends.
 
Try passing an instance of your main form to your splash screen form. When the splash screen is about to go away, call the .Activate method on the main form first.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi Chip,

I can't pass a reference of the main form to the worker thread (splash screen) because the main form isn't loaded at this stage. All the processing is done in Sub Main. However in the line of your suggestion I'll try creating a call-back procedure in the worker thread to notify the main app when it's about to finish.
I am also hearing things about using API calls such as SetWindowForeground to make an application active. It's all worth a try.

Thank you
 
You could also reverse the direction of things -- have your sub main pass a reference to the splash screen to the main form, and have the main form pass a reference to itself to the splash screen (you'd do this early in the main form's processing). That way when the splash goes away, it will have a reference to the main form.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
This is the approach that I took for my solution:

Close the Splash screen thread from within the Login form during the Activated event and call SetForegroundWindow API to get focus back to the main app.

Code below:

Private Sub frmLogin_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Activated

If Not _formActivated Then

If Not g_splashForm Is Nothing AndAlso Not g_splashForm.IsClosed Then
g_splashForm.Close()
g_splashForm = Nothing
End If

SetForegroundWindow(Me.Handle.ToInt32)

txtUserId.Focus()
_formActivated = True
End If

End Sub

Thank you for your help Chip.
 
I do this without using threading. I just show the splashscreen in the constructor before initializecomponent and close it in the on load form method but don't forget to do .refresh after the show.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Hi Christiaan,

The reason behing the threading is to minimize loading time. While the splash screen is shown some other processing is executed on the background "at the same time".
But thank you for your post. Sometimes we may be so engrossed on a complicated solution and overllok simple ways to achieve the same results!

Yes, there is such thing as a winnable war! Being a Christian, a follower of Jesus Christ, means you are always engaged in a winnable war!
 
Charis -
Be careful in using threading with WinForms. They are not usually thread-safe, as they want all interaction to come from the thread that created them.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top