简体   繁体   中英

how can we use the timer in thread to create the text file after specific periofd of time using vc++?

I have tried this code to generate the same text file after every minute, but it is not working on console application. I am using vs2017

 void CALLBACK f(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
 {
ofstream myfile;
myfile.open("C:\\log.txt", ios::app);
myfile <<  " : test" << "\n";
myfile.close();
KillTimer(hwnd, timerId);
}
unsigned int __stdcall mythread(void* data)
{
while (startii == 0)
{
    SetTimer(0,             // handle to main window 
        0,            // timer identifier 
        10000,                 // 10-second interval 
        (TIMERPROC)&f);     // no timer callback 

    return 0;
  }
 int main()
{
myhandle = (HANDLE)_beginthreadex(0, 0, &mythread, 0, 0, 0);
}enter code here

use std::thread and std::this_thread::sleep_for(std::chrono::milliseconds(7000)) or so.

thread itself will look like that

UINT CALLBACK hammersmith(VOID *c)
{   
    while (1)
    {   
        std::unique_lock<std::mutex> lb(com_m);
        sp_tray = std::shared_ptr<int>(new int(7));
        com_r = sp_tray;
        lb.unlock();    
        sp_tray.reset();
    }

    return 0;
    
}

or use similar thread function with _beginthreadex . And you can use winapi's Sleep() . And CALLBACK includes stdcall stuff so use it instead.
timers are from 16-bit windows era

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM