473,803 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callback methods are executed in different threads. How have the main thread do the job???

Hello,

exploring more and more the .NET framework I've seen that threads are
often employed by the framework, for example in:

- callback methods on timers
- async operations (async receives on message queues, etc.)

Wanting to avoid executing the event handler code in a different
thread is there a *standard* way (something like a pattern) to
accomplishing this??? I remember having read something relative to the
need to update UIs only from the thread that creates them. Having a
worker thread that needs to update a UI element how can the update be
done by the thread that created the UI on behalf of the worker
thread??? I'm asking because I believe that the same approach could be
a possible answer to my initial question.
Thx.
Bob Rock
Nov 16 '05 #1
2 1436
Bob,
Having a
worker thread that needs to update a UI element how can the update be
done by the thread that created the UI on behalf of the worker
thread???

Typically you would do this by having an event handler on your form to do
the work of updating the GUI. As for calling the event handler from another
thread, any class that descends from Control { for example Form } has methods
for this purpose:

BeginInvoke
EndInvoke
Invoke

These methods assure that the thread that created the control will execute
the event delegate that you specify; you can safely call these 3 methods from
your worker thread. See documentation for explanations and examples...

As far as patterns are concerned - I forget the name exactly but there is an
"Event and Delegate Pattern" out in MSDN. The pattern explains the required
pieces for making a custom event handler and it provides naming convention
recommendations for the pieces...

--Richard
"Bob Rock" wrote:
Hello,

exploring more and more the .NET framework I've seen that threads are
often employed by the framework, for example in:

- callback methods on timers
- async operations (async receives on message queues, etc.)

Wanting to avoid executing the event handler code in a different
thread is there a *standard* way (something like a pattern) to
accomplishing this??? I remember having read something relative to the
need to update UIs only from the thread that creates them. Having a
worker thread that needs to update a UI element how can the update be
done by the thread that created the UI on behalf of the worker
thread??? I'm asking because I believe that the same approach could be
a possible answer to my initial question.
Thx.
Bob Rock

Nov 16 '05 #2
Hi Bob,

If you have a method declared in a class that inherits from Control and that
method sometimes might be executed in a worker thread (generally speeking in
a thread that has not created the control), but you want to have it executed
in the thread that has created the control, the pattern I 'd suggest (and
the pattern many developers follow) is

//Foo is declared in a class inheriting from control
ReturnType Foo(param1, param2,...)
{
if(this.InvokeR equired)
{
return (ReturnType)thi s.Invoke(new DelegateForFoo( Foo), new
object[]{param1, param2,....});
}

//Do some work in the UI thread

return value;
}

--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Bob Rock" <an************ ***@yahoo.com> wrote in message
news:98******** *************** **@posting.goog le.com...
Hello,

exploring more and more the .NET framework I've seen that threads are
often employed by the framework, for example in:

- callback methods on timers
- async operations (async receives on message queues, etc.)

Wanting to avoid executing the event handler code in a different
thread is there a *standard* way (something like a pattern) to
accomplishing this??? I remember having read something relative to the
need to update UIs only from the thread that creates them. Having a
worker thread that needs to update a UI element how can the update be
done by the thread that created the UI on behalf of the worker
thread??? I'm asking because I believe that the same approach could be
a possible answer to my initial question.
Thx.
Bob Rock

Nov 16 '05 #3

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

Similar topics

0
2515
by: jordi | last post by:
Hi, I'm starting to use Python embedded in a C program. I'm using Python to execute several scripts using as a variables information retrieved for several multithread "agents" written in C. The method is: 1- Create a thread for each agent (I use a pool thread, every agent run in a different thread) 2- Precompile a different script for each agent. 3- Make the agent retrieve its data
1
7145
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these threads are complete, the call the Callback method. Each "thread" calls the same callback method. What thread does this callback method exist on? My testing indicates the
0
1314
by: chadb | last post by:
Basic scenario -Winforms client calling an asp.net web service on Windows X Problem: When I perform an async web service call from a winform client, my Thread.CurrentCulture and Thread.CurrentUICulture are changed from the initiating thread's culture Some code Set all the Cultures to a different culture static void Main()
2
3225
by: Michel Schilthuizen | last post by:
Hi, I am working on an application that can use functionlity in some sort of plugins. I have implemented this by using Assembly.LoadFrom and MethodInfo.GetMethods. The calling of the plugins is handled in threads. All works fine, but when I call a method that processes for some time, the mainthread (my form) becomes unresponsive.
1
2625
by: johnny | last post by:
In a multi-threaded application, say a worker thread makes an asynchronous call and specifies a callback method. But before the callback is executed, the thread is aborted by its creator. What is the expected behavior for this scenario? Does the thread stay alive until the callback is executed? If an exception is thrown, can it be caught? I posted this message last week, but got no response. I am really hoping someone can help me with...
2
6553
by: Steve Lutz | last post by:
Hi, I have a simple question that I cannot seem to find the answer to. Let's say my application has 2 threads, a Main thread and a Worker thread. As the worker thread progresses, it send results back to the main thread via a delegate. What thread does the delegate run in?
4
5617
by: Bob Rock | last post by:
Hello, I was wondering if callback delegates, called in response to a event, are executed in their own thread. I was suspecting the OS might spawn a new thread and have the delegate execute in that thread but having written a small sample application this does not seem to be happening. I'm writing an application that will be receiving messages from a Tibco RV messaging bus and from MSMQ and that will also have to handle those messages...
1
1441
by: swartzbill2000 | last post by:
Hello, >From the following location: MSDN Home > MSDN Library > .NET Development > .NET Framework SDK > ..NET Framework > Programming with the .NET Framework > Threading > Using Threads and Threading I copied the VB.Net code for 'Retrieving Data With Callback Methods'. Using Thread.CurrentThread.ManagedThreadId and Debug.Print, I see that the Callback method runs on the worker thread, not the main thread. Is
4
4803
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread completes it invokes a method from the calling thread. Sort event driven concept with threads. Thanks. Ed Gomez
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9566
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10300
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7607
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.