all groups > dotnet academic > january 2006 >
You're in the

dotnet academic

group:

Newbie Updating UI from separate thread


Newbie Updating UI from separate thread Shauno
1/30/2006 3:23:28 AM
dotnet academic:
Hi,
I am having problems updatingh the user interface in a multi-threaded
application.

BACKGROUND
My App has a user interface (the starting class containing main) and a
CPU class to handle many asynchronous comms routines.
The user interface will be kept updated on the status of the comms by the
CPU class.

My user interface creates an instance of a class called CPU and passes itself
in the constructor this way the CPU can update the user interface by calling
a method of UI

PROBLEM
The user interface is becoming unresponsvie.
The cursor turns to the hour glass and it doesnt repaint
Although all the comms threads run happily in the background as soon as
the CPU tries to call the UpdateScreen method the UI hangs.

ASSISTANCE REQUIRED
Could you please advise me on how i should go about resolving such a problem.
I may well be using bad coding strtegy so pleas advise as you see fit on any
aspect.

Thanks,
Shaun

PSEUDO CODE
// within the UI class
private CPU theCPU;
public UI()
{
theCPU = new CPU(this);
}

UpdateScreen(string status)
{
textBox1.Text = status;
panel1.Visible= ! panel1.Visible;
}

// within the CPU class
public CPU (UI userInterface)
{
this.userInterface = userInterface;
// start threads to run async comms
}

private void ReceivedStatusChange()
{
userInterface.UpdateScreen("Some new status info");
}
Re: Newbie Updating UI from separate thread Mattias Sjögren
1/30/2006 8:57:36 PM
[quoted text, click to view]

You should onlyd do so from the thread the UI elements are created.
You can use Control.Invoke from a background thread to make the
updating code run on the UI thread.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button