解决方案 »
- 关于:基于http协议大文件断点续传上传至web服务器
- CxImage 加载png图片后,picture控件上图片背景是黑的
- 如何是MFC单文本文档包含WORD的功能~~或者让单文本文档在编辑文字的同时可以插入图片(能复制粘贴)~~
- 急!如果将图片显示在浏览器控件之上?
- 如何为一个程序加入帮助文件?
- 用CreateProcess()函数可以打开*.txt文件吗?
- 在工具栏的按钮右边加文字怎么加??
- ★★★ 请问高手:如何实现想VB一样,刚打开程序........ ★★★
- 【求助】Tree节点+access数据表结构联接设计问题
- 请高手指教怎么把图像读入数据库,再从数据库里读出图像
- 一个很简单但是困扰我很久的 dll 传字符串问题!跪求高手解答!
- mfc文字显示不出
if(nID == 编号)
{
(如果只要触发一次KILLTIMER(编号))
处理内容
}
我在X线程:SetTimer(1,180000,NULL); Y线程:写什么? OnTimer():写什么?
Y线程不是在WaitForSingleObject 等待那个内核互斥变量最后发送D嘛
to tiger9991:
我用了你的方法,不行,"Xdlg->SetTimer(1,180000,NULL);"直接执行后面的断点, 没去OnTimer(UINT nIDEvent),指令没发完就结束了 我估计用SetTimer这个方法:这180秒还是等信号,跟SetWaitableTimer效果差不多 我需要在这180秒发前面的指令,只需要通知另一个线程180秒到了,你觉得自定义消息可以
实现这个功能吗? 还是有其它的办法?谢谢!
我跟你讲的方法,请不要估计,要实践,去MSDN看看这个函数吧
to redeyerabbit: 你能给出一个大概的例子(code)吗?
//#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;
PTP_TIMER g_Timer;
PTP_WORK work1, work2; //2个线程。
void A();
void B();
void C();
void D();
volatile LONG iCount = 0;
HANDLE hEvent = NULL; // FINISH
HANDLE ha, hb, hc, hd;
CRITICAL_SECTION g_cs;
VOID NTAPI thread_x(PTP_CALLBACK_INSTANCE, PVOID, PTP_WORK)
{
A();
C();
}
VOID NTAPI thread_y(PTP_CALLBACK_INSTANCE, PVOID, PTP_WORK)
{
B();
D();
}
VOID NTAPI TimerHandler(PTP_CALLBACK_INSTANCE, PVOID, PTP_TIMER)
{
//InterlockedIncrement(&iCount);
if (iCount < 10)
{
SetEvent(hd);
}
}
void A()
{
WaitForSingleObject(ha, INFINITE);
ResetEvent(ha);
EnterCriticalSection(&g_cs);
cout << "命令A\n";
LeaveCriticalSection(&g_cs); SetEvent(hb);
}
void C()
{
WaitForSingleObject(hc, INFINITE);
ResetEvent(hc);
EnterCriticalSection(&g_cs);
cout << "命令C\n";
LeaveCriticalSection(&g_cs); FILETIME ft;
ft.dwLowDateTime = -3 * 1000; // 8秒,我性子急。
ft.dwHighDateTime = 0;
SetThreadpoolTimer(g_Timer, &ft, 0, 0); //由这个来发信号
}
void B()
{
WaitForSingleObject(hb, INFINITE);
ResetEvent(hb);
EnterCriticalSection(&g_cs);
cout << "命令B\n";
LeaveCriticalSection(&g_cs);
SetEvent(hc);
}
void D()
{
WaitForSingleObject(hd, INFINITE);
ResetEvent(hd);
EnterCriticalSection(&g_cs);
cout << "命令D\n";
cout << "第" << iCount << "计算\n";
LeaveCriticalSection(&g_cs); if (++iCount < 10)
{
SubmitThreadpoolWork(work1);
SubmitThreadpoolWork(work2);
SetEvent(ha);
}
else
{
SetEvent(hEvent);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
InitializeCriticalSection(&g_cs);
ha = CreateEvent(NULL, TRUE, FALSE, NULL);
hb = CreateEvent(NULL, TRUE, FALSE, NULL);
hc = CreateEvent(NULL, TRUE, FALSE, NULL);
hd = CreateEvent(NULL, TRUE, FALSE, NULL); work1 = CreateThreadpoolWork(thread_x, NULL, NULL);
work2 = CreateThreadpoolWork(thread_y, NULL, NULL);
g_Timer = CreateThreadpoolTimer(&TimerHandler, 0, 0);
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); SubmitThreadpoolWork(work1);
SubmitThreadpoolWork(work2); SetEvent(ha);
WaitForSingleObject(hEvent, INFINITE);
DeleteCriticalSection(&g_cs);
cout << "finished.\n";
return 0;
}
// ThreadPool.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include "stdafx.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;
PTP_TIMER g_Timer;
PTP_WORK work1, work2; //2个线程。
void A();
void B();
void C();
void D();
volatile LONG iCount = 0;
HANDLE hEvent = NULL; // FINISH
HANDLE ha, hb, hc, hd;
CRITICAL_SECTION g_cs;
VOID NTAPI thread_x(PTP_CALLBACK_INSTANCE, PVOID, PTP_WORK)
{
A();
C();
}
VOID NTAPI thread_y(PTP_CALLBACK_INSTANCE, PVOID, PTP_WORK)
{
B();
D();
} VOID NTAPI TimerHandler(PTP_CALLBACK_INSTANCE, PVOID, PTP_TIMER)
{
//InterlockedIncrement(&iCount);
if (iCount < 10)
{
SetEvent(hd);
}
}
void A()
{
WaitForSingleObject(ha, INFINITE);
ResetEvent(ha);
EnterCriticalSection(&g_cs);
cout << "命令A\n";
LeaveCriticalSection(&g_cs); SetEvent(hb);
}
void C()
{
WaitForSingleObject(hc, INFINITE);
ResetEvent(hc);
EnterCriticalSection(&g_cs);
cout << "命令C\n";
LeaveCriticalSection(&g_cs); FILETIME ft;
ULARGE_INTEGER ulRelativeTime;
ulRelativeTime.QuadPart = (LONGLONG)-3*10000000;
ft.dwHighDateTime = ulRelativeTime.HighPart;
ft.dwLowDateTime = ulRelativeTime.LowPart;
SetThreadpoolTimer(g_Timer, &ft, 0, 0); //由这个来发信号
}
void B()
{
WaitForSingleObject(hb, INFINITE);
ResetEvent(hb);
EnterCriticalSection(&g_cs);
cout << "命令B\n";
LeaveCriticalSection(&g_cs);
SetEvent(hc);
} void D()
{
WaitForSingleObject(hd, INFINITE);
ResetEvent(hd);
EnterCriticalSection(&g_cs);
cout << "命令D\n";
cout << "第" << iCount << "计算\n";
LeaveCriticalSection(&g_cs); if (++iCount < 10)
{
SubmitThreadpoolWork(work1);
SubmitThreadpoolWork(work2);
SetEvent(ha);
}
else
{
SetEvent(hEvent);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
InitializeCriticalSection(&g_cs);
ha = CreateEvent(NULL, TRUE, FALSE, NULL);
hb = CreateEvent(NULL, TRUE, FALSE, NULL);
hc = CreateEvent(NULL, TRUE, FALSE, NULL);
hd = CreateEvent(NULL, TRUE, FALSE, NULL); work1 = CreateThreadpoolWork(thread_x, NULL, NULL);
work2 = CreateThreadpoolWork(thread_y, NULL, NULL);
g_Timer = CreateThreadpoolTimer(&TimerHandler, 0, 0);
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); SubmitThreadpoolWork(work1);
SubmitThreadpoolWork(work2); SetEvent(ha);
WaitForSingleObject(hEvent, INFINITE);
DeleteCriticalSection(&g_cs);
cout << "finished.\n";
return 0;
}
ULARGE_INTEGER ulRelativeTime;
ulRelativeTime.QuadPart = (LONGLONG)-3*10000000;
ft.dwHighDateTime = ulRelativeTime.HighPart;
ft.dwLowDateTime = ulRelativeTime.LowPart;
SetThreadpoolTimer(g_Timer, &ft, 0, 0); //由这个来发信号把3设置成你需要的180秒就可以了,已经测试通过。