⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threads.c

📁 Threads-multi threading
💻 C
字号:
#include	"threads.h"


int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
    WNDCLASSEX wc      = {0};
    MSG Msg            = {0};
	char szClassName[] = "ColorsX";

    wc.cbSize        = sizeof( WNDCLASSEX );
    wc.style         = 0;
    wc.lpfnWndProc   = MainWndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor( NULL, IDC_ARROW );
    wc.hbrBackground = ( HBRUSH ) CreateSolidBrush( GetSysColor( COLOR_3DFACE ) );
    wc.lpszMenuName  = 0;
    wc.lpszClassName = szClassName;
    wc.hIconSm       = NULL;

    if( !RegisterClassEx( &wc ) )
    {
        MessageBox( NULL, "Window Registration Failed!", "Error!", MB_OK | MB_ICONEXCLAMATION );
        return 0;
    }

    hWnd = CreateWindow( szClassName, "Threads", WS_SYSMENU | WS_MINIMIZEBOX,
											     0, 0, 216, 200, NULL, NULL,
												 hInstance, NULL );
	
    if(!hWnd)
    {
        MessageBox( NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK );
        return 0;
    }
		
	ShowWindow( hWnd, nCmdShow );
    UpdateWindow( hWnd );

    while( GetMessage( &Msg, NULL, 0, 0 ) > 0 )
    {
        TranslateMessage( &Msg );
        DispatchMessage( &Msg );
    }
    return Msg.wParam;
}

// Main Window Procedure
LRESULT CALLBACK MainWndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam )
{
	switch( Msg )
	{
	case WM_CREATE: return OnCreate( hWnd ); break;
	case WM_DESTROY: OnDestroy( hWnd ); break;
	case WM_COMMAND:
		switch( LOWORD( wParam ) )
		{
		case IDBEGIN: return OnBegin( hWnd ); break;
		case IDSTOP: return OnStop( ); break;
		}
		break;
	break;

	}
	return DefWindowProc( hWnd, Msg, wParam, lParam );
}

// OnCreate
BOOL OnCreate( HWND hWnd )
{
	HWND hEdit1 = 0;
	HWND hEdit2 = 0;
	HWND hBegin = 0;
	HWND hStop  = 0;
	
	// Create First Edit Control
	hEdit1 = CreateWindow( "EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER,
														  10, 10, 80, 25,
												    hWnd, (HMENU)IDEDIT1, 
										   GetModuleHandle(NULL), NULL );
																 
	if( !hEdit1 )  // Check to see if hEdit1 is valid
	{
		MessageBox( hWnd, "Error While Creating Edit Control!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}
	
	// Create Second Edit Control
	hEdit2 = CreateWindow( "EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER, 
		                                                 120, 10, 80, 25,
												    hWnd, (HMENU)IDEDIT2, 
										   GetModuleHandle(NULL), NULL );
												   
	if( !hEdit2 ) // Check to see if hEdit2 is valid
	{
		MessageBox( hWnd, "Error While Creating Edit Control!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}

	// Create Begin Button
	hBegin = CreateWindow( "BUTTON", "Begin", WS_VISIBLE | WS_CHILD,
													 10, 50, 80, 25,
											   hWnd, (HMENU)IDBEGIN,
									  GetModuleHandle(NULL), NULL );
	if( !hBegin ) // Check to see if hBegin is valid
	{
		MessageBox( hWnd, "Error While Creating Button Control!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}

	// Create Stop Button
	hStop = CreateWindow( "BUTTON", "Stop", WS_VISIBLE | WS_CHILD,
													120, 50, 80, 25,
											    hWnd, (HMENU)IDSTOP,
									  GetModuleHandle(NULL), NULL );

	if( !hStop ) // Check to see if hStop is valid
	{
		MessageBox( hWnd, "Error While Creating Button Control!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}

	return TRUE;
}

// OnDestroy
void OnDestroy( HWND hWnd )
{
	DestroyWindow( hWnd );
	PostQuitMessage( 0 );
}

BOOL OnBegin( HWND hWnd )
{
	SECURITY_ATTRIBUTES sa = {0};
	DWORD dwThreadID1 = 0;			// Thread1 ID
	DWORD dwThreadID2 = 0;			// Thread2 ID
	
	sa.nLength = sizeof( SECURITY_ATTRIBUTES );

	hThread1 = CreateThread( &sa, 0, ThreadProc1, ( LPVOID )hWnd, 0, &dwThreadID1 );
	
	if( !hThread1 )	// Check to see if Thread1 is valid
	{
		MessageBox( hWnd, "Creation of First Thread Failed!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}

	hThread2 = CreateThread( &sa, 0, ThreadProc2, ( LPVOID )hWnd, 0, &dwThreadID2 );

	if( !hThread2 )	// Check to see if Thread2 is valid
	{
		MessageBox( hWnd, "Creation of Second Thread Failed!", "Error", MB_OK | MB_ICONEXCLAMATION );
		return FALSE;
	}
	
	return TRUE;
}

BOOL OnStop( )
{
	if( iState == 0 )
	{
		if( SuspendThread( hThread1 ) == -1 )	// Make sure Thread1 isnt' already Suspended
		{
			return FALSE;
		}

		if( SuspendThread( hThread2 ) == -1 )	// Make sure Thread2 isn't already Suspended
		{
			return FALSE;
		}
		
		SetWindowText( GetDlgItem( hWnd, IDSTOP ), "Resume" );
		iState = 1;

		return TRUE;
	}
	
	if( iState == 1 )
	{
		if( ResumeThread( hThread1 ) == -1 )	// Make sure Thread1 hasn't Resumed
		{
			return FALSE;
		}
		
		if( ResumeThread( hThread2 ) == -1 )	// Make sure Thread2 hasn't Resumed
		{
			return FALSE;
		}
		
		SetWindowText( GetDlgItem( hWnd, IDSTOP ), "Stop" );
		iState = 0;

		return TRUE;
	}
	
	return TRUE;
}

// Thread 1 Execution
BOOL WINAPI ThreadProc1( LPVOID hWnd )
{
	int iCount			 = 0;
	char szCount[6]  = {0};

	while( iCount != 10000 )
	{
		iCount++;
		wsprintf( szCount, "%d", iCount );
		SetWindowText( GetDlgItem( hWnd, IDEDIT1 ), szCount );
	}
	
	ExitThread( 0 );	// Exit Thread1

	return 1;
}

// Thread 2 Execution
BOOL WINAPI ThreadProc2( LPVOID hWnd )
{
	int iCount			 = 10000;
	char szCount[6]  = {0};
	LPDWORD	lpdwExitCode = 0;

	while( iCount != 0 )
	{
		iCount--;
		wsprintf( szCount, "%d", iCount );
		SetWindowText( GetDlgItem( hWnd, IDEDIT2 ), szCount );
	}
	
	ExitThread( 0 );	// Exit Thread2
	return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -