首页 > 解决方案 > 使用 Win32 API 在 C++ Builder 中制作多线程计时器

问题描述

我有一个在主线程中运行的应用程序。现在我需要使用 Timer 事件启动一个新线程,该线程必须每 5 分钟定期调用一个方法。在那,我需要打印主线程的状态。即使主线程死了。我需要第二个线程处于活动状态打印主线程死亡或出现异常。

我尝试了示例,但我不知道如何调用 Timer 间隔,即我不知道如何实现定期调用。

以下是我尝试的示例代码片段:

#include <vcl.h>
#include <windows.h>

#pragma hdrstop
#pragma argsused

#include <tchar.h>
#include <stdio.h>
#include <errno.h>
#include <stddef.h>
#include <process.h>
#include <time.h>

#pragma comment(lib, "Winmm.lib")

void Thread_Start(void*);

int _tmain(int argc, _TCHAR* argv[])
{
   _beginthread(Thread_Start, 10000, NULL);
   printf("/n RS");
   //   getchar();
   //   return 0;
}

int i = 0;
void Thread_Start(void*)
{
  while (i <50)
  {
    printf("/n Thread Started");
    i++;
    break;
  }
}

标签: c++multithreadingtimerc++builder

解决方案


推荐阅读