Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / October 2004

Tip: Looking for answers? Try searching our database.

Updating the UI from a Secondary Thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BG - 25 Oct 2004 19:06 GMT
We're having trouble writing the code to update a UI control (label.Text)
from a secondary thread.  We're using C# with Windows Forms.

We have a main form named MainForm, a splash screen form named SplashScreen,
and a C# class library named BackgroundProcess.

On application start, we simply want to show the splash screen form, kick
off the background processing on a separate thread, and, at different points
in the background processing, update a label on the splash screen to inform
the user of what the application is doing.  When the processing is done, the
splash screen form goes away and the main form appears.

In the main form's form load event we have:

private void MainForm_Load(object sender, System.EventArgs e)

{

     // Create and show the splash screen

     SplashScreen frmSS = new SplashScreen();

     frmSS.Show();

     // Create the object for background processing and set an initial
property

     BackgroundProcess bp = new BackgroundProcess();

     bp.StartingDirectory = @"C:\";

     // Create a new thread for the background processing and start it

     System.Threading.Thread activeThread = new System.Threading.Thread(new
System.Threading.ThreadStart(bp.StartProcess));

     activeThread.Start();

     while (activeThread.IsAlive)

     {

           Application.DoEvents();

     }

     // Clean up

     activeThread = null;

     bp = null;

     frmSS.Close();

     frmSS = null;

}

This code works, and the code in our class library (i.e., the background
processing code) works, but we still can't get the splash screen's label to
update.

We've trying using delegates/events, etc. and have not been successful in
getting the label updated from the secondary thread that's doing all the
"behind-the-scenes" processing.

Any suggestions?

Thanks.
Jon Skeet [C# MVP] - 25 Oct 2004 19:12 GMT
> We're having trouble writing the code to update a UI control (label.Text)
> from a secondary thread.  We're using C# with Windows Forms.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

I would suggest not creating the main form (or at least not running it)
until the background thread has finished. Instead, call Application.Run
(new SplashScreen()). The background task should invoke a delegate to
close the splash screen, at which point Application.Run will return,
and you can show your main form.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Herfried K. Wagner [MVP] - 25 Oct 2004 19:29 GMT
"BG" <bgnospam@bgnospam.com> schrieb:
> We're having trouble writing the code to update a UI control (label.Text)
> from a secondary thread.  We're using C# with Windows Forms.

Always show your forms in the app's main UI thread and use 'Control.Invoke'
to communicate in the tread -> UI direction:

Multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.a
sp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.a
sp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.a
sp>

<URL:http://www.devx.com/dotnet/Article/11358/>

<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFo
rmsControlClassInvokeTopic.asp>

Multithreading in Visual Basic .NET (Visual Basic Language Concepts)
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisu
albasic.asp>

Sample:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/downloads/FileSystemEn
umerator.zip>

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/

G Namx - 26 Oct 2004 17:53 GMT
Hi,

Timely post as was hitting the same sort of wall.

Thanks for the links.

A lazy fyi to anyone trying to implement this. Make sure you don't have any
DoEvents calls in your code!

> "BG" <bgnospam@bgnospam.com> schrieb:
> > We're having trouble writing the code to update a UI control (label.Text)
[quoted text clipped - 4 lines]
>
> Multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.a
> sp>

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.a
> sp>

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.a
> sp>
>
> <URL:http://www.devx.com/dotnet/Article/11358/>

<URL:http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWindowsFo
> rmsControlClassInvokeTopic.asp>
>
> Multithreading in Visual Basic .NET (Visual Basic Language Concepts)

<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisu
> albasic.asp>
>
> Sample:

<URL:http://dotnet.mvps.org/dotnet/samples/filesystem/downloads/FileSystemEn
> umerator.zip>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.