Link to home
Start Free TrialLog in
Avatar of jvalescu
jvalescuFlag for United States of America

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(object sender, System.EventArgs e)
            {
                        // Start a separate thread to perform the computer's move.
                        this.EnvironmentalMonitorThread = new Thread(new ThreadStart(this.Begin_Monitoring("Environment",30)));
                        this.EnvironmentalMonitorThread.IsBackground = true;
                        this.EnvironmentalMonitorThread.Priority = System.Threading.ThreadPriority.Lowest;
                        this.EnvironmentalMonitorThread.Name = "Environmental Monitor";
                        this.EnvironmentalMonitorThread.Start();
          }

When I try to compile it, I get a "Method Name Expected" error and it highlights "this.Begin_Monitoring("Environment",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.
Avatar of Nightman
Nightman
Flag of Australia image

Where is the function 'Begin_Monitoring' ?

I suspect that what you want is a ParameterizedThreadStart (http://msdn2.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx)

Like this:
   this.EnvironmentalMonitorThread = new Thread(new ParameterizedThreadStart(this.Begin_Monitoring());
   this.EnvironmentalMonitorThread.Start("Environment",30)
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.
Avatar of jvalescu

ASKER

Forgot to mention that this is using framework 1.1, so a ParameterizedThreadStart option is not available.
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.

Avatar of AlexFM
AlexFM

class ThreadData
{
    public string s;
    public int n;
};

ThreadData d = new ThreadData();
d.n = 30;
d.s = "Environment";
this.EnvironmentalMonitorThread = new Thread(new ParameterizedThreadStart(this.Begin_Monitoring));
...
this.EnvironmentalMonitorThread.Start(d);


void Begin_Monitoring(object data)
{
    ThreadData d = (ThreadData)data;
    ...
}
AlexFM,

     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.
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.GetHashCode key.
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
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.