473,786 Members | 2,375 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 1694
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
8865
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 need to make sure the run was actually a second. This is because if i add another delay of...
7
1512
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(); MessageBox.Show("Program has ended");
5
2221
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) { if( bStopSignal ) { // disable controls that aren't valid when running
37
8910
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
1425
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
1699
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 ThreadStart(x.StartWork));
4
4121
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 particular computer. I get no exceptions; it simply reports the threadstate as Stopped.
0
1528
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
1426
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
2003
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
9647
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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
10360
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
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
6744
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
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.