Click to See Complete Forum and Search --> : Threading


sivaprakash.shanmugam
May 30th, 2006, 06:41 AM
Whats wrong with the Threading statements which is no allowing me to create the Thread

System::Threading::Thread *backgroundThread = new System::Threading::Thread (new System::Threading::ThreadStart (SocketConnection,0) );
backgroundThread.Start();


void TCPIP::SocketConnection()
{
}

Error Message:
error C3364: 'System::Threading::ThreadStart' : invalid second argument for delegate constructor; needs to be a pointer to a member function


How it should be rewritten.

ThermoSight
May 30th, 2006, 07:06 PM
The compiler appears to me to be pointing out that you may have the arguments reversed (if your method is a static method) or improperly declared.

The first argument in the Threadstart should be an Object *, typically though not always the 'this' pointer. The second argument is a pointer to the method to be called. Indeed, I believe the IDE informs you of the arguments required when you type the opening paren of the ThreadStart declaration. It's telling you what the arguments should be.


For example, the following works for me .....
pThread = new Thread(new ThreadStart(this, FunctionalOversight));

In the statement above, the delegate points to method 'FunctionalOversight' in the current class (hence the 'this' pointer).


an alternate declaration wherein we're not working with the current class is
Thread1 = new Thread( new ThreadStart(print1, &MessagePrinter::Printjob);)

In the example above, 'Print1' is a pointer to the MessagePrinter class instantiation(i.e MessagePrinter *print1;) and 'PrintJob' is a method in class 'MessagePrinter'.

Note that the first argument, the Object *, may (must?) be a zero IF the method to be called is a static method.

- with thanks to Deitel's "VISUAL C++ .NET - How To Program" - a Dyn-O-Mite book!


Best wishes,

bill