473,325 Members | 2,792 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,325 software developers and data experts.

ISynchronizeInvoke - 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 ISynchronizeInvoke 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
ISynchronizeInvoke 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 ISynchronizeInvoke 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
ISynchronizeInvoke 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 2469

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

Similar topics

9
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...
0
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...
1
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...
4
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...
1
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...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
1
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...
4
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...
11
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.