wince同步机制示例 飞凌视频

/**************************.h***************************/

#include "resource.h"

#define dim(x) (sizeof(x)/sizeof(x[0]))

struct DecodeUnit{
UINT message;
LONG (*fun)(HWND,UINT,WPARAM,LPARAM);
};

typedef struct _Data{
TCHAR name[20];
int age;
} DATA;

INT_PTR CALLBACK DlgPorc(HWND,UINT,WPARAM,LPARAM);

LONG DoInit(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
LONG DoCommand(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
LONG OnClose(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);
LONG OnBtnClick(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam);

DWORD WINAPI WriteThread(LPVOID lpVoid);
DWORD WINAPI ReadThread(LPVOID lpVoid);

/**************************.cpp******************************************/

#include <windows.h>

#include <windowsx.h>
#include <CommCtrl.h>
#include "wince 同步机制.h"
#include <winuser.h>

struct DecodeUnit DlgMessage[] ={
WM_INITDIALOG ,DoInit,
WM_COMMAND,DoCommand,
};

struct DecodeUnit CommandMessage[] = {
IDCANCEL,OnClose,
IDOK,OnBtnClick,
};

DATA data;

HINSTANCE g_hInst;

HANDLE hWriteThread;
HANDLE hReadThread;

//CRITICAL_SECTION m_section;
//HANDLE m_mutex;
//HANDLE m_semaphore;
HANDLE m_EVENT;
int WINAPI WinMain(HINSTANCE hInstance,
  HINSTANCE hPrevInstance, 
  LPTSTR    lpCmdLine,
  int       nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,DlgPorc);
CommandBar_Show(NULL, TRUE);
return (0);
}

INT_PTR CALLBACK DlgPorc(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i;
for (i = 0;i < dim(DlgMessage);i++)
{
if(DlgMessage[i].message == message)
(*DlgMessage[i].fun)(hDlg,message,wParam,lParam);
}
return FALSE;
}

LONG DoInit(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
// InitializeCriticalSection(&m_section);
// m_mutex = CreateMutex(NULL,FALSE,NULL);//记:第二个参数如果为true,程序不运行。第三参数本程序无用
// m_semaphore = CreateSemaphore(NULL,1,1,NULL);//记:第2个参数至少为1才能运行。
m_EVENT = CreateEvent(NULL,FALSE,TRUE,NULL);
return TRUE;
}

LONG DoCommand(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
int i ;
for(i =0 ;i< dim(CommandMessage);++i)
{
if(CommandMessage[i].message == LOWORD(wParam))
(*CommandMessage[i].fun)(hDlg,message,wParam,lParam);
}
return TRUE;
}

LONG OnClose(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
//DeleteCriticalSection(&m_section);
//CloseHandle(m_mutex);
//CloseHandle(m_semaphore);
CloseHandle(m_EVENT);
CloseHandle(hWriteThread);
CloseHandle(hReadThread);
EndDialog(hDlg,0);
return TRUE;
}

LONG OnBtnClick(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
{
DWORD dwThread1,dwTread2;
HWND hWriteList = GetDlgItem(hDlg,IDC_LIST1);
hWriteThread = CreateThread(NULL,0,WriteThread,LPVOID(hWriteList),0,&dwThread1);

HWND hReadList = GetDlgItem(hDlg,IDC_LIST2);
hReadThread = CreateThread(NULL,0,ReadThread,LPVOID(hReadList),0,&dwTread2);
return 0;
}

DWORD WINAPI WriteThread(LPVOID lpVoid)
{
int i ;
HWND hList = (HWND)lpVoid;

TCHAR buf[64];


while(!ListBox_DeleteString(hList,0))
continue;
for(i= 0;i < 20;i++)
{
//EnterCriticalSection(&m_section);

if (WaitForSingleObject(m_EVENT,-1)== WAIT_OBJECT_0)
{
if (i%2)
{
wcscpy( data.name,TEXT("kevin"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hList,buf);
Sleep(30);
data.age = 20;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hList,buf);
}
else
{
wcscpy( data.name,TEXT("susan"));
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hList,buf);
Sleep(20);
data.age = 22;
wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hList,buf);
}
//LeaveCriticalSection(&m_section);
}
//ReleaseSemaphore(m_semaphore,1,NULL);
SetEvent(m_EVENT);
}
return 0;
}
DWORD WINAPI ReadThread(LPVOID lpVoid)
{
int i ;
HWND hList = (HWND)lpVoid;
TCHAR buf[64];

while(!ListBox_DeleteString(hList,0))
continue;
for (i = 0;i < 20;i++)
{
//EnterCriticalSection(&m_section);
if (WaitForSingleObject(m_EVENT,-1) == WAIT_OBJECT_0)
{
wsprintf(buf,TEXT("name is %s"),data.name);
ListBox_AddString(hList,buf);

wsprintf(buf,TEXT("age is %d"),data.age);
ListBox_AddString(hList,buf);
//LeaveCriticalSection(&m_section);
}
//ReleaseSemaphore(m_semaphore,1,NULL);
SetEvent(m_EVENT);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值