471,337 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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 1607
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*********************@g44g2000cwa.googlegro ups.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(doSomeStuff));

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

"parsifal" <se*************@gmail.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.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(doSomeStuff);
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.com
"parsifal" <se*************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.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
What?? That better not be true. I'm not even using 1.1.

Dec 14 '05 #11
parsifal <se*************@gmail.com> wrote:
What?? That better not be true. I'm not even using 1.1.


No, it's true. You can't write 2.0 code in VS.NET 2002 or VS.NET 2003.

See http://www.pobox.com/~skeet/csharp/t...rameters.shtml for
alternatives though.

Note that your first example wasn't Java, by the way - it was
Javascript, which is very different.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by vee_kay | last post: by
7 posts views Thread by VMI | last post: by
5 posts views Thread by taylorjonl | last post: by
37 posts views Thread by ales | last post: by
2 posts views Thread by John Henry | last post: by
8 posts views Thread by Carl Heller | last post: by
4 posts views Thread by =?Utf-8?B?UGhpbA==?= | last post: by
9 posts views Thread by =?Utf-8?B?anVhbg==?= | last post: by
3 posts views Thread by =?Utf-8?B?SlQ=?= | last post: by

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.