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

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.Timer 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.GetType().GetMethod(MethodName).Invoke(In stance, 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 ISynchronizeInvoke, 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 1547
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.Threading.Timer.
"Chuck" <Chuck Cox (at) chucks software (dot) com> wrote in message
news:YP********************@comcast.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.Timer 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.GetType().GetMethod(MethodName).Invoke(In stance, 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 ISynchronizeInvoke, 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
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...
3
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 {...
8
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...
4
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...
2
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...
0
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...
3
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...
23
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...
5
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...
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...
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...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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...

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.