Error » Certification & Programming center Error !! » Programming tutorials » Pass Variables to a New Thread in C#

Programming tutorials All Knowledge Info and links to posted here

Post New Thread Reply
  Pass Variables to a New Thread in C#
LinkBack Thread Tools Display Modes
Old 15-Dec-2006, 01:21 AM   #1 (permalink)
Administrator
 
Anilrgowda's Avatar

Posts: 18,715
Join Date: Jan 2006
Rep Power: 10 Anilrgowda is on a distinguished road

IM:
Default Pass Variables to a New Thread in C#

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.


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:


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.
Anilrgowda is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Reply With Quote
   


   
Post New Thread Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wii looks to pass Xbox 360 sales soon Anilrgowda wii news 0 14-Aug-2007 12:21 AM
EE thread opens at start, not at actual post Anilrgowda Microsoft windows vista error 1 10-Jul-2007 12:45 AM
PHOTOSHOP TUTORILAS FOR BEGINERS MEGA Thread AQUARIAN Graphic tutorials 51 03-Mar-2007 02:24 AM
How to set environment variables Anilrgowda Linux and Unix Error ! 0 24-Jan-2007 03:18 AM
C++: Pointers, Pass by Value, Pass by Reference Admin Programming tutorials 0 03-Dec-2006 11:26 PM


All times are GMT -8. The time now is 09:30 AM.

Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0

DMCA Policy

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228