473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Thread-Safe GUI-programming

Hey there,

im quite new to C# GUI-Framework and got a question now. In Java its no
problem todo cross-over Thread-calls. So i can change something in a Window
from another thread. It seems C# lacks of this ability.

I got a normal GUI and want to start a thread where this gui is also changes
from. I made this working before with delegates. Ok so far...this would be a
way to solve the issue. But it looks like much work if i have todo this for
every single method i want to use over different threads.

No heres my question now...could you tell me whats the best practice in this
case ? How do you implement that ?
Thanks and regards,
oliver
Oct 2 '07 #1
8 6486
On Oct 2, 9:56 am, Oliver <Oli...@discussions.microsoft.comwrote:
im quite new to C# GUI-Framework and got a question now. In Java its no
problem todo cross-over Thread-calls. So i can change something in a Window
from another thread.
No, you can't (at least not in Swing/AWT). It may work in some cases,
but it's not guaranteed to and you shouldn't do it. That's the whole
point of SwingWorker. From the docs for SwingWorker:

<quote>
Swing components should be accessed on the Event Dispatch Thread only.
</quote>
It seems C# lacks of this ability.
Just to be clear, it's not a language issue. It's a library issue -
and the Java libraries (well, Swing/AWT at least) have exactly the
same issue. Most UI libraries take exactly the same approach, as far
as I'm aware.
I got a normal GUI and want to start a thread where this gui is also changes
from. I made this working before with delegates. Ok so far...this would be a
way to solve the issue. But it looks like much work if i have todo this for
every single method i want to use over different threads.

No heres my question now...could you tell me whats the best practice in this
case ? How do you implement that ?
Invoke/BeginInvoke are indeed the way to go. Try to minimise the
number of "entry points" to the UI,
and make each of those thread-safe.

Jon

Oct 2 '07 #2
Oliver <Ol****@discussions.microsoft.comwrites:

[...]
I got a normal GUI and want to start a thread where this gui is also changes
from. I made this working before with delegates. Ok so far...this would be a
way to solve the issue. But it looks like much work if i have todo this for
every single method i want to use over different threads.

No heres my question now...could you tell me whats the best practice in this
case ? How do you implement that ?
Oliver -

I've recently started using C#'s anonymous methods for this:

public void ThreadUpdateStatus(string s)
{
try
{
this.Invoke(new EventHandler(delegate(object sender2, EventArgs e2)
{
lblStatus.Text = s;
}));
}
catch (ObjectDisposedException)
{
// Do nothing
}
}

I like this approach because it avoids creating a separate method for
every action you want to take from a thread, and keeps the code all in
one place, which makes it easier to quickly tell what's going on.

Good luck!

----Scott.
Oct 2 '07 #3
On Oct 2, 4:56 am, Oliver <Oli...@discussions.microsoft.comwrote:
Hey there,

im quite new to C# GUI-Framework and got a question now. In Java its no
problem todo cross-over Thread-calls. So i can change something in a Window
from another thread. It seems C# lacks of this ability.

I got a normal GUI and want to start a thread where this gui is also changes
from. I made this working before with delegates. Ok so far...this would be a
way to solve the issue. But it looks like much work if i have todo this for
every single method i want to use over different threads.

No heres my question now...could you tell me whats the best practice in this
case ? How do you implement that ?

Thanks and regards,
oliver
For the .net 1.1 framework (e.g., no anonymous methods) I've been
using the following pattern:

void ChangeWidget()
{
if ( InvokeRequired )
Invoke(new MethodInvoker(ChangeWidget))
else
// ... manipulate UI
}

To pass parameters you'll need a matching delegate (and the Invoke
overload that accepts an object[]).

Oct 2 '07 #4
Hey Scott,
I've recently started using C#'s anonymous methods for this:

public void ThreadUpdateStatus(string s)
{
try
{
this.Invoke(new EventHandler(delegate(object sender2,
EventArgs e2)
{
lblStatus.Text = s;
}));
}
catch (ObjectDisposedException)
{
// Do nothing
}
}

I like this approach because it avoids creating a separate method for
every action you want to take from a thread, and keeps the code all in
one place, which makes it easier to quickly tell what's going on.

yeah that looks like a cleaner solution. But the main-problem still exists.
I have to write new methods or override existing ones of methods i'd like to
use.

Well but i see now its not just me doing it wrong or something so i can live
with that.
thanks guys!
oliver
Oct 2 '07 #5
Hey Jon,
No, you can't (at least not in Swing/AWT). It may work in some cases,
but it's not guaranteed to and you shouldn't do it. That's the whole
point of SwingWorker. From the docs for SwingWorker:

<quote>
Swing components should be accessed on the Event Dispatch Thread only.
</quote>
yeah true. And i more meant that i dont need such thing like a
delegate/invoke stub which i have to write on my own. It can become much
work especially if you wrote your application non-thread-aware and got to
bring thread-awareness into it on a later time :/

Well but you answered my main-question which referred to the way of dealing
itself.
So thanks jon and regards,
oliver
Oct 2 '07 #6
Scott Gifford wrote:
Oliver <Ol****@discussions.microsoft.comwrites:

[...]
>I got a normal GUI and want to start a thread where this gui is also changes
from. I made this working before with delegates. Ok so far...this would be a
way to solve the issue. But it looks like much work if i have todo this for
every single method i want to use over different threads.

No heres my question now...could you tell me whats the best practice in this
case ? How do you implement that ?

Oliver -

I've recently started using C#'s anonymous methods for this:

public void ThreadUpdateStatus(string s)
{
try
{
this.Invoke(new EventHandler(delegate(object sender2, EventArgs e2)
{
lblStatus.Text = s;
}));
I too favour this paradigm but instead use the MethodInvoker Delegate.

this.[Begin]Invoke(new MethodInvoker(delegate{
//Do some stuff
}));

<...>
JB
Oct 3 '07 #7
Jon Skeet [C# MVP] wrote:
>It seems C# lacks of this ability.

Just to be clear, it's not a language issue.
In a functional language like F#, the overhead is minimal but still there:

form.Invoke(fun () -button.Text <- "Foo")

Couldn't the language provide a construct to add such indirections across
the interface of a given class, making the given class thread safe?

--
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u
Oct 4 '07 #8
Jon Harrop <jo*@ffconsultancy.comwrote:
Jon Skeet [C# MVP] wrote:
It seems C# lacks of this ability.
Just to be clear, it's not a language issue.

In a functional language like F#, the overhead is minimal but still there:

form.Invoke(fun () -button.Text <- "Foo")
My point is that the *framework* is still forcing you to marshall the
call to another thread. Yes, F# makes that relatively easy (as does C#
3) but that doesn't make the controls themselves more threadsafe.
Couldn't the language provide a construct to add such indirections across
the interface of a given class, making the given class thread safe?
I certainly wouldn't want *transparent* marshalling of calls to another
thread. Threading is hard enough when everything is explicit - making
marshalling implicit would only make it harder, IMO.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 4 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: adeger | last post by:
Having trouble with my first forays into threads. Basically, the threads don't seem to be working in parallel (or you might say are blocking). I've boiled my problems to the following short code...
4
by: Gilles Leblanc | last post by:
Hi I have started a small project with PyOpenGL. I am wondering what are the options for a GUI. So far I checked PyUI but it has some problems with 3d rendering outside the Windows platform. I...
7
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates...
4
by: Matthew Groch | last post by:
Hi all, I've got a server that handles a relatively high number of concurrent transactions (on the magnitude of 1000's per second). Client applications establish socket connections with the...
5
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
9
by: mareal | last post by:
I have noticed how the thread I created just stops running. I have added several exceptions to the thread System.Threading.SynchronizationLockException System.Threading.ThreadAbortException...
13
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow...
7
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have...
3
by: John Nagle | last post by:
There's no way to set thread priorities within Python, is there? We have some threads that go compute-bound, and would like to reduce their priority slightly so the other operations, like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.