Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Updated 10-Nov-17 2:16am

Google will find you samples, or you can look at Multithreading with C++ and MFC[^]
 
Share this answer
 
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).
 
Share this answer
 
v2
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.
 
Share this answer
 
Comments
Member 12208944 11-Nov-17 20:38pm    
What I understood I am writing:
I am press one button that button function will have afxbeginthread function to call threadfunc function enter in while loop then what happens from where m_stopeventhandle will come , if it is the member of button waitforsingleobject wait for 50ms after timeout generate signal once after that break end of while loop, I want every 50ms generating Frequency signal without hangup another functions timers threads of same application, if I am wrong in understanding can u explain me
Jochen Arndt 12-Nov-17 5:01am    
You need to read about threads and to understand.

With Windows you usually create a class for your thread. That contains at least the (static) thread function, a start function which creates the thread and starts execution, and a stop function (to be also called from the destructor).

The stop function will set the event to inform the thread that he should leave his loop and return. It must be called when terminating your application and may be called whenever you want to stop the thread.

The WaitFor... functions will either return when an event occured or when the timeout has elapsed.
Member 12208944 13-Nov-17 14:01pm    
Thank you it's working ....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900