Home > Software engineering >  MFC through AFXbeginthread () function of the new worker threads can use a timer?
MFC through AFXbeginthread () function of the new worker threads can use a timer?

Time:11-12

I built a dialog project, and then in the program through AFXbeginthread () function is opened a new worker thread, the thread function used in the timer complains, don't know what the reason, search on the Internet said worker thread no news mechanism, is in the worker thread cannot use timer, you can use the words and how to use, hope to have and give some hints,

CodePudding user response:

In the timer AFXbeginthread ()
KillTimer ensure the timer again AFXbeginthread (once)

CodePudding user response:

Can't use the timer

CodePudding user response:

Threads can use timeSetEvent

CodePudding user response:

refer to the original poster qq_38881359 response:
I built a dialog project, and then in the program through AFXbeginthread () function is opened a new worker thread, use the timer complains in the thread function, don't know what the reason, search on the Internet said worker thread no news mechanism, is in the worker thread cannot use timer, you can use the words and how to use, want to have and give some hints,

In principle need not, MFC timer thread support message loop mechanism, the main thread of MFC is the UI thread, have a message loop mechanism, so you can use; Self-built threads belonging to a worker thread, no message loop mechanism, can't use,

CodePudding user response:

, the need to establish a message loop, this I have done,

CodePudding user response:

Worker thread cannot be used in the timer, you can change the way of thinking, in dialog interface open timer, for example, in the timer timing setting flags, query flags in a worker thread to achieve timing between perform a piece of code, other time to wait or do something else, can also be used for processing object

CodePudding user response:

Thread actually is a write your own "infinite loop" function (write your own in a condition to trigger out after death cycle), so you can use sleep (in the infinite loop... ) wait, the real is the timer

CodePudding user response:

The child thread can use SetWaitableTimer
Can also open the timer in your main thread, setting an event when the time is up, then the child thread to wait for this event,

CodePudding user response:

MFC timer is actually the default binding and a window, rely on this window message loop to receive the timer, a similar mechanism and CSocket, if be sure to do this you can create a hidden window, bind it should be ok

CodePudding user response:

Used in the thread function timer??? With what to do
  • Related