📄 threadsync.cpp
字号:
// ThreadSync.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "ThreadSync.h"
#include <commctrl.h>
#include <sipapi.h>
DWORD WINAPI AThread (PVOID pArg);
DWORD WINAPI BThread (PVOID pArg);
LRESULT CALLBACK About (HWND, UINT, WPARAM, LPARAM);
WCHAR ShareBuffer[20];
HANDLE hBufferMutex=NULL;
HANDLE hATextInputEvent=NULL;
HANDLE hBTextInputEvent=NULL;
HANDLE hAThread=NULL;
HANDLE hBThread=NULL;
DWORD AThreadID=0;
DWORD BThreadID=0;
HWND g_hDlg=NULL;
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
WCHAR str[100];
hBufferMutex = CreateMutex (NULL, NULL, L"BufferMutex");
hATextInputEvent = CreateEvent (NULL, FALSE, FALSE, L"Text Input to A");
hBTextInputEvent = CreateEvent (NULL, FALSE, FALSE, L"Text Input to B");
hAThread = CreateThread (NULL, 0, AThread, 0, 0, &AThreadID);
hBThread = CreateThread (NULL, 0, BThread, 0, 0, &BThreadID);
memset(ShareBuffer,0,sizeof(ShareBuffer));
DialogBox(hInstance, (LPCTSTR)IDD_ABOUTBOX, NULL, (DLGPROC)About);
wsprintf(str,L"the last error is %d",GetLastError());
MessageBox(NULL,str,L"Info",MB_OK);
return 0;
}
// Mesage handler for the About box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
int rc=0;
switch (message)
{
case WM_INITDIALOG:
// Create a Done button and size it.
SetForegroundWindow(hDlg);
g_hDlg=hDlg;
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
if (LOWORD(wParam) == IDC_BUTTON1)
{
rc = WaitForSingleObject (hBufferMutex, INFINITE);
if (rc == WAIT_OBJECT_0)
{
SendDlgItemMessage(hDlg,IDC_EDIT1,WM_GETTEXT,(WPARAM)sizeof(ShareBuffer),(LPARAM)ShareBuffer);
ReleaseMutex(hBufferMutex);
}
else
{
EndDialog(hDlg, -1);
}
SetEvent(hATextInputEvent);
SendDlgItemMessage(hDlg,IDC_EDIT1,WM_SETTEXT,(WPARAM)0,(LPARAM)NULL);
SetFocus(GetDlgItem(hDlg,IDC_EDIT1));
}
if (LOWORD(wParam) == IDC_BUTTON2)
{
rc = WaitForSingleObject (hBufferMutex, INFINITE);
if (rc == WAIT_OBJECT_0)
{
SendDlgItemMessage(hDlg,IDC_EDIT1,WM_GETTEXT,(WPARAM)sizeof(ShareBuffer),(LPARAM)ShareBuffer);
ReleaseMutex(hBufferMutex);
}
else
{
EndDialog(hDlg, -1);
}
SetEvent(hBTextInputEvent);
SendDlgItemMessage(hDlg,IDC_EDIT1,WM_SETTEXT,(WPARAM)0,(LPARAM)NULL);
SetFocus(GetDlgItem(hDlg,IDC_EDIT1));
}
break;
}
return FALSE;
}
DWORD WINAPI AThread (PVOID pArg)
{
WCHAR str[30];
DWORD rc=0;
int index=0;
while(1)
{
rc = WaitForSingleObject (hATextInputEvent, 500);
if (rc == WAIT_OBJECT_0)
{
rc = WaitForSingleObject (hBufferMutex, INFINITE);
if (rc == WAIT_OBJECT_0)
{
wsprintf(str,L"A Input:%s",ShareBuffer);
ReleaseMutex(hBufferMutex);
}
else
ExitThread(-1);
index=SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_INSERTSTRING,(WPARAM)0,(LPARAM)str);
SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_SETTOPINDEX,(WPARAM)index,(LPARAM)0);
}
else
{
wsprintf(str,L"A Input nothing");
index=SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_INSERTSTRING,(WPARAM)0,(LPARAM)str);
SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_SETTOPINDEX,(WPARAM)index,(LPARAM)0);
Sleep(2000);
}
}
return 0;
}
DWORD WINAPI BThread (PVOID pArg)
{
WCHAR str[30];
DWORD rc=0;
int index=0;
while(1)
{
rc = WaitForSingleObject (hBTextInputEvent, 500);
if (rc == WAIT_OBJECT_0)
{
rc = WaitForSingleObject (hBufferMutex, INFINITE);
if (rc == WAIT_OBJECT_0)
{
wsprintf(str,L"B Input:%s",ShareBuffer);
ReleaseMutex(hBufferMutex);
}
else
ExitThread(-1);
index=SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_INSERTSTRING,(WPARAM)0,(LPARAM)str);
SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_SETTOPINDEX,(WPARAM)index,(LPARAM)0);
}
else
{
wsprintf(str,L"B Input nothing");
index=SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_INSERTSTRING,(WPARAM)0,(LPARAM)str);
SendDlgItemMessage(g_hDlg,IDC_LIST1,LB_SETTOPINDEX,(WPARAM)index,(LPARAM)0);
Sleep(2000);
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -