Click here to Skip to main content
Licence 
First Posted 18 Apr 2003
Views 89,678
Bookmarked 23 times

Win32 Worker Thread Wrapper

By | 18 Apr 2003 | Article
Win32 Worker Thread Wrapper

Sample Image - demo.jpg

Introduction

Windows is an OS that does a "great job" of pretending to be a multitasking environment, so it would be a shame, if we programmers would not use this feature at least just for fun. This class should bring you this fun, and maybe (I hope) some knowledge. The class CAG_Thread is just another win32 worker thread wrapper class. Yes, I am sure you could find many other classes that would do the same job just as fine or even better, but what I tried to accomplish is the simplicity. But as we all know the simplicity is subjective, so I will let you judge it for yourself.

Background

Some time ago I noticed that every once in a while I write a worker thread. Each time I need to write one, I copy and paste some functions, change them, and adjust them so they do the new job. After a while I got bored so I decided to write a class that would be very simple and still provide most common features used by me in a worker thread. So the class CAG_Thread was born.

Using the code

The class should be very simple to use. It is intended to be a base class for another class, because it contains the pure virtual function ThreadMain() which must be overridden. Here is the step by step use of the class:

  1. Include AG_Thread.cpp and AG_Thread.h into your project
  2. Derive a new class from CAG_Thread class
  3. Override in the new class the virtual function ThreadMain()
  4. Use the newly created class to instantiate and start the thread by StartThread() function

Here is more detailed explanation with sample code which could be downloaded at the top of the article. I will assume that you should know how to add files to project. Follwoing is the second step of deriving a new class, in my case myThread class from CAG_Thread class. Following is the myThread class definition.

#include "AG_Thread.h" 

class myThread : public CAG_Thread
{
public:
 myThread();
 virtual ~myThread(); 

 // I provide this function to pass any kind of parameters
 BOOL Work(CProgressCtrl *pProgress); 

protected:
 // Progress control to show progress
 CProgressCtrl *m_pProgress; 

 // must override this function
 UINT ThreadMain();
}; 

And here is (partly) the  class implementation

BOOL myThread::Work(CProgressCtrl *pProgress)
{
 if(pProgress == NULL)
  return FALSE; 

 m_pProgress = pProgress; 

 return StartThread();
} 

UINT myThread::ThreadMain()
{
 // init progress control
 m_pProgress->SetRange(0, 100);
 m_pProgress->SetPos(0); 

 for(int i=0; i<101; ++i)
 {
  if(ShouldDie())
   return 1; 

  m_pProgress->SetPos(i);
  Sleep(50);
 } 

 return 0;
}

Now we need to create instance of our class myThread in the dialog box and use it. I simply used a button for starting the thread, but it is not limited to dialog boxes. So here is the function where the starting and ending of the thread occurs.

// check is the thread running and if not then
// start it otherwise abort it
if(m_myThread.GetThreadStatus())
{
 if(m_myThread.StopThread())
 {
  AfxMessageBox(_T("Thread killed successfully!"));
  SetDlgItemText(IDC_START_STOP_BUTTON, _T("Start Thread"));
  return;
 } 

 AfxMessageBox(_T("Could not abort thread."));
}
else
{
 if(m_myThread.Work(&m_progress))
 {
  SetDlgItemText(IDC_START_STOP_BUTTON, _T("Stop Thread"));
  return;
 } 

 AfxMessageBox(_T("Could not start thread."));
}

And that should be it. The demo included is the very basic example of what this class is good for. Please note that this class does not provide any synchronization objects, with the purpose to be as simple as possible. Also the demo is not a useful application, it simply shows the principal of using the class. This article is not very detailed, which again should add to the simplicity, but the code should be documented enough. English is my third language, so if you do not understand something (and experience tells me that you will not), please leave a message and I'll try to answer.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Amer Gerzic

President
Infinity Software Solutions, LLC.
United States United States

Member

Originally from Bosnia and Herzegovina, but lived for 6 years in Germany where I did majority of education, then moved to US, where I live since 1999. I like programming, computers in general, but also Basketball, Soccer, Tennis, and many other things. Masters graduate from Grand Valley State University in CIS and working as a full time software developer. Please visit my website www.amergerzic.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCool stuff PinmemberNedim Sabic23:44 23 Nov '08  
GeneralRe: Cool stuff PinmemberAmer Gerzic2:53 24 Nov '08  
GeneralAn alternative view PinmemberMizan Rahman1:39 21 Jul '08  
GeneralRe: An alternative view PinmemberAmer Gerzic2:33 21 Jul '08  
Questionwhat about function for chagin thread priority Pinmemberwinxalex1:36 24 Sep '03  
AnswerRe: what about function for chagin thread priority PinmemberAmer Gerzic2:22 24 Sep '03  
GeneralRe: what about function for chagin thread priority Pinmemberwinxalex4:32 24 Sep '03  
GeneralRe: what about function for chagin thread priority PinmemberAmer Gerzic7:46 24 Sep '03  
QuestionHow can i check that the thread is done ? PinmemberSickboy10:54 17 Sep '03  
AnswerRe: How can i check that the thread is done ? PinmemberAmer Gerzic1:59 18 Sep '03  
GeneralRe: How can i check that the thread is done ? PinmemberSickboy7:08 18 Sep '03  
GeneralRe: How can i check that the thread is done ? PinmemberAmer Gerzic9:47 18 Sep '03  
GeneralInteresting! PinmemberWREY14:06 30 Apr '03  
GeneralRe: Interesting! PinmemberAmer Gerzic1:54 1 May '03  
GeneralRe: Interesting! PinmemberWREY11:13 1 May '03  
GeneralRe: Interesting! PinmemberAmer Gerzic1:44 2 May '03  
GeneralCThread by Bjarke Viksoe PinsussStanislav Panasik19:02 20 Apr '03  
GeneralRe: CThread by Bjarke Viksoe PinmemberAmer Gerzic2:05 21 Apr '03  
GeneralATL Thread Classes Pinmembermdgray18:28 19 Apr '03  
GeneralRe: ATL Thread Classes PinmemberAmer Gerzic7:25 20 Apr '03  
GeneralRe: ATL Thread Classes Pinmembermdgray13:59 20 Apr '03  
GeneralNice! PinmemberDaniel Lohmann13:36 19 Apr '03  
GeneralRe: Nice! PinmemberAmer Gerzic5:42 20 Apr '03  
GeneralRe: Nice! PinmemberDaniel Lohmann10:54 20 Apr '03  
GeneralRe: Nice! PinmemberAmer Gerzic15:05 20 Apr '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Apr 2003
Article Copyright 2003 by Amer Gerzic
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid