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.
{
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:
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.
VB.Net to C# and J# Converters
Software:Software Development:Tools & Editors
Save Money with our Package Deal: VBConversions VB.Net to C# Converter and VB.Net to J# Converter.
The VB.Net to C# Converter is a stand alone program which converts VB.Net 2003 and 2005 projects to C#.
The VB.Net to J# converter is a Visual Studio 2003 add-in which converts VB.Net 2003 programs to J# conveniently within Visual Studio.
Buy them both together and save. After purchase, you will receive both unlock codes emailed to you immediately from RegNow.
No comments:
Post a Comment