Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

CSPServer, State-based Protocol Server Class

, 11 Mar 2003
Class framework for creating client/server protocol servers
cspserver_src.zip
spserver
file_id.diz
SampleServer.dsp
SampleServer.dsw
//***********************************************************************
// (c) Copyright 1999-2003 Santronics Software, Inc. All Rights Reserved.
//***********************************************************************
// File Name : thread.h
// Subsystem : thread class
// Date      : 03/03/2003
// Author    : Hector Santos, Santronics Software, Inc.
// VERSION   : 1.00P
//
// Revision History:
// Version  Date      Author  Comments
// -------  --------  ------  -------------------------------------------
// v1.00P   03/03/03  HLS     Public Release version
//***********************************************************************

#include <afx.h>
#include <process.h>
#include "thread.h"

#define assert ASSERT
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CThread::CThread(DWORD startflags)
{
    ShutdownWaitTime = 1000;
    StartFlags       = startflags;
    TerminateEvent   = CreateEvent(NULL, TRUE, FALSE, NULL);
    ThreadHandle     = NULL;
    tid              = 0;
}

CThread::~CThread()
{
    Stop();
    CloseHandle(TerminateEvent);
    if (ThreadHandle) {
        CloseHandle(ThreadHandle);
        ThreadHandle = NULL;
    }
}

/* virtual */
BOOL CThread::IsTerminated()
{
   return (WaitForSingleObject(TerminateEvent, 0) == WAIT_OBJECT_0);
}


/* virtual */
void CThread::Start()
{
    if (ThreadHandle) {
        ResumeThread(ThreadHandle);
        return;
    }
    ThreadHandle = (HANDLE)_beginthreadex(NULL, 0,
                                          ThreadRoutine,
                                          this,
                                          StartFlags,
                                          (unsigned int *)&tid);
}

/* virtual */
void CThread::Stop()
{
    if (ThreadHandle && GetCurrentThreadId() != tid) {
        InitiateShutdown();
        int ret = WaitForSingleObject(ThreadHandle, ShutdownWaitTime);
        if (ret != WAIT_OBJECT_0) {
            TerminateThread(ThreadHandle, 0);
            CloseHandle(ThreadHandle);
            Beep(800, 100);  // abnormal disconnect
        }
        ThreadHandle = NULL;
    }
}

/* virtual */
unsigned CThread::ThreadRoutine(void *p)
{
    ((CThread *)p)->Go();
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

Share

About the Author

hector santos

United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web03 | 2.8.141029.1 | Last Updated 12 Mar 2003
Article Copyright 2003 by hector santos
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid