Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 14, 2024, 02:00:39 PM]

[February 08, 2024, 10:26:18 AM]

[February 08, 2024, 10:26:18 AM]

[February 08, 2024, 10:26:18 AM]

[February 08, 2024, 10:26:18 AM]

[November 27, 2023, 06:32:12 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » C/C++/C#
 Pass Variables to a New Thread in C#
Pages: [1]   Go Down
  Print  
Author Topic: Pass Variables to a New Thread in C#  (Read 52532 times)
Taruna
Elite Member
*****



Karma: 13
Offline Offline

Posts: 845

Hi ALL


View Profile
Pass Variables to a New Thread in C#
« Posted: December 27, 2006, 12:59:29 AM »


When you create a new thread in .net 1.1, you cannot pass any parameters to the ThreadStart delegate, which makes passing startup variables difficult. This recipe shows you an easy workaround.

Since the ThreadStart delegate doesn't accept parameters, you need to set the parameters somewhere before you create the new thread. What we'll do is create a small class to store the variables, and then create a function in the class to pass into ThreadStart.

Code:
class myObject
{
   public string myvariable;

   public void RunThread()
   {
      // use myvariable here
   }
}

You should really use properties instead of a public variable, but this makes the code sample simpler. The method that you pass into ThreadStart must be void, and accept no parameters.

Here's the sample code for creating the object, passing in a variable, and then using the object to create the new thread:

Code:
myObject m = new myObject();
m.myvariable = "test data that the thread will need";

// Create the new thread, using the function
// from the object we created

Thread t = new Thread(new ThreadStart(m.RunThread));
t.IsBackground = true;
t.Name = "UpdateBookmarkThread";
t.Start();

Now when the thread is started, it will have access to the variables stored in it's own object instance. This technique is very useful if you need to open multiple threads, passing in different information to each.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.102 seconds with 24 queries.