Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++

Switching threads

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
16 Sep 2007CPOL17 min read 64.5K   911   39  
How to switch the thread that a routine is running on.
// ThreadSwitch.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "ThreadSwitch.h"
#include "tswitch.h"

#define MAX_LOADSTRING		100

//
// Global Variables:
//
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

//
// Forward declarations of functions included in this code module:
//
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void				RunThread( void *pv );
int __stdcall		StdcallFunction( HWND hwnd, char ch, int a );
int	__cdecl			CdeclFunction( HWND hwnd, short a, short b );

int APIENTRY _tWinMain( HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
						LPTSTR    lpCmdLine,
						int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	MSG msg;
	HACCEL hAccelTable;

	//
	// Initialize global strings
	//
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_THREADSWITCH, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	//
	// Perform application initialization:
	//
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_THREADSWITCH));

	//
	// Main message loop:
	//
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_THREADSWITCH));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)CreateSolidBrush( RGB( 240, 240, 240 ) );
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_THREADSWITCH);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   //
   // Store instance handle in our global variable
   //
   hInst = hInstance; 

   SIZE sizeScreen = { GetSystemMetrics( SM_CXSCREEN ),
	   GetSystemMetrics( SM_CYSCREEN ) };
   SIZE sizeWin = { 320, 240 };

   hWnd = CreateWindow( szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      ( sizeScreen.cx - sizeWin.cx ) / 2,
	  ( sizeScreen.cy - sizeWin.cy ) / 2,
	  sizeWin.cx,
	  sizeWin.cy,
	  NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int					wmId, wmEvent;
	PAINTSTRUCT			ps;
	HDC					hdc;
	RECT				rcClient;
	static const TCHAR	szText[] = _T( "Click anywhere in the window to exercise thread switching." );

	//
	// process WM_INVOKE; this cannot be a case statement because
	// WM_INVOKE is not a constant
	//
	if( message == WM_INVOKE )
	{
		THREAD_SWITCH_INVOKE(wParam);
	}

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);

		//
		// Parse the menu selections:
		//
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);

		GetClientRect( hWnd, &rcClient );
		SetBkMode( ps.hdc, TRANSPARENT );

		DrawText( ps.hdc, szText, ( sizeof( szText ) / sizeof( TCHAR ) ) - 1,
				  &rcClient,
				  DT_CENTER | DT_WORDBREAK );

		EndPaint(hWnd, &ps);
		break;

	case WM_LBUTTONDOWN:
		{
			_beginthread( RunThread, 0, hWnd );
			break;
		};

	case WM_DESTROY:
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;
}

int __stdcall StdcallFunction( HWND hwnd, char ch, int a )
{
	THREAD_SWITCH_WITH_RETURN(hwnd, StdcallFunction, 3, ccStdcall, int)

	TCHAR szBuf[1024];
	wsprintf( szBuf, _T( "Thread ID = %d, hwnd = 0x%X, ch = %c, a = %d\n" ),
		GetCurrentThreadId(), (DWORD_PTR)hwnd, ch, a );
	MessageBox( hwnd, szBuf, _T( "ThreadSwitch - STDCALL" ), MB_OK | MB_ICONINFORMATION );

	return 5;
}

int __cdecl CdeclFunction( HWND hwnd, short a, short b )
{
	THREAD_SWITCH_WITH_RETURN(hwnd, CdeclFunction, 3, ccCdecl, int)

	TCHAR szBuf[1024];
	wsprintf( szBuf, _T( "Thread ID = %d, hwnd = 0x%X, a = %d, b = %d\n" ),
		GetCurrentThreadId(), (DWORD_PTR)hwnd, a, b );
	MessageBox( hwnd, szBuf, _T( "ThreadSwitch - CDECL" ), MB_OK | MB_ICONINFORMATION );

	return 10;
}

void RunThread( void *pv )
{
	int result;

	result = StdcallFunction( (HWND)pv, 'c', 20 );
	_ASSERT( result == 5 );

	result = CdeclFunction( (HWND)pv, 10, 20 );
	_ASSERT( result == 10 );
}

//
// Message handler for about box.
//
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Microsoft
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions