473,770 Members | 2,069 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoke question when dealing with multiple threads

I am creating a pub/sub broker implementation in C# and I am having some
trouble understanding how I should best implement the invoke portion in
order to make sure it is as thread-safe as it can be.

The "broker" class allows other objects to subscribe to messages that will
get published by other objects. To subscribe they pass in a Object that
represents thier instance (aka 'this') and a String that contains the method
name to be invoked. When a particular message is published the instance and
method name are used to forward the message (and any data that was sent with
it) to the subscriber so they can process it. Each message may have zero or
more subscribers as well.

To process the queued messages I am using a System.Timers.T imer class which
uses a separate thread to handle the delay between the timer events. Inside
the Timer elapsed event I am forwarding all of the queued messages to any
subscribers that are interrested in them.

To perform the Invoke I am doing the following which I believe will cause
problems.

Instance.GetTyp e().GetMethod(M ethodName).Invo ke(Instance, Parms);

Where Instance is an Object that the subscriber passed in (aka 'this'),
MethodName is a String that contains a public method to be invoked, and
Parms is a Object[] that contains the parameters that the method is
expecting.

How thread-safe is this since there are at least 2 threads being used
(whatever the application thread is and the Timer thread.) I will not know
ahead of time what the subscriber actually is - plain class, WinForms class,
etc.

I have seen mention of ISynchronizeInv oke, Invoke, BeginInvoke and EndInvoke
but I am not sure exactly how they play into this sort of environment.

Any suggestions or input would be appreciated.

Chuck

Nov 17 '05 #1
1 1566
If you have no other threads acting in your system, you could use the
System.Windows. Forms.Timer which will have your timer routine called by the
thread processing your message pump. In your scenario it doesn't look like
timing is too critical. This way you stay single threaded. If you have other
worker threads in your system already than you have to worry about
synchronization anyway and you might as well get the performance advantage
of System.Threadin g.Timer.
"Chuck" <Chuck Cox (at) chucks software (dot) com> wrote in message
news:YP******** ************@co mcast.com...
I am creating a pub/sub broker implementation in C# and I am having some
trouble understanding how I should best implement the invoke portion in
order to make sure it is as thread-safe as it can be.

The "broker" class allows other objects to subscribe to messages that will
get published by other objects. To subscribe they pass in a Object that
represents thier instance (aka 'this') and a String that contains the
method name to be invoked. When a particular message is published the
instance and method name are used to forward the message (and any data
that was sent with it) to the subscriber so they can process it. Each
message may have zero or more subscribers as well.

To process the queued messages I am using a System.Timers.T imer class
which uses a separate thread to handle the delay between the timer events.
Inside the Timer elapsed event I am forwarding all of the queued messages
to any subscribers that are interrested in them.

To perform the Invoke I am doing the following which I believe will cause
problems.

Instance.GetTyp e().GetMethod(M ethodName).Invo ke(Instance, Parms);

Where Instance is an Object that the subscriber passed in (aka 'this'),
MethodName is a String that contains a public method to be invoked, and
Parms is a Object[] that contains the parameters that the method is
expecting.

How thread-safe is this since there are at least 2 threads being used
(whatever the application thread is and the Timer thread.) I will not
know ahead of time what the subscriber actually is - plain class, WinForms
class, etc.

I have seen mention of ISynchronizeInv oke, Invoke, BeginInvoke and
EndInvoke but I am not sure exactly how they play into this sort of
environment.

Any suggestions or input would be appreciated.

Chuck

Nov 17 '05 #2

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

Similar topics

6
3206
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
3
8883
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form { mySocketClass sss; ...
8
1247
by: Lenn | last post by:
Hello, Just some background: I'm developing an application that basically executes series of tasks. So far I have 2 group of tasks run on different threads (2 different threads). Which run in parallel. Now, these threads need read-only access to app config settings Object. So, I have one public class with 1 static field - Hashtable which holds key/value configs settings. So far, application runs in debug mode without any errors. Is that...
4
3584
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
2
2622
by: Lucvdv | last post by:
To avoid a temporarily frozen user interface, I'm using a separate thread to fill a list with items found in a database (there can be from a few up to about 1000 or 1500 items). There seems to be a problem in this: if the form containing the list is closed before the worker thread has finished, the Invoke call used to add entries to the list never returns. I'm trying to avoid it in any way I can imagine, but somehow it keeps getting...
0
2961
by: Pawan Narula via DotNetMonster.com | last post by:
hi all, i'm using VB.NET and trying to code for contact management in a tree. all my contacts r saved in a text file and my C dll reads them one by one and sends to VB callback in a sync mode thread. so far so good. all contacts r added properly. now when another login adds me in his contact, i recv a subscription, so i popup a form and ask for accept/reject. this all happens in a separate thread. popup form gets opened and choice is...
3
9581
by: Me | last post by:
I am trying to figure out any issues with calling System.Reflection.MethodInfo.Invoke() when dealing with multiple threads. For instance.. Say I have a class that allows you to pass in a string that represents a method name and an object that represents an instance of some class which contains the previous function name. Now, I want to execute that method name by calling the Invoke() method of MethodInfo similar to the following...
23
2646
by: Thomas Due | last post by:
Hi, I have a class which monitors a TCP socket. This will on occasion raise an event which can be handled by a GUI. Now, I am aware of the if(InvokeRequire) { EventHandler d = new EventHandler(); Invoke(d, new object{sender, e}); } else {
5
1711
by: iLL | last post by:
So why is it that we need to use the Invoke method when updating the GUI? The following is the way we are suppose to update GUI components: delegate void textIt(object o); public partial class Form1 : Form { public Form1() {
0
9454
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
10260
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
10038
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
8933
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...
0
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
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.