473,386 Members | 1,785 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,386 software developers and data experts.

best way to receive 'callbacks' in a GUI from other threads

Hi

Advice needed about what's the best in the following situation.

In essence, I have a GUI that needs to detail time taken to do jobs
that execute in their own thread.

Currently, the GUI thread instantiates a class that wraps a job,
handing it a callback (delegate) to call once the job has finished. On
callback, always on a different thread than the GUI thread,
InvokeRequired is tested (and is always true), and the GUI is updated
via Invoke.

Is this standard and best? Or do people out there do something
different? I'm just concerned about the GUI class having to do a
thread-related test. It seems out of place: it shouldn't have to know
that other threads are involved so is there some kind of standard way
to hide this away?

Thanks and any help appreciated!

Emma.

Sep 18 '06 #1
4 2214
Does this class (the thread) have a reference to the GUI form?
If yes, an alternative to invoking the DELEGATE from the thread, would be to
call Invoke/BeginInvoke directly on the form (passing the callback
delegate).

Ex:

class SomeClass
{
public void WorkerMethod()
{
//do whatever work necessary

mainForm.Invoke(callback);
}
}

--
Adam Clauss

<em**************@fastmail.fmwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi

Advice needed about what's the best in the following situation.

In essence, I have a GUI that needs to detail time taken to do jobs
that execute in their own thread.

Currently, the GUI thread instantiates a class that wraps a job,
handing it a callback (delegate) to call once the job has finished. On
callback, always on a different thread than the GUI thread,
InvokeRequired is tested (and is always true), and the GUI is updated
via Invoke.

Is this standard and best? Or do people out there do something
different? I'm just concerned about the GUI class having to do a
thread-related test. It seems out of place: it shouldn't have to know
that other threads are involved so is there some kind of standard way
to hide this away?

Thanks and any help appreciated!

Emma.

Sep 18 '06 #2
<em**************@fastmail.fmwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
[...]
Currently, the GUI thread instantiates a class that wraps a job,
handing it a callback (delegate) to call once the job has finished. On
callback, always on a different thread than the GUI thread,
InvokeRequired is tested (and is always true), and the GUI is updated
via Invoke.

Is this standard and best? Or do people out there do something
different? I'm just concerned about the GUI class having to do a
thread-related test. It seems out of place: it shouldn't have to know
that other threads are involved so is there some kind of standard way
to hide this away?
IMHO, I agree. There is also the issue that using Invoke causes the calling
thread to halt until the GUI thread processes it.

There is another solution, using the AsyncOperation class, that allows you
to specify a thread on which an operation will be done, queuing it to that
thread without blocking the current thread. The basic idea is that you
create the AsyncOperation on the thread on which you want the operation to
occur, and then you use the AsyncOperation on other threads to put the
operation on the desired thread.

I have used it as an alternative to the Invoke paradigm, and like it a lot.
The code looks simpler IMHO (I don't have a function that does two different
things depending on what thread it's on). It's probably a wash when you
compare things directly (you have an extra object to maintain with
AsyncOperation, and if you use it for raising an event you wind up with an
extra level of function calling), but it just seems cleaner to me, and I
like that the non-GUI thread can get on with what it's doing.

Anyway, none of this is to say that I actually think there's something wrong
with using Invoke. For example, in some cases you always know that you're
in a different thread. If that's true, then there's no reason to check
InvokeRequired. Just have two functions...one used by the non-GUI thread,
which invokes another function used by the GUI thread. It's just that
Invoke isn't the only way to address the issue and you may find some other
technique more to your liking.

Pete
Sep 18 '06 #3
Hi,

<em**************@fastmail.fmwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
Hi
Is this standard and best? Or do people out there do something
different? I'm just concerned about the GUI class having to do a
thread-related test. It seems out of place: it shouldn't have to know
that other threads are involved so is there some kind of standard way
to hide this away?
Then do the opposite, let the worker thread have a reference to the UI
thread ( or to a control of the form to be exact) and is this thread the one
that invoke the "Finished" event. Note that Invoke receive a delegate, so
you can pass any info you need (like time taken executing) back to the UI
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 18 '06 #4
Emma,

If the mechanism you're using implies asynchronous behavior then I
would continue doing what you're doing. Most people assume that
callbacks are invoked from the thread running the asynchronous
operation. This is especially the case if you've used the
BeginSomeOperation/EndSomeOperation pattern.

Another option is to pass a reference a form or control to your worker
class and let it do the marshaling for you. Microsoft used a similar
pattern for the System.Timers.Timer object. In that case they created
a SynchronizingObject property that the Timer uses to automatically
marshal the Elapsed event. If SynchronizingObject is null then the
Elapsed event is executed on a ThreadPool thread. I find that to be a
rather elegant pattern especially if the worker class uses events to
report progress.

Brian

em**************@fastmail.fm wrote:
Hi

Advice needed about what's the best in the following situation.

In essence, I have a GUI that needs to detail time taken to do jobs
that execute in their own thread.

Currently, the GUI thread instantiates a class that wraps a job,
handing it a callback (delegate) to call once the job has finished. On
callback, always on a different thread than the GUI thread,
InvokeRequired is tested (and is always true), and the GUI is updated
via Invoke.

Is this standard and best? Or do people out there do something
different? I'm just concerned about the GUI class having to do a
thread-related test. It seems out of place: it shouldn't have to know
that other threads are involved so is there some kind of standard way
to hide this away?

Thanks and any help appreciated!

Emma.
Sep 18 '06 #5

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

Similar topics

4
by: Kylotan | last post by:
I'm writing a wxPython telnet-like application that needs to be able to send and receive data independently of each other. The asyncore library comes close to what I want, but still requires I have...
10
by: Alfonso Morra | last post by:
This may be considered as OT since the C++ Standard says not one word about threads. Nevertheless, C++ is routinely and widely used to write solid multithreaded code. I wondered if anyone has...
5
by: Adam Clauss | last post by:
Couple questions: 1) I have an application using TCP sockets. When I make a call to BeginReceive(), is the callback I specify called in the current thread or from a new thread? 2) Similar to...
0
by: Shoveler | last post by:
I've got an odd problem here that I've been beating my head on for days. I've written a class that uses a pre-established connection for communication, meaning I can use this class for a server or...
3
by: danavni | last post by:
i need to build a service that will accept incoming TCP/IP connections. the service should act like a "HUB" where on one side clients connect to it and stay connected for as long as they like and...
5
by: Dave A | last post by:
I have an application that does lots of socket communications all asynchronously via the TcpClient class. The code has been working 99.9999% of the time (yeah one of those bugs) but occasionally...
1
by: geoffschmidt | last post by:
I'm trying to write an extension in C that delivers callbacks to Python. The C code starts several threads, and I'd like one of the new threads that is started to be able to deliver callbacks to...
1
by: brekehan | last post by:
I am going to try and keep this non-OS specific, especially when I am trying to implement this on multiple OSes The need has arisen for a callback mechanism. Currently, things are going to go...
6
by: Diego F. | last post by:
Hello. I'm using the socket class in an application and I use socket.receive to wait for data. The problem is that the interface gets locked while the receive method is executing. I want to see...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.