473,545 Members | 527 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to start a thread with in-place function?

In Java I can start a thread and write the function in-place. So, you
could do, in pseudocode:

def someFunc:
thread.new( def summit: doSomeStuff() )

How can I do that in C#?

Dec 14 '05 #1
11 1674
Are you refering to anonymous methods?

They are not present in 1.1 they are in 2.0 though.

"parsifal" <se************ *@gmail.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
In Java I can start a thread and write the function in-place. So, you
could do, in pseudocode:

def someFunc:
thread.new( def summit: doSomeStuff() )

How can I do that in C#?

Dec 14 '05 #2
You need to pass an instance of the ThreadStart delegate to the
constructor for your thread class, like so:

Thread t = new Thread(new ThreadStart(doS omeStuff));

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"parsifal" <se************ *@gmail.com> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
In Java I can start a thread and write the function in-place. So, you
could do, in pseudocode:

def someFunc:
thread.new( def summit: doSomeStuff() )

How can I do that in C#?

Dec 14 '05 #3
If using 2.0 you can write the ThreadStart method in place as Ignacio
pointed. Here is an example.

public static void Main()
{
ThreadStart doSomeStuff = delegate()
{
// Put your thread code here.
};

Thread thread = new Thread(doSomeSt uff);
thread.Start();
thread.Join();
}

Brian

parsifal wrote:
In Java I can start a thread and write the function in-place. So, you
could do, in pseudocode:

def someFunc:
thread.new( def summit: doSomeStuff() )

How can I do that in C#?


Dec 14 '05 #4
Okay so here's what I want to do: I need to start multiple threads that
do the same things, but with different parameters. What's the simplest
way to do that?

Dec 14 '05 #5
Oh interesting. That's exactly what I want, but my Visual Studio is
complaining about my use of delegate in "delegate() {..}". It says
"Invalid expression term 'delegate'". Do I need to tell it to use 2.0?
What am I doing wrong?

Dec 14 '05 #6
parsifal,

If you are using .NET 2.0, have your method take an object parameter,
and then pass the parameter to the Start method on the Thread instance.

If you are using .NET 1.1 and before, then what you want to do is
encapsulate your logic into a type which holds all the information needed to
perform the processing. Then, you set the values on an instance of the
type, and then pass the delegate to your Thread instance. For each separate
instance, you pass a separate instance of your class.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"parsifal" <se************ *@gmail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Okay so here's what I want to do: I need to start multiple threads that
do the same things, but with different parameters. What's the simplest
way to do that?

Dec 14 '05 #7
Are you using VS2005 and the .NET Framework 2.0?

parsifal wrote:
Oh interesting. That's exactly what I want, but my Visual Studio is
complaining about my use of delegate in "delegate() {..}". It says
"Invalid expression term 'delegate'". Do I need to tell it to use 2.0?
What am I doing wrong?


Dec 14 '05 #8
I have installed .NET 2.0, but I'm using VS.NET 7. In the About box,
it says .NET Framework 1.0. How can I change that?

Dec 14 '05 #9

parsifal wrote:
I have installed .NET 2.0, but I'm using VS.NET 7. In the About box,
it says .NET Framework 1.0. How can I change that?


I don't think you can??

Dec 14 '05 #10

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

Similar topics

6
8843
by: vee_kay | last post by:
Ihave a written aprogram in C which implements _beginthread(to create a thread) and _endthread(to end a thread).The program need to write a string of date n time to a file for each succesful thread created. I had put a delay of a second so that the thread and io operation will occur after a second. Now i need to implement another thing which i...
7
1502
by: VMI | last post by:
In the following code, how can I display the MessageBox once MyThread has finished executing? updateFiles method copies 5 humongous files to the HDD but for some reason, the MessageBox is displayed before updateFles has finished executing. This is the code: MyThread = new Thread(new ThreadStart (updateFiles)); MyThread.Start();...
5
2202
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a MainForm what has a Start/Stop button that starts and stops the processing thread. private void StartButton_Click(object sender, System.EventArgs e) {...
37
8844
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here is code used for measuring:
2
1416
by: John Henry | last post by:
Can Python thread start threads? It appears not. When I do that, the sub-threads gets to certain point and just sit there. If I run the code serially and not run the sub-thread code as threads, everything is fine. I throught the problem is when you run multiple processes of Pythons...
8
1680
by: Carl Heller | last post by:
If I'm creating a class to do some work that I want threaded out, where's the best location to call ThreadStart? Or does it depend on the nature of the work? a. Call it outside the class, giving it the starting method of the class? b. Have the class create the thread itself? ie: x = new WorkerClass(); ioThread = new Thread(new...
4
4112
by: =?Utf-8?B?UGhpbA==?= | last post by:
I have a dll that I call to start a thread that will monitor a serial port and then process and store the data received from that port. On most every computer I have run this on, the program works perfectly. However, I have a Panasonic CF-51 laptop that fails when I try to launch the thread. It works on other CF-51's, it only fails on this...
0
1518
by: Yue Fei | last post by:
I have a multi thread python code, threads can start immediately if I run on command line, but I can get them started right the way if I call the same code from C/C++. test code like this: from threading import Thread import thread class testThread(Thread): def __init__ (self, id): Thread.__init__(self) self.id = id def run(self):
9
1414
by: =?Utf-8?B?anVhbg==?= | last post by:
How can I do to start a thread. I tried everything... but it had no work. Any solution? It is a Sub working with a Timer control. I want to do two tasks at the same time (more or less for a minute). Visual Basic 2005. Thanks.
3
1993
by: =?Utf-8?B?SlQ=?= | last post by:
If I call Thread.Start am I guaranteed that thread will be running before the call from Thread.Start returns? i.e. //Doing Something on main thread Thread.Start(NewThreadWork); Subscription.Create //This relies on the New Thread running Thanks
0
7464
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...
0
7656
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. ...
1
7413
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...
0
5968
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...
1
5323
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...
0
3440
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1874
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
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
700
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...

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.