Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Системное программирование и WinAPI > _tWinMain(): организовать запуск и работу таймера


Автор: malor 7.8.2011, 20:10
Вот так таймер работает:
Код
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int cmdShow)
{
    SetTimer(NULL, NULL, MY_TIME_PERIOD, (TIMERPROC)MyTimerProc);
    MessageCycle():

    return 0;
}

VOID CALLBACK MyTimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwtime)
{
    i++;
}

void MessageCycle()
{
    MSG msg;
    BOOL bRet; 
    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error anpossibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 
    } 
}

А вот так нет:
Код
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int cmdShow)
{
    SetTimer(NULL, NULL, MY_TIME_PERIOD, (TIMERPROC)MyTimerProc);
  
    Cycle();

    return 0;
}

void Cycle()
{
    HANDLE hDir = CreateFile(_T(pathDir),                        // pointer to the file name
                            FILE_LIST_DIRECTORY,                // access (read/write) mode
                            FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,  // share mode
                            NULL,                               // security descriptor
                            OPEN_EXISTING,                      // how to create
                            FILE_FLAG_BACKUP_SEMANTICS,         // file attributes    (FILE_FLAG_OVERLAPPED)
                            NULL);                              // file with attributes to copy
                            
  
    const DWORD dwNotificationFlags = FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_FILE_NAME;
    FILE_NOTIFY_INFORMATION Buffer[1024];
    DWORD BytesReturned;
    VOID *pBuf = (BYTE*)&Buffer;    

    while (ReadDirectoryChangesW(hDir,                     // handle to directory
                                 &Buffer,                  // read results buffer
                                 sizeof(Buffer),           // length of buffer
                                 TRUE,                     // monitoring option
                                 dwNotificationFlags,
                                 &BytesReturned,           // bytes returned
                                 NULL,                     // overlapped buffer
                                 NULL))                       // completion routine
                                 
    {
        FILE_NOTIFY_INFORMATION* pInfo = (FILE_NOTIFY_INFORMATION*) pBuf;
        SelectAction(pInfo);

    }
    
    CloseHandle(hDir);

}

VOID CALLBACK MyTimerProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwtime)
{
    i++;
}

Как организовать работу MyTimerProc?

VS 2008

Автор: Dem_max 7.8.2011, 20:26
не работает потому что нету цикла выборки сообщений
Цитата

    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    { 
        if (bRet == -1)
        {
            // handle the error anpossibly exit
        }
        else
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 
    } 
 

Автор: malor 7.8.2011, 20:29
Хорошо, а куда тогда вставить код про ReadDirectoryChangesW()?

Автор: Dem_max 7.8.2011, 20:37
Вынеси ReadDirectoryChanges в отдельный поток

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)