473,599 Members | 3,118 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.Thread and multiple method calls

Hi group,

I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:

Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:

Thread t = new Thread(A);

But how do I add the other 2 methods? Can this be done somehow with
the .NET Library classes or do I have to create my own "Thread-Method-
Manager" for this purpose? I also would like to add and remove the
methods being executed dynamically.

Thansk for your ideas!

Regards,
Paul
Dec 25 '07 #1
16 8370

"Paul Schwann" <pa**********@g mail.comwrote in message
news:66******** *************** ***********@e23 g2000prf.google groups.com...
Hi group,

I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:

Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:

Thread t = new Thread(A);

But how do I add the other 2 methods? Can this be done somehow with
the .NET Library classes or do I have to create my own "Thread-Method-
Manager" for this purpose? I also would like to add and remove the
methods being executed dynamically.

Thansk for your ideas!

Regards,
Paul
Hi Paul,

Not so sure about Java and C, but in every language I've ever worked in a
thread has a single starting point and linear execution path, just like the
"main" program thread. So, in your case, if you want to execute methods A,
B, and C, you would need to create a new method D that calls A, B, and C and
use method D as your thread proc. You can, of course, pass parameters to
method D to identify which other methods to be executed.

I suppose you *could* implement some sort of message pump (like the "main"
program thread usually has) and dynamically execute methods based on
messages, but that seems like it may be overkill, depending on what you are
trying to accomplish.

Scott

Dec 26 '07 #2
Hi Scott,

thanks for your reply! Implementing such a method D by myself is what
I more or less will do now. I just thought how nice it would be based
on some kind of delegates... Like

Thread t = new Thread();
t.executeThis += A;
t.start();
....
t.executeThis += B;
....
t.executeThis += C;
....
t.executeThis -= A;

.... You get the idea... :-)

Regards,
Paul
Dec 26 '07 #3
Scott Roberts <sr******@no.sp am.here-webworks-software.comwro te:
Not so sure about Java and C, but in every language I've ever worked in a
thread has a single starting point and linear execution path, just like the
"main" program thread. So, in your case, if you want to execute methods A,
B, and C, you would need to create a new method D that calls A, B, and C and
use method D as your thread proc. You can, of course, pass parameters to
method D to identify which other methods to be executed.
Yup. One "wrinkle" to .NET is that what you pass in when you create a
thread is a delegate - and delegates can have multiple targets. I've
*never* used ThreadStart in this way before, but it will work. It's
pretty odd...

using System;
using System.Threadin g;

class Test
{
static void Main()
{
ThreadStart start = null;

start += A;
start += B;
start += C;

new Thread(start).S tart();
}

static void A()
{
Console.WriteLi ne ("A");
}

static void B()
{
Console.WriteLi ne ("B");
}

static void C()
{
Console.WriteLi ne ("C");
}
}

Note that you can't add to the list of methods to execute after you've
started the thread though.
I suppose you *could* implement some sort of message pump (like the "main"
program thread usually has) and dynamically execute methods based on
messages, but that seems like it may be overkill, depending on what you are
trying to accomplish.
Again, I agree. Fortunately if the OP *does* need to do this, it's not
too hard to do with a producer/consumer queue.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 26 '07 #4
Paul Schwann <pa**********@g mail.comwrote:
thanks for your reply! Implementing such a method D by myself is what
I more or less will do now. I just thought how nice it would be based
on some kind of delegates... Like
<snip>

Yup, see my recent post for a complete example. You should probably
comment this in your code, however - it's not a common pattern. (Quite
a neat one though.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Dec 26 '07 #5
Hi,
You are saying that the 3 methods needs to be run one after the other, what
if you create a new method (can be anonymous) that call the 3 methods?

void DoIt()
{
A();
B();
C();
}
and then is that method the one that you start the thread with?
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Scott Roberts" <sr******@no.sp am.here-webworks-software.comwro te in
message news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>
"Paul Schwann" <pa**********@g mail.comwrote in message
news:66******** *************** ***********@e23 g2000prf.google groups.com...
>Hi group,

I am relatively new to C# (although I have a lot of programming
excperience in other languages like Java and C). Currently I am
searching for a solution to this problem:

Suppose you have 3 methods A, B and C. All of them shall be run
threaded (I/O tasks) and one after another. I can create a thread and
schedule one method just like:

Thread t = new Thread(A);

But how do I add the other 2 methods? Can this be done somehow with
the .NET Library classes or do I have to create my own "Thread-Method-
Manager" for this purpose? I also would like to add and remove the
methods being executed dynamically.

Thansk for your ideas!

Regards,
Paul

Hi Paul,

Not so sure about Java and C, but in every language I've ever worked in a
thread has a single starting point and linear execution path, just like
the "main" program thread. So, in your case, if you want to execute
methods A, B, and C, you would need to create a new method D that calls A,
B, and C and use method D as your thread proc. You can, of course, pass
parameters to method D to identify which other methods to be executed.

I suppose you *could* implement some sort of message pump (like the "main"
program thread usually has) and dynamically execute methods based on
messages, but that seems like it may be overkill, depending on what you
are trying to accomplish.

Scott

Dec 26 '07 #6
Hi,
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Yup. One "wrinkle" to .NET is that what you pass in when you create a
thread is a delegate - and delegates can have multiple targets. I've
*never* used ThreadStart in this way before, but it will work. It's
pretty odd...
I have never though of this. Of course as ThreadStart is a delegate you can
do it. I wander what would happen if one method throw an exception ....
Dec 26 '07 #7


"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
[Side point - Ignacio, all your posts end up with a signature before
all of the quoted text. That means when I start to reply, all I end up
with is "Hi". Could you either remove the signature separator or make
sure it ends up after the body?]

Sorry for that, no idea why OE place the signature at the top instead of at
the botton of the message. Will have to dig around to see if I can change it
somewhere.

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Dec 26 '07 #8
Hi Ignacio,

basically, you are right:
>
You are saying that the 3 methods needs to be run one after the other, what
if you create a new method (can be anonymous) that call the 3 methods?

void DoIt()
{
* * A();
* * B();
* * C();

}
But I need a way t configure the "three" methods such that I can say
when (and when not) they run. I want to select them during runtime and
thus, need something more dynamic than just a function D().

If you have any ideas how to do this nicely, please let me know!

Regards,
Paul
Dec 26 '07 #9
Hi Pete and all the others,

thanks a lot for your valuable input! Although Jon's solution is
almost perfect, it does'nt work for me. I have to be able to add and
remove any of the A, B, C, ... functions form the delegate while the
thread is *already running*. I think I will implement my own way of
doing this. Here is my first idea (sort of Java/Pseudo code):

class MyThreadManager extends Thread {
List<Runnableex ecuteThis;

public void run() {
for (runnable in executeThis) {
runnable.run();
}
}

synchronized addExecutable(R unnable r) {...}
synchronized removeExecutabl e(Runnable r) {...}
}

Just an idea.... :-)

Regards,
Paul

Dec 27 '07 #10

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

Similar topics

5
10652
by: fooooo | last post by:
This is a network app, written in wxPython and the socket module. This is what I want to happen: GUI app starts. User clicks a button to 'start' the work of the app. When start is pressed, a new thread is spawned (threading module) and this thread starts listening for data on a socket. When someone connects, a new thread is spawned, It needs to do I/O on that socket and open a GUI window so the user can communicate with the client...
1
7129
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
4
2596
by: Jesse Elve | last post by:
I am using an XmlValidatingReader which uses an XSD for xml validation. The code has been performing reliably for months. Yesterday it failed for the first time with the following exception: Inner exception of type System.InvalidOperationException has occurred : The operation is not valid due to the current state of the object. [stack trace = at System.Xml.XmlValidatingReader.set_ValidationType(ValidationType
8
3966
by: Robert Zurer | last post by:
I have a server application that makes a MarshalByReferenceObject available via remoting. It's lifetime is set to never expire and it is implemented as a Singleton. Are calls to this object inherently thread safe? If 1000 threads all call the same method of this object at once, do I have to add anything to my code to assure that there are no problems? Excuse me if this is really obvious. I am somewhat new to remoting and totally new...
6
2852
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things, periodically calls a method in this TimerState object which raises an event to the startup form,...
1
1567
by: Gregory Gadow | last post by:
I am looking for a networkwide messaging system to announce server reboots, etc. To implement various custom features, I would much rather roll my own than use an out-of-the-box type of app. Because my company has a very strict, industry regulated archival policy, I would like to avoid using email (which relies on the user having Outlook up and running anyway, not always a safe bet.) Using VB.NET 2.0, I've written an app that uses MSMQ...
4
8177
by: nhmark64 | last post by:
Hi, Does System.Collections.Generic.Queue not have a Synchronized method because it is already in effect synchronized, or is the Synchronized functionality missing from System.Collections.Generic.Queue? Putting it another way can I safely replace a System.Collections.Queue.Synchronized(myUnSynchronizedQueue) with a System.Collections.Generic.Queue while porting a working 2003 project? Thanks,
15
2758
by: Laser Lu | last post by:
I was often noted by Thread Safety declarations when I was reading .NET Framework Class Library documents in MSDN. The declaration is usually described as 'Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.' So, does this mean All the static/shared methods written in .NET compatible programming language, such as C#, VB.NET, are guaranteed to be...
1
7175
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread. Logic is: 1) i will first create a thread to display a splash screen until a big process is completed 2)then i will close the splash screen and kill the thread.
0
7904
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
8398
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...
0
8267
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
5438
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
3898
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
3940
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2414
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
1
1505
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
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.