本帖最后由 chenquangobeijing 于 2012-09-18 21:43:00 编辑

解决方案 »

  1.   

    这个貌似是很久之前的一个帖子的延续吧SetTimer(编号,时间(MS单位),NULL) //NULL直接回调OnTimerOnTimer中
    if(nID == 编号)
    {
       (如果只要触发一次KILLTIMER(编号))
       处理内容
    }
      

  2.   


      我在X线程:SetTimer(1,180000,NULL);  Y线程:写什么?  OnTimer():写什么?
      

  3.   

    OnTimer(): SetEvent那个内核互斥变量
    Y线程不是在WaitForSingleObject   等待那个内核互斥变量最后发送D嘛
      

  4.   


      to tiger9991:
      我用了你的方法,不行,"Xdlg->SetTimer(1,180000,NULL);"直接执行后面的断点,  没去OnTimer(UINT nIDEvent),指令没发完就结束了  我估计用SetTimer这个方法:这180秒还是等信号,跟SetWaitableTimer效果差不多  我需要在这180秒发前面的指令,只需要通知另一个线程180秒到了,你觉得自定义消息可以
        实现这个功能吗?
      还是有其它的办法?谢谢! 
      

  5.   

    我只讲了核心函数,内部逻辑你自己要整理了方法肯定没问题的。“我估计用SetTimer这个方法:这180秒还是等信号,跟SetWaitableTimer效果差不多”
    我跟你讲的方法,请不要估计,要实践,去MSDN看看这个函数吧
      

  6.   


     to redeyerabbit:  你能给出一个大概的例子(code)吗? 
      

  7.   

    // 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;  
        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;  
    }  
      

  8.   

    上面的时间设置的不对,重新修正为:
    // 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;  
    }  
      

  9.   

    FILETIME ft;   
    ULARGE_INTEGER ulRelativeTime;
     ulRelativeTime.QuadPart = (LONGLONG)-3*10000000;
     ft.dwHighDateTime = ulRelativeTime.HighPart;
     ft.dwLowDateTime = ulRelativeTime.LowPart;
     SetThreadpoolTimer(g_Timer, &ft, 0, 0); //由这个来发信号把3设置成你需要的180秒就可以了,已经测试通过。