Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / December 2005

Tip: Looking for answers? Try searching our database.

how to start a thread with in-place function?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
parsifal - 14 Dec 2005 15:46 GMT
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#?
Ignacio Machin ( .NET/ C# MVP ) - 14 Dec 2005 16:00 GMT
Are you refering to anonymous methods?

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

> In Java I can start a thread and write the function in-place.  So, you
> could do, in pseudocode:
[quoted text clipped - 3 lines]
>
> How can I do that in C#?
Nicholas Paldino [.NET/C# MVP] - 14 Dec 2005 16:01 GMT
   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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> In Java I can start a thread and write the function in-place.  So, you
> could do, in pseudocode:
[quoted text clipped - 3 lines]
>
> How can I do that in C#?
parsifal - 14 Dec 2005 16:15 GMT
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?
Nicholas Paldino [.NET/C# MVP] - 14 Dec 2005 16:19 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.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?
Brian Gideon - 14 Dec 2005 16:09 GMT
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

> In Java I can start a thread and write the function in-place.  So, you
> could do, in pseudocode:
[quoted text clipped - 3 lines]
>
> How can I do that in C#?
parsifal - 14 Dec 2005 16:18 GMT
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?
Brian Gideon - 14 Dec 2005 16:42 GMT
Are you using VS2005 and the .NET Framework 2.0?

> 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?
parsifal - 14 Dec 2005 16:45 GMT
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?
Brian Gideon - 14 Dec 2005 17:00 GMT
> 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??
parsifal - 14 Dec 2005 17:10 GMT
What??  That better not be true.  I'm not even using 1.1.
Jon Skeet [C# MVP] - 14 Dec 2005 22:56 GMT
> 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/threads/parameters.shtml for
alternatives though.

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

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.