Now in click of stop button ,how to stop this running thread.
SOLUTION:
Step 1. Declared these on header file.
#include <Windows.h>
HANDLE m_Thread_handle;
CWinThread *cTh;
static UINT __cdecl StaticThreadFunc(LPVOID pParam);
UINT ThreadFunc();
Step 2. Starting the thread from a function or button click ---
void CServerDlg::OnBnClickedStart()Step 3. Thread initialization function --
{
// TODO: Add your control notification handler code here
cTh = AfxBeginThread(
StaticThreadFunc,
this);
m_Thread_handle = cTh->m_hThread;
}
UINT __cdecl CServerDlg::StaticThreadFunc(LPVOID pParam)Step 4. Work processing function for thread --
{
CServerDlg *pYourClass = reinterpret_cast<CServerDlg*>(pParam);
UINT retCode = pYourClass->ThreadFunc();
return retCode;
}
UINT CServerDlg::ThreadFunc()// while loop on start listening that are blocking call
{
// Do your thing, this thread now has access to all the classes member variables
CString txtname;
GetDlgItemText(IDC_EDIT1, txtname);
int iPort = _wtoi( txtname.GetString() );
m_pServer = new ServerManager(this);
m_pServer->StartListening(iPort);
return 0;Step 5. Now to close the thread from clicking stop button ---
}
void CServerDlg::OnBnClickedStop()
{
// TODO: Add your control notification handler code here
CloseHandle(m_Thread_handle);
delete m_pServer;
}
No comments:
Post a Comment