C ++ WinForms参数通过System :: Threading :: ParameterizedThreadStart

时间:2013-09-16 20:36:49

标签: c++ multithreading winforms

我必须创建新线程并将参数发送到线程函数,但不能使它工作。 我按照这个参考文献工作:http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart.aspx

这是线程创建(注释中有错误):

System::Threading::Thread^ T = gcnew System::Threading::Thread(gcnew System::Threading::ParameterizedThreadStart(this, &Server::ClientHandler)); // ERROR: 'void Server::ClientHandler(System::Object ^,System::Object ^)' : the specified function does not match the delegate type 'void (System::Object ^)'
T->Start(ClientSocket); // ERROR: 'System::Threading::Thread::Start' : no overloaded function takes 2 arguments

以下是ClientHandler decleration:

void Server::ClientHandler(Object^ data, Object^ data1);

我只使用一个参数尝试了它,而且我只有第二个错误。

P.S,在ClientHandler函数中,我必须将Object^参数转换为SOCKET*SOCKADDR_IN*,如何才能完成?

我的尝试:

SOCKET* _Sock = (SOCKET*)data;
SOCKADDR_IN* _ADDR = (SOCKADDR_IN*)data1;

我正在使用visual studio 2012。

1 个答案:

答案 0 :(得分:2)

非常确定Server::ClientHandler的声明不正确。

应该是:

void Server::ClientHandler(Object^ data)
{
    //Do stuff with data here..
}

System::Threading::Thread^ T = gcnew System::Threading::Thread(gcnew System::Threading::ParameterizedThreadStart(this, &Server::ClientHandler));
T->Start("Pass Your Data Here");