473,385 Members | 1,356 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,385 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 1667
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
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...
7
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...
5
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...
37
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...
2
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,...
8
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,...
4
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...
0
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...
9
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...
3
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);...
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
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.