Discussion:
Worker thread
(too old to reply)
Me
2012-03-08 16:18:58 UTC
Permalink
I am experimenting with worker threads and seeing id I can start/stop it
from running
With the code below the worker thread does start and stop with the buttons.
BUT, when you press the stop button the thread still completes the full
cycle before exiting.
How do I get the thread to stop immediatly with the stop button?
I pasted the main .h and .cpp for review.
Also how do I pass a unsigned char array into the thread for processing?
How do I return the processed unsigned char array back to the main Dlg?
Thanks


// AdelDlg.h : header file

#if
!defined(AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_)
#define AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_

#define RunMsg (WM_APP + 1)

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CAdelDlg dialog

class CAdelDlg : public CDialog
{
// Construction
public:
CAdelDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CAdelDlg)
enum { IDD = IDD_ADEL_DIALOG };
CStatic c_Info;
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAdelDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

afx_msg LRESULT ShowMessage(UINT wParam, LONG lParam);

// Implementation

protected:
HICON m_hIcon;

// Generated message map functions
//{{AFX_MSG(CAdelDlg)
virtual BOOL OnInitDialog();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.

#endif //
!defined(AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_)


==================================================================================================
==================================================================================================


// AdelDlg.cpp : implementation file

#include "stdafx.h"
#include "Adel.h"
#include "AdelDlg.h"
#include "io.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

int MessCode;
volatile bool KillThread;
UINT runThread( LPVOID Param );


/////////////////////////////////////////////////////////////////////////////
// CAdelDlg dialog

CAdelDlg::CAdelDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdelDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdelDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAdelDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdelDlg)
DDX_Control(pDX, IDC_INFO, c_Info);
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAdelDlg, CDialog)
//{{AFX_MSG_MAP(CAdelDlg)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TEST, OnTest)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(RunMsg, ShowMessage)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdelDlg message handlers

BOOL CAdelDlg::OnInitDialog()
{
CDialog::OnInitDialog();

SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

return TRUE;
}

void CAdelDlg::OnTest() //button to start thread
{
KillThread = false;
AfxBeginThread(runThread,(LPVOID)m_hWnd);
c_Info.SetWindowText("Thread Started...");
}

void CAdelXDlg::OnStop() //button to stop thread
{
KillThread = true;
}

LRESULT CAdelDlg::ShowMessage(UINT wParam, LONG lParam) //display message
depending on code from thread
{
if(MessCode == 0)
c_Info.SetWindowText("MessageCode = 0");
if(MessCode == 1)
c_Info.SetWindowText("MessageCode = 1");
if(MessCode == 2)
c_Info.SetWindowText("MessageCode = 2");
if(MessCode == 3)
c_Info.SetWindowText("MessageCode = 3");
return 0;
}

UINT runThread( LPVOID Param ) //test thread just sends a message
each second until stopped
{
HWND hDlg = (HWND)Param;

KillThread = false;
do{
MessCode = 0;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 1;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 2;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 3;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
}while(!KillThread);

return 0;
}
Bill Snyder
2012-03-08 16:42:48 UTC
Permalink
Post by Me
I am experimenting with worker threads and seeing id I can start/stop it
from running
With the code below the worker thread does start and stop with the buttons.
BUT, when you press the stop button the thread still completes the full
cycle before exiting.
How do I get the thread to stop immediatly with the stop button?
I pasted the main .h and .cpp for review.
Also how do I pass a unsigned char array into the thread for processing?
How do I return the processed unsigned char array back to the main Dlg?
Thanks
// AdelDlg.h : header file
#if
!defined(AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_)
#define AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_
#define RunMsg (WM_APP + 1)
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CAdelDlg dialog
class CAdelDlg : public CDialog
{
// Construction
CAdelDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CAdelDlg)
enum { IDD = IDD_ADEL_DIALOG };
CStatic c_Info;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAdelDlg)
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
afx_msg LRESULT ShowMessage(UINT wParam, LONG lParam);
// Implementation
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CAdelDlg)
virtual BOOL OnInitDialog();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately
before the previous line.
#endif //
!defined(AFX_ADELDLG_H__CB0DCF35_E38B_4013_B978_A696202F8736__INCLUDED_)
==================================================================================================
==================================================================================================
// AdelDlg.cpp : implementation file
#include "stdafx.h"
#include "Adel.h"
#include "AdelDlg.h"
#include "io.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int MessCode;
volatile bool KillThread;
UINT runThread( LPVOID Param );
/////////////////////////////////////////////////////////////////////////////
// CAdelDlg dialog
CAdelDlg::CAdelDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAdelDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAdelDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAdelDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAdelDlg)
DDX_Control(pDX, IDC_INFO, c_Info);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAdelDlg, CDialog)
//{{AFX_MSG_MAP(CAdelDlg)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TEST, OnTest)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_WM_PAINT()
//}}AFX_MSG_MAP
ON_MESSAGE(RunMsg, ShowMessage)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAdelDlg message handlers
BOOL CAdelDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
return TRUE;
}
void CAdelDlg::OnTest() //button to start thread
{
KillThread = false;
AfxBeginThread(runThread,(LPVOID)m_hWnd);
c_Info.SetWindowText("Thread Started...");
}
void CAdelXDlg::OnStop() //button to stop thread
{
KillThread = true;
}
LRESULT CAdelDlg::ShowMessage(UINT wParam, LONG lParam) //display message
depending on code from thread
{
if(MessCode == 0)
c_Info.SetWindowText("MessageCode = 0");
if(MessCode == 1)
c_Info.SetWindowText("MessageCode = 1");
if(MessCode == 2)
c_Info.SetWindowText("MessageCode = 2");
if(MessCode == 3)
c_Info.SetWindowText("MessageCode = 3");
return 0;
}
UINT runThread( LPVOID Param ) //test thread just sends a message
each second until stopped
{
HWND hDlg = (HWND)Param;
KillThread = false;
do{
MessCode = 0;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 1;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 2;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
MessCode = 3;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
Sleep(1000);
}while(!KillThread);
return 0;
}
Why aren't you sending MsgCode as one of the message parameters?

Anyway, you can make KillThread a semaphore rather than a boolean,
do a ReleaseSemaphore() to kill the worker thread, and re-code the
worker thread itself something like this:

UINT runThread( LPVOID Param )
{
HWND hDlg = (HWND)Param ;
int msgnum = -1 ;

do{
if( ++msgnum > 3 )
msgnum = 0 ;
MessCode = msgnum ;
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
} while( WaitForSingleObject( KillThread,1000 )==WAIT_TIMEOUT)

return 0;
}
--
Bill Snyder [This space unintentionally left blank]
s***@mvps.org
2012-03-08 16:58:04 UTC
Permalink
There is not any safe and clean way to stop the thread immediately. The SuspendThread and TerminateThread APIs can do it, but are not recommended for routine use. The best way to stop the thread quickly is to eliminate the Sleep() calls in the thread. You have the option of making it a message-driven thread and using a timer (WM_TIMER) instead of Sleep() calls. Another option is to use an event instead of a bool to stop the thread. The thread can use WaitForSingleObject to suspend itself until the event is set OR a timeout elapses.

When starting the thread you can pass a pointer (where you now pass m_hWnd). That could be a pointer to anything. A pointer to a struct would let you pass a collection of things, or maybe just pass the 'this' pointer so the thread can access dialog member variables.

When the thread does PostMessage you can pass a pointer in wParam and/or lParam. For example, the thread could allocate a CString or char array from the heap and pass the pointer. The main thread message handler must delete the pointer after receiving it.
Me
2012-03-08 19:31:53 UTC
Permalink
Post by s***@mvps.org
There is not any safe and clean way to stop the thread immediately. The SuspendThread and TerminateThread APIs can do it, but are not recommended for routine use. The best way to stop the thread quickly is to eliminate the Sleep() calls in the thread. You have the option of making it a message-driven thread and using a timer (WM_TIMER) instead of Sleep() calls. Another option is to use an event instead of a bool to stop the thread. The thread can use WaitForSingleObject to suspend itself until the event is set OR a timeout elapses.
When starting the thread you can pass a pointer (where you now pass m_hWnd). That could be a pointer to anything. A pointer to a struct would let you pass a collection of things, or maybe just pass the 'this' pointer so the thread can access dialog member variables.
When the thread does PostMessage you can pass a pointer in wParam and/or lParam. For example, the thread could allocate a CString or char array from the heap and pass the pointer. The main thread message handler must delete the pointer after receiving it.
//I have in header unsigned char MyData[32];
//I want the data in this array to be able to be used within the thread and
//then returned with the modified data.
//I have this now
void CAdelphiXDlg::OnTest()
{
AfxBeginThread(runThread,(LPVOID)m_hWnd); //works
c_Info.SetWindowText("Thread Started...");
}
//How do I change it to pass the that unsigned char array?

===========================================================================

//how do I retrieve the unsigned char array in the thread below?
UINT runThread( LPVOID Param )
{
HWND hDlg = (HWND)Param;

KillThread = false;
do{
MessCode = 0; //started
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);

//do code manipulation which can take up to 30secs
//which is why I might need to kill if stuck

MessCode = 1; //finished
::PostMessage(hDlg, RunMsg, (WPARAM)0, (LPARAM)0);
}while(!KillThread);

return 0;
}

Loading...