Working with Threads

C++ .NET 2005
I used Thread with function of connecting to internet. And at first it was very good. I did so:

using namespace System::Threading;
static void myClass::connectToInet(void){...}
Thread *oThread =
new Thread(new ThreadStart(0, &myClass::connectToInet));

But now I want "myClass::connectToInet" to return an int-variable. I changed the second string to
static int myClass::connectToInet(void){...}

After that I have a mistake and can not compile my program:
'int myClass::connectToInet(void)' : the specified function does not match the delegate type 'void (void)'

What shall I do

So, the question is:
How can I recieve data from Thread

 




Answer this question

Working with Threads

  • MBD-Team

    This is easy. In C++/CLI delegates have a certain signature, and if you want to use a method as a delegate then it needs to match the signature exactly. So changing the thread methods return type to int from void makes the delegate invalid.

    The best way to get around this problem is to use a global variable or a static class member variable to hold the value you wish to return. This can cause syncronization problems though so be careful.



  • jake072

    I think there must be another variant.

  • Sundaraguru

    May be there's another variant to recieve data from thread

  • ksilhol

     crescens2k wrote:

    The best way to get around this problem is to use a global variable or a static class member variable to hold the value you wish to return.

    I had the same idea, but I think it's rather bad thought, because using globals variable for such purpose isn't good.

    I do not know what to do, but if there's no other solution of this problem I will have to use global varibles (as you wrote).



  • Working with Threads