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 / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

threading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
johnny - 07 Jan 2005 09:38 GMT
Very simple question. I need to pass a parameter to a newly created thread.
How can I accomplish it. I'm sure it must be very easy - something like it
was in Win32 CreateThread had 1 4B parameter.
Wiktor Zychla - 07 Jan 2005 09:54 GMT
> Very simple question. I need to pass a parameter to a newly created
> thread.
> How can I accomplish it. I'm sure it must be very easy - something like it
> was in Win32 CreateThread had 1 4B parameter.

basically you have to create a helper class where you put all parameters as
fields and make the thread function a method of the class. this way you will
be able to use all parameters inside the thread func. remember to initialize
these parameters somehow before you start new thread.

Wiktor Zychla
Michael Groeger - 07 Jan 2005 09:56 GMT
Create a class for the thread and pass the parameter in the constructor of
the class:

public class MyThreadClass
{
   private int param;

   public void Run()
   {
       new Thread(new ThreadStart(DoSomethingImportant)).Start();
   }

   public void DoSomethingImportant()
   {
       param++;
   }
}

public class TestApp
{
   public static void Main()
   {
        MyThreadClass o = new MyThreadClass(100);
       o.Run();
   }
}

Michael

> Very simple question. I need to pass a parameter to a newly created thread.
> How can I accomplish it. I'm sure it must be very easy - something like it
> was in Win32 CreateThread had 1 4B parameter.
Miha Markic [MVP C#] - 07 Jan 2005 10:04 GMT
Hi johnny,

Jon Skeet has good articles on this topic, see:
http://www.yoda.arachsys.com/csharp/threads/parameters.shtml

Signature

Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com

> Very simple question. I need to pass a parameter to a newly created
> thread.
> How can I accomplish it. I'm sure it must be very easy - something like it
> was in Win32 CreateThread had 1 4B parameter.
Marcos Stefanakopolus - 07 Jan 2005 17:05 GMT
My preferred solution for this is to create a class that holds all the data
that the thread will need.  Then, make an instance (non-static) method in
that class to encapsulate whatever work it is that you want to do in the
thread.  Create an instance of your class in your main thread, initialize it
however you like, and then call Thread.Start on the method:

using System.Threading;
namespace ThreadExample {

   class ThreadExample {
       static void Main() {
           myClass C = new myClass(12, "hello"); // construct and
initialize object to hold data for the thread
           workerThread = new Thread(new
ThreadStart(c.myThreadStartMethod));
           workerThread.IsBackground = true;
           workerThread.Start();
       }
   }

   class myClass {
       private int SomeField;
       private string SomeOtherField;
       public void myClass(int i, string s) {
           SomeField = i;
           SomeOtherField = s;
       }
       public void myThreadStartMethod() {
           // do lots of work involving SomeField and SomeOtherField
       }
   }
}

> Very simple question. I need to pass a parameter to a newly created
> thread.
> How can I accomplish it. I'm sure it must be very easy - something like it
> was in Win32 CreateThread had 1 4B parameter.

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.