Click here to Skip to main content
15,885,910 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: QT C1128: number of sections exceeded object file format limit : compile with /bigobj Pin
«_Superman_»11-Aug-13 22:31
professional«_Superman_»11-Aug-13 22:31 
AnswerRe: QT C1128: number of sections exceeded object file format limit : compile with /bigobj Pin
Stephen Hewitt12-Aug-13 16:15
Stephen Hewitt12-Aug-13 16:15 
Questionusing scardAPI how to get DeviceInstanceId Pin
xiliang_pan11-Aug-13 3:40
xiliang_pan11-Aug-13 3:40 
QuestionHow to use an event object for synchronization.? Pin
mbatra319-Aug-13 23:42
mbatra319-Aug-13 23:42 
AnswerRe: How to use an event object for synchronization.? Pin
Richard Andrew x6410-Aug-13 5:39
professionalRichard Andrew x6410-Aug-13 5:39 
AnswerRe: How to use an event object for synchronization.? Pin
pasztorpisti10-Aug-13 10:13
pasztorpisti10-Aug-13 10:13 
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3111-Aug-13 21:18
mbatra3111-Aug-13 21:18 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti12-Aug-13 5:42
pasztorpisti12-Aug-13 5:42 
Both the WM_TIMER message and the "button pressed" event are handled on the same thread in some order so you don't have any multithreading issues/race conditions, these message handlers never get executed in parallel but you don't know which of these gets executed first and it can easily happen that both of them get executed at the wrong time. These message handlers of yours are called in the following way: the gui thread has a message queue. The operating system periodically puts a WM_TIMER message into it. When the user presses the cancel button the OS puts in a message that will call your "button pressed" handler. It can easily happen that there is a button pressed handler and a WM_TIMER message in the queue and when your Cancel button pressed handler executes its already later to turn of the WM_TIMER messages - you will still receive one WM_TIMER message because it is already in the queue. It can happen in reverse order too, if the WM_TIMER arrives first it can easily happen that you already have a button pressed event in the queue so you will receive it even if you disable the cancel button.

Because both handler are executed on the same thread you can create cooperation between the two handlers very easily. Use a global variable or preferrable a class member variable in your window class, you can safely use/modify the same variables from both handlers. I would use a bool variable that indicates whether the process is still "running" or not, whether it is still actual to execute a message handler. In the very beginning of both handlers you return if this variable is false. If any of the handlers is called when this variable is true you immediately set it to false and you execute the handler. This way only one of the handlers gets executed at most once regardless of the number of WM_TIMER and button pressed events accumulated in the message queue of the gui.

Cosmetics: When executing any of the handlers, disable the cancel button. Especially in the cancel handler because there you are waiting for up to 5 seconds.

BTW, with WM_TIMER you could implement a cancel that doesn't "freeze" the gui. Define an enum that indicates the process launch state:
C++
enum EProcessLaunchState
{
    EProcessLaunchState_NotRunning,
    EProcessLaunchState_Running,
    EProcessLaunchState_Cancelled,
};

Use this instead of the bool. From your cancel button handler just set this enum variable to EProcessLaunchState_Cancelled, then grey out the cancel button and return immediately. In your WM_TIMER message handler you act depending on the value of this enum. If its EProcessLaunchState_NotRunning you do nothing. If its EProcessLaunchState_Running then you just check whether it is still alive. If its EProcessLaunchState_Cancelled then you Still check whether the process is alive just like in case of EProcessLaunchState_Running but if alive you additionally perform the following: increase a wait_time variable and if the wait_time exceeds the timout then you call TerminateProcess. If you know that you set up a WM_TIMER with 1 second period then you know that 5 WM_TIMER calls is your timeout. This way your gui doesnt freeze. With your solution you couldnt even grey out the cancel button when it gets presssed because in worst case you don't return control to your main loop so the message queue processing of the gui thread is suspended for 5 seconds preventing your gui from receiving any events including the redraw of your cancel button as grey.

modified 12-Aug-13 11:50am.

GeneralRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 1:30
Erudite_Eric16-Aug-13 1:30 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti16-Aug-13 1:53
pasztorpisti16-Aug-13 1:53 
AnswerRe: How to use an event object for synchronization.? Pin
Krishnakumartg12-Aug-13 6:15
Krishnakumartg12-Aug-13 6:15 
AnswerRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 1:32
Erudite_Eric16-Aug-13 1:32 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti16-Aug-13 1:52
pasztorpisti16-Aug-13 1:52 
GeneralRe: How to use an event object for synchronization.? Pin
Erudite_Eric16-Aug-13 23:02
Erudite_Eric16-Aug-13 23:02 
GeneralRe: How to use an event object for synchronization.? Pin
pasztorpisti17-Aug-13 1:31
pasztorpisti17-Aug-13 1:31 
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3126-Dec-13 19:35
mbatra3126-Dec-13 19:35 
GeneralRe: How to use an event object for synchronization.? Pin
mbatra3126-Dec-13 19:34
mbatra3126-Dec-13 19:34 
QuestionCButton and CListCtrl two controls how self drawing Pin
ztwisdom9-Aug-13 16:17
ztwisdom9-Aug-13 16:17 
SuggestionRe: CButton and CListCtrl two controls how self drawing Pin
Richard MacCutchan9-Aug-13 23:28
mveRichard MacCutchan9-Aug-13 23:28 
AnswerRe: CButton and CListCtrl two controls how self drawing Pin
Krishnakumartg12-Aug-13 6:17
Krishnakumartg12-Aug-13 6:17 
QuestionChanging Treeview control apperance Pin
Member 78617636-Aug-13 22:58
Member 78617636-Aug-13 22:58 
AnswerRe: Changing Treeview control apperance Pin
_AnsHUMAN_ 7-Aug-13 2:10
_AnsHUMAN_ 7-Aug-13 2:10 
GeneralRe: Changing Treeview control apperance Pin
Member 78617637-Aug-13 3:38
Member 78617637-Aug-13 3:38 
GeneralRe: Changing Treeview control apperance Pin
Richard MacCutchan7-Aug-13 7:18
mveRichard MacCutchan7-Aug-13 7:18 
Questionregarding edit box Pin
Member 101853676-Aug-13 21:59
Member 101853676-Aug-13 21:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.