Update Control on Loading/Splash Screen in Separate Thread

Hi all,

I am working on an CFv2 app that needs to run a few processes before displaying the main form of the app. I want to have a loading screen with a progress bar and label that are updated to reflect where we are in the process of initializing the app. The loading screen is the first one the user sees, but FormMain is loading first and opens the loading screen in a separate thread while the main form continues processing.

I currently have the app loading the loading screen while the rest of the app initializes and then it unloads and displays my main form as it should. I based my code off of a webcast by Maarten Struys (Creating a Windows Mobile Line of Business Application - episode 6 of 13, User Interface) and it is working fine. The thing I need some help with is updating the UI of the loading screen, which is running in it's own thread.

Here is the flow of the app so far:

1) App starts, and I throw up a loading screen in it's own thread (now off of the main UI thread of main form)
2) App continues to run it's initialization processes in the background, both on the main UI thread (NOT the loading screen thread) and a couple of worker threads spawned by the main thread
3) App initialization finishes, the loading screen and thread are terminated and the main form is displayed

What is the best way to update a label on the loading screen, that is in it's own worker thread Really, the loading screen doesn't do anything except display status/progress messages and let the user know the app is loading, but it is running on a worker thread other than the main UI thread. I have looked at a few examples but am a little confused, as they all seem to discuss updating a UI control on the main UI thread that created the worker thread. One option I read is that I should create a new EventArgs class that inherits from EventArgs, and then have the splash screen subscribe to that event and have the main form and worker threads trigger that event, but I am a little lost as to how to do that.

Any suggestions

Thanks in advance!


Answer this question

Update Control on Loading/Splash Screen in Separate Thread

  • Greg J. Brown

    That makes sense. I do know how to use Control.Invoke, but I was trying to invoke a delegate on the main UI thread, from a worker thread, that was then trying to invoke a method on the splash screen (which was spawned by the main UI thread). This is my first time playing with the multi-threading, so I am still just learning the ins and outs.

    So, would the preferred way of doing the splash screen be to put it on the main UI thread as you suggest, and keep the other controls on the form invisible until the app finishes initializing, then hide the splash controls and show the other controls (a datagrid, etc.)

    Thanks for your help,

    LJ

  • cyberjoe2

    Hi

    Please tell me how created splash form in access

    Tank you


  • jwraith

    Sure, you can do that. Or, create a separate splash screen form and pop it up on top of the main form. Hide it as soon as you done.



  • Liu Feng

    Few rules with UI:

    1. You can only create/access forms on a main UI thread (the thread message pump is running) on, it will not work any other way.

    2. You can not do anything with the form from any thread but main UI thread. To access controls from another thread Control.Invoke() must be used (which marshals that access to UI thread).

    That means your #1 is not possible the way you envision it. However, it is possible to show splash screen on UI thread, then start number of worker threads which would update controls on forms using Control.Invoke(). You have to keep main UI thread unblocked or it won’t work.

    If you don’t know how to use Control.Invoke(), please look it up on MSDN or search this forum.



  • magcianaux

    Dir sir,
    I read your post on MSDN forum about WPF application with splash screen,
    Have you got a solution for this problem yet
    can you teach me how to do that
    I faced a problem with thread,
    I used the first form as the splash, while the other function loading data and update the status on the form,
    I used Notifychange to update the value, when I debug, I can see the value updated on the form, but the form deosnot show the correct value, just show the last value,
    Please help me, I'm new with XAM and threadf
    thanks in advance


  • Update Control on Loading/Splash Screen in Separate Thread