mfc VC ++ 6.0中的计时器问题 [英] Timer issues in mfc VC++ 6.0

查看:121
本文介绍了mfc VC ++ 6.0中的计时器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在已经验证过的代码中添加代码,通过ethernet.i连接到仪器。我在同一个代码中添加另一个仪器API。我希望每50ms生成一次信号以实现那个时间我用于循环生成信号对于不同的频率使用睡眠50ms。当我运行代码时,整个应用程序被挂起以完成for循环。我希望每50ms产生一次频率信号,而不需要挂起应用程序。我有两个想法,一个是计时器,另一个是线程。



我尝试过:



我已经厌倦了计时器但是没有工作。任何基于对话框的线程生成示例都会发送给我。

I am adding code in already proven code which is connected to instrument thru ethernet.i am adding another instrument API in the same code.i want to generate signal for every 50ms to achieve that time I am using for loop to generate signal for different Frequency used sleep for 50ms. When I run the code the whole application is hanged up to completion of for loop. I want to generate Frequency signal for every 50ms without hanging application. I have two ideas one is timer and another is thread.

What I have tried:

I have tired with timer but is not worked. Any dialog based thread generation examples send me.

推荐答案

Google会找到您的样本,或者您可以查看使用C ++和MFC进行多线程处理 [ ^ ]
Google will find you samples, or you can look at Multithreading with C++ and MFC[^]


查看Newcomer的主题和流程系列



请注意 50 ms Windows操作系统的时间相对较短。此外,为了使您的应用程序保持响应,您的代码的活动(即不睡眠)部分应该是 50 ms 的一小部分(这是您的线程应该是大部分时间都在睡觉。)
Have a look at Newcomer's Threads and Processes series.

Please note 50 ms is a relatively short time for Windows OS. Moreover, in order to keep your application responsive the 'active' (i.e. not sleeping) part of your code should be a small fraction of 50 ms (that is your thread should be sleeping most of the time).


永远不要在主(GUI)线程中使用 Sleep()



如前所述,有多个关于线程的资源。

一个例子是使用工作线程 [ ^ ]。



基于该文章,您应该创建一个工作线程和一个事件来停止该线程。然后工作线程函数可以如下所示:

Never use Sleep() in the main (GUI) thread.

As already mentioned there are multiple resources about threads.
An example is Using Worker Threads[^].

Based on that article you should create a worker thread and an event to stop the thread. Then the worker thread function can look like:
/*static*/ UINT CMyThreadClass::ThreadFunc(LPVOID p)
{
    CMyThreadClass * self = (CMyThreadClass *)p;
    bool bStop = false;
    while (!bStop)
    {
        switch (::WaitForSingleObject(self->m_StopEventHandle, 50))
        {
        case WAIT_OBJECT_0 :
            bStop = true;
            break;
        case WAIT_TIMEOUT :
            // Generate signal here
            break;
        }
    }
    return 0;
}

虽然这提供了比普通计时器更好的计时精度,但它仍然不是绝对准确的。如果您在工作线程中使用例如阻塞套接字调用,则可能需要通过测量执行时间来相应地调整超时值。

While this provides a better timing accuracy than a normal timer, it will still not be absolute accurate. If you use for example blocking socket calls in the worker thread, it might be necessary to adjust the time out value accordingly by measuring the execution time.


这篇关于mfc VC ++ 6.0中的计时器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆