jvalescu
asked on
Hopefully easy threading question
I'm implementing a threaded solution for the first time. I have a function declared in my form class called Begin_Monitoring(string name, int interval).
There is a button that starts this process and I've included the following code in my click event:
private void btnBeginMonitoring_Click(o bject sender, System.EventArgs e)
{
// Start a separate thread to perform the computer's move.
this.EnvironmentalMonitorT hread = new Thread(new ThreadStart(this.Begin_Mon itoring("E nvironment ",30)));
this.EnvironmentalMonitorT hread.IsBa ckground = true;
this.EnvironmentalMonitorT hread.Prio rity = System.Threading.ThreadPri ority.Lowe st;
this.EnvironmentalMonitorT hread.Name = "Environmental Monitor";
this.EnvironmentalMonitorT hread.Star t();
}
When I try to compile it, I get a "Method Name Expected" error and it highlights "this.Begin_Monitoring("En vironment" ,30)" Why doesn't it consider Begin_Monitoring to be a method? Or rather, how do I solve this?
It's urgent, hence the points.
There is a button that starts this process and I've included the following code in my click event:
private void btnBeginMonitoring_Click(o
{
// Start a separate thread to perform the computer's move.
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT
}
When I try to compile it, I get a "Method Name Expected" error and it highlights "this.Begin_Monitoring("En
It's urgent, hence the points.
Help for the thread class constructor has an example. You can't include args on the method being passed into ThreadStart. You'll usually make your own thread work class. You instantiate the class, set some member variables (passing in your "Environment" and 30), and then use a method of that instance as your ThreadStart. Then when the thread starts it accesses the local member variables to get the Environment and 30. This allows passing inputs (and outputs) in a threadsafe fashion.
ASKER
Forgot to mention that this is using framework 1.1, so a ParameterizedThreadStart option is not available.
ASKER
For Nightman:
Begin_Monitoring is contained within the form class, the same with the btnBegin_Monitoring_Click event. Should it be somewhere else? I removed the parameters and am still getting the same error.
Begin_Monitoring is contained within the form class, the same with the btnBegin_Monitoring_Click event. Should it be somewhere else? I removed the parameters and am still getting the same error.
class ThreadData
{
public string s;
public int n;
};
ThreadData d = new ThreadData();
d.n = 30;
d.s = "Environment";
this.EnvironmentalMonitorT hread = new Thread(new ParameterizedThreadStart(t his.Begin_ Monitoring ));
...
this.EnvironmentalMonitorT hread.Star t(d);
void Begin_Monitoring(object data)
{
ThreadData d = (ThreadData)data;
...
}
{
public string s;
public int n;
};
ThreadData d = new ThreadData();
d.n = 30;
d.s = "Environment";
this.EnvironmentalMonitorT
...
this.EnvironmentalMonitorT
void Begin_Monitoring(object data)
{
ThreadData d = (ThreadData)data;
...
}
ASKER
AlexFM,
Remember, that I don't have access to "ParameterizedThreadStart" .
Remember, that I don't have access to "ParameterizedThreadStart"
Oops, ParameterizedThreadStart is not available.
In this case encapsulate thread into class which keeps thread parameters. This means, class should contain parameters and thread function. Every thread belongs to class instance. Running thread can get these parameters from current thread instance. Something like this:
http://www.codeproject.com/csharp/workerthread.asp
LongProcess clas can have int and string members which can be set by caller.
In this case encapsulate thread into class which keeps thread parameters. This means, class should contain parameters and thread function. Every thread belongs to class instance. Running thread can get these parameters from current thread instance. Something like this:
http://www.codeproject.com/csharp/workerthread.asp
LongProcess clas can have int and string members which can be set by caller.
Another method is to keep thread parameters in hashtable with key equal to thread instance hash code. When thread instance is created, get it hash code and add all required parameters to hashtable with Integer key, ThreadData value. Thread can access its parameters reading hashtable by Thread.CurrentThread.GetHa shCode key.
I prefer class encapsulating method, but hashtable will work as well.
I prefer class encapsulating method, but hashtable will work as well.
Stepped away for a while. Yes, you will have problems without the parameterised thread. The sample that AlexFM posted is a good example of running a worker thread in the background for each class instance.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So will going to Framework 2.0 be better (and safer)?
I'd say just a bit easier :)
Framework 2.0 will only be safer if you choose the wrong model and are not careful. That said, there are many benefits - generics and parameterised threads among them.
I suspect that what you want is a ParameterizedThreadStart (http://msdn2.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx)
Like this:
this.EnvironmentalMonitorT
this.EnvironmentalMonitorT