|
-
Jul 20th, 2007, 02:35 PM
#1
Thread Starter
Hyperactive Member
Passing in a string to a thread I am about to start? [C# 2002]
I am trying to accomplish something like this:
Code:
{
string sType ="Update";
Thread th1 = new Thread (new ThreadStart(Operation1(sType)));
th1.Start();
}
private void Operaton1(string sType)
{
if (sType == "Update")
{
}
else
{
}
}
Obviouslly this doesn't work and returns and error message as follows:
Method name expected
It works fine if my "Operation1" has no parameters but I somehow need to pass the information from "sType" into the Thread itself...
How could I accomplish passing a STRING into a THREAD I am about to start?
Any help would be greatly appreciated.
Thanks,
-
Jul 20th, 2007, 03:44 PM
#2
Re: Passing in a string to a thread I am about to start? [C# 2002]
Hi there,
I'm certainly no expert in threading, but I read up a little bit and using your version of the framework you can consider storing your variable in an instance of a class. So perhaps something like this:
c# Code:
// My form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// New instance of my other class.
private void button1_Click(object sender, EventArgs e)
{
string str = "Update";
MyOperations op = new MyOperations(str);
new Thread(new ThreadStart(op.Operation1)).Start();
}
}
// My other class.
public class MyOperations
{
string _str;
public MyOperations (string str)
{
this._str = str;
}
public void Operation1()
{
if (_str == "Update")
{
MessageBox.Show("Update.");
}
else
{
MessageBox.Show("No update.");
}
}
}
I did find many sites that spoke about this topic in my search with much more information. Give Google a go and I'm sure you'll find plenty. I hope this little gets you started.
-
Jul 20th, 2007, 04:08 PM
#3
Thread Starter
Hyperactive Member
Re: Passing in a string to a thread I am about to start? [C# 2002]
Thats a great idea - and while trying to implement it I ran into a single roadblock - Operation1 uses functions part of the class that is spawning the thread - if I pass in a class.function as opposed to a local function I loose scope of these functions and can no longer launch them (from what I can see). For example ... using your code ...
Code:
// My form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// New instance of my other class.
private void button1_Click(object sender, EventArgs e)
{
string str = "Update";
MyOperations op = new MyOperations(str);
new Thread(new ThreadStart(op.Operation1)).Start();
}
private delegate void DoTheWorkDelegate
private void DoTheWork(string sStuff)
{
// DoTheWork stuff
// This can be called by a thread using “DoTheWorkDelegate”
}
}
// My other class.
public class MyOperations
{
string _str;
public MyOperations (string str)
{
this._str = str;
}
public void Operation1()
{
if (_str == "Update")
{
DoTheWork(“update”);
}
else
{
DoTheWork(“not update”);
}
}
}
So doing it that way I get errors because "DoTheWork" is not defined in the new class "MyOperations" - any clue how to solve this aside from passing the "this" into the "MyOperations" class also and doing something like this.DoTheWork? (as I am not sure how threadsafe that kind of request would be).
Thanks,
-
Jul 20th, 2007, 06:19 PM
#4
Re: Passing in a string to a thread I am about to start? [C# 2002]
Heres a simple example of passing a string to a thread:
C# Code:
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread thr1 = new System.Threading.Thread(DoWork);
thr1.Start("Hello");
}
private void DoWork(object obj)
{
string Message = Convert.ToString(obj);
MessageBox.Show(Message);
}
-
Jul 20th, 2007, 06:30 PM
#5
Re: Passing in a string to a thread I am about to start? [C# 2002]
 Originally Posted by Atheist
Heres a simple example of passing a string to a thread:
C# Code:
private void Form1_Load(object sender, EventArgs e)
{
System.Threading.Thread thr1 = new System.Threading.Thread(DoWork);
thr1.Start("Hello");
}
private void DoWork(object obj)
{
string Message = Convert.ToString(obj);
MessageBox.Show(Message);
}
Wow, that was quite easy. I can't believe that in all of sites that I looked on regarding passing a parameter to a new thread, that I didn't see that. All of them mentioned something with a class.
Is this also available in 1.1? I'm not sure how the documentation on MSDN is exactly for 1.1, but they don't show an Overload for this method.
-
Jul 20th, 2007, 06:41 PM
#6
Re: Passing in a string to a thread I am about to start? [C# 2002]
If you want to be able to execute a method of an object you have no reference to then you need to pass a delegate. The very reason that delegates exist is to enable executing methods without references to their owners. Create a DoTheWorkDelegate instance and pass it to your MyOperations object, from which it can be invoked.
-
Jul 23rd, 2007, 09:03 AM
#7
Thread Starter
Hyperactive Member
Re: Passing in a string to a thread I am about to start? [C# 2002]
jmcilhinney: How do you pass in a delegate to a thread?
As it stands the solution I found was to pass in the "this" pointer to the thread so that I can access all the required functions - and in the spawning class I created delegates for each function based on the Invoke requirements. I did a quick test and it looks like, when in my thread, if I do something like fMain.DoSomething the thread will go to the calling class and be Invoked and then run the function.
Is this also a correct and threadsafe way to do it?
Last edited by Shaitan00; Jul 23rd, 2007 at 12:16 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|