Passing parameters to a Thread'ed function 
Author Message
 Passing parameters to a Thread'ed function

...
Network net = new Network();
System.Threading.Thread thread = new System.Threading.Thread(new
System.Threading.ThreadStart(net.networkscan(txtIP2.Text, txtSN.Text, listBox1,
txtIP2)));
System.Threading.ThreadStart threadstart = new
System.Threading.ThreadStart(net.networkscan(txtIP2.Text, txtSN.Text, listBox1,
txtIP2));
System.Threading.Thread thread = new System.Threading.Thread(threadstart);

thread.Start();
...

networkscan takes 2 strings and 2 controls parameters, but when I do it the
above way, I get "Method Name Expected" error.

But how do I do it, so I can pass parameters to the functions, or would I be
better off using a Property/Get/Set, keeping in mind that listBox1 and txtIP2
need to be updated on the fly.



Sun, 28 Nov 2004 08:50:27 GMT  
 Passing parameters to a Thread'ed function
Try looking into the SetData/GetData in order to set information in TLS
slots (Thread Local Storage).

--
Justin Rogers
DigiTec Web Consultants, LLC


Quote:
> ...
> Network net = new Network();
> System.Threading.Thread thread = new System.Threading.Thread(new
> System.Threading.ThreadStart(net.networkscan(txtIP2.Text, txtSN.Text,
listBox1,
> txtIP2)));
> System.Threading.ThreadStart threadstart = new
> System.Threading.ThreadStart(net.networkscan(txtIP2.Text, txtSN.Text,
listBox1,
> txtIP2));
> System.Threading.Thread thread = new System.Threading.Thread(threadstart);

> thread.Start();
> ...

> networkscan takes 2 strings and 2 controls parameters, but when I do it
the
> above way, I get "Method Name Expected" error.

> But how do I do it, so I can pass parameters to the functions, or would I
be
> better off using a Property/Get/Set, keeping in mind that listBox1 and
txtIP2
> need to be updated on the fly.



Sun, 28 Nov 2004 15:03:34 GMT  
 Passing parameters to a Thread'ed function

Quote:
> Try looking into the SetData/GetData in order to set information in TLS
> slots (Thread Local Storage).

After thinking some more, I decided to use properties - it seems to work the way
I currently need.

Thanks, I will look it up anyway.



Sun, 28 Nov 2004 16:30:42 GMT  
 Passing parameters to a Thread'ed function
Wayne,

    This is the way to go.  Using TLS will NOT work in this situation (nor
is it related, actually).

    Basically, when you pass a parameter to the ThreadStart, you are passing
a function.  ThreadStart is a delegate, which is a type-safe wrapper for
instance and static methods.  The reason why compilation fails is that you
are passing parameters, when it only wants the method itself.  It is not
being called at this point, but will be called later (when the thread is
actually started).

    What you did with the properties in the class is the correct way to
handle passing parameters to threads.

    Hope this helps.

--
               - Nicholas Paldino [.NET MVP]


Quote:


> > Try looking into the SetData/GetData in order to set information in TLS
> > slots (Thread Local Storage).

> After thinking some more, I decided to use properties - it seems to work
the way
> I currently need.

> Thanks, I will look it up anyway.



Sun, 28 Nov 2004 20:56:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Using calloc'ed data in threads causes access violation

2. Passing C++ Class Member Function to as a C Callback Function Parameter

3. How to pass parameters represented by '...'?

4. Dynamic memory malloc'ed in a function.

5. Passing parameters to a new thread

6. HANDLES passed as parameters to threads

7. multithreading: passing parameter from the main to the thread

8. more passing multiple parameters to _begin thread

9. How to pass parameters to worker thread??

10. How to pass parameters to worker thread??

11. function parameters and return values aren't changed by function call

12. Passing parameters into a user-interface thread???

 

 
Powered by phpBB® Forum Software