473,624 Members | 2,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ISynchronizeInv oke - Delegate Queue

One of the things I've struggled with from time to time is handling
events raised on different threads. For example, an object could be
listening to events from one or more objects running in different
threads besides itself. There is a concern for thread safety. There are
various ways of dealing with this. One way is to make the class thread
safe by putting in locks. But another way is to stream the events from
the various threads into a single thread.

I implemented the ISynchronizeInv oke interface as a delegate queue
class. Basically, clients of this class can call BeginInvoke from
various threads to stream the threads into one. Internally, the delegate
queue is running in its own thread. When BeginInvoke is called, it
enqueues the method and arguments (and a few other items as well) into
the queue and pulses a locked object. From inside the worker thread, the
queue is dequeued, the arguments are retrieved, and the method is
invoked. There are other things going on as well just to ensure that the
ISynchronizeInv oke specification is met.

The way more than one thread can be streamed into one is to have several
objects running in different threads share the same delegate queue.
From the outside, the classes look normal, but when a method call is
made or an event needs to be raised, the delegate representing the
method to invoke is enqueued into the delegate queue. The end result is
that the objects are streaming to the same thread. This would have to be
used with caution for performance reasons, obviously.

Has the ISynchronizeInv oke interface been implemented this way before?
I'm only aware of the Control class's implementation.

Event queues are nothing new. So there's nothing new here in this idea.
I just thought it might be a useful application of the
ISynchronizeInv oke interface. I plan to explore this more to see how
well it works in practise.

I do have a question, though. The Invoke method is suppose to return
synchronously. That's no problem, just have the method wait until the
method is invoked inside the worker thread. However, the documentation
also says that any exceptions thrown from the method invocation are
propagated back to the caller. Hmm, I could do a try/catch in the worker
thread. If an exception is thrown, I could include the exception as an
argument, signal the Invoke method, it would check to see if an
exception has been thrown. If so, it rethrows the exception. But I'm
wondering is there another (better) way?

Nov 17 '05 #1
0 2494

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

Similar topics

9
2239
by: GM | last post by:
Hi, My application has a need to cache a number of shared reference lists containing basic business objects. In order to improve performance these lists are fetched and updated in the background. When a UI is required to display one of these reference lists it requests it by name from the cache. If the list already exists in the cache it is returned to the caller otherwise a brand new empty one is created and returned. This new empty...
0
1414
by: Jedrzej Miadowicz | last post by:
I have a problem when using ISynchronizeInvoke.BeginInvoke with polymorphic parameters. It seems that if I try to call BeginInvoke and in the array of parameters place an object that's derived from a base class my method expects as a parameter, the method gets only an object of the type of the base class, rather than my full object. I know this sounds convoluted, so here's an example. My program consists of an engine and a WindowsForms...
1
323
by: John | last post by:
Hi, I have a worker thread that receives messages from a socket and puts them in a queue (it sits inside a "while true") loop. Each time it adds a message to the queue, I need the main thread to pull a message off the queue and process it. Because it manipulates GUI controls I need the main thread to do the dequeue and not the worker thread.
4
1189
by: Paul Tomlinson | last post by:
OK, I have a multi-threaded application. I have a master thread which calls a delegate which I want the child thread to execute but that is not happening, instead the master thread is running the code in the delegate function. I know this as I can see the thread id's. This is what I *want* to happen i.e. (Master Thread:)
1
5006
by: Tim Murray | last post by:
Hi, I am creating a multithreaded server application in Managed C++. Each client that connects to the server will be running on their own thread to process their input. They all need to share one instance of a class running on the main thread. From my understanding, if multiple threads invoke a function in this object(on the main thread) using a delegate, there could be problems. I was trying to figure out how to invoke the functions...
3
2607
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
3079
by: Thai Mai Shu | last post by:
What is wrong with my call below. If I change the delegate and the CallBackComplete function to not take in parameters then the .Invoke call works fine. As soon as I put the parameters back I get "Parameter count mismatch." error. Am I supposed to implement ISynchronizeInvoke in order to call .Invoke? If so, why does it work when the method i am trying to invoke has no parameters. Private Delegate Sub CallbackCompleteDelegate(ByVal...
4
4019
by: jherl | last post by:
I am getting an "Object reference not set to an instance of an object" when I try to execute the Invoke method. I have it set up as follows: --- Private _syncObject As System.ComponentModel.ISynchronizeInvoke .. .. .. Dim args(0) As Object Dim d As New RaiseReceiveEvent(AddressOf OnReceive) ..
11
6952
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then making the main thread sleep until the counter gets back to zero. The call back function for each call decrements the counter. Is there a better way to make the thread wait until all calls are complete besides using the counter? I've seen some things...
0
8238
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
8680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8336
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
8478
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...
0
7164
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
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
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.