Click here to Skip to main content
15,900,378 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: save/load variables in a dialog Pin
Green Fuze12-Apr-05 0:35
Green Fuze12-Apr-05 0:35 
GeneralRe: save/load variables in a dialog Pin
ThatsAlok11-Apr-05 20:35
ThatsAlok11-Apr-05 20:35 
GeneralRe: save/load variables in a dialog Pin
Green Fuze12-Apr-05 2:55
Green Fuze12-Apr-05 2:55 
GeneralThread synchronization. Pin
Nick Z.11-Apr-05 13:50
Nick Z.11-Apr-05 13:50 
GeneralRe: Thread synchronization. Pin
Ryan Binns11-Apr-05 17:59
Ryan Binns11-Apr-05 17:59 
GeneralRe: Thread synchronization. Pin
Nick Z.11-Apr-05 18:09
Nick Z.11-Apr-05 18:09 
GeneralRe: Thread synchronization. Pin
markkuk12-Apr-05 0:23
markkuk12-Apr-05 0:23 
GeneralRe: Thread synchronization. Pin
Jack Puppy11-Apr-05 18:00
Jack Puppy11-Apr-05 18:00 
You should try using an event for this.

in the GUI thread:

<br />
// Create an event in non-signalled state that's a member variable of your dialog<br />
m_hCancelEvent = CreateEvent(NULL, TRUE, FALSE, NULL);<br />


then pass the event handle to the worker thread in the AfxBeginThread call.

The GUI can then signal the worker (say via a Stop button) using:

<br />
// Signal the event<br />
SetEvent(m_hCancelEvent);<br />


and the worker can test for that signal in the thread procedure using WaitForSingleObject (or WaitForMultipleObjects if you're tracking multiple events)

<br />
// Check for signal; Wait will return WAIT_OBJECT_0 when the event has been signalled<br />
if (::WaitForSingleObject(pYourThreadData->hCancelEvent, 0) == WAIT_OBJECT_0))<br />
  // bail out here<br />
else<br />
// do thread stuff<br />
<br />


You need to close the event when your dialog closes:

<br />
// CloseHandle will throw an exception if the handle is bad, so you should use a try block here<br />
CloseHandle(m_hCancelEvent);<br />


II)

If you need to read/write shared data between threads, it's best practice to use the appropriate sync object. This is a very simplified example.

<br />
// Global defines<br />
static CCriticalSection g_criticalSection;<br />
static int* g_pSharedValue;<br />


In Thread 1:

<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
// Block the other thread<br />
singleLock.Lock();<br />
<br />
if (g_pSharedValue != NULL)<br />
{<br />
    // I give to you, 8 more units<br />
    *g_pSharedValue += 8;<br />
<br />
    // Then double the profits<br />
    *g_pSharedValue *= 2;<br />
}<br />
<br />
// OK, we're done, other thread can have at it now<br />
singleLock.Unlock();<br />
<br />


In Thread 2:

<br />
CSingleLock singleLock(&g_criticalSection);<br />
<br />
// Block the other thread; all statements until UnLock are atomic<br />
singleLock.Lock();<br />
<br />
// Hah! I delete your silly profits<br />
delete g_pSharedValue;<br />
g_pSharedValue = NULL;<br />
<br />
// OK, we're done, other thread can have at it now<br />
singleLock.Unlock();<br />
<br />


Without the criticial section here, Thread 2 may execute the statement "delete g_pSharedValue", and then pass control to Thread 1. g_pSharedValue is corrupt, and causes Thread 1 to barf.



Suspicious | :suss: Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
GeneralRe: Thread synchronization. Pin
Nick Z.11-Apr-05 18:12
Nick Z.11-Apr-05 18:12 
GeneralUNICODE COMPILATION ReplaceSel usage for char array. Pin
kristna11-Apr-05 11:19
kristna11-Apr-05 11:19 
GeneralRe: UNICODE COMPILATION ReplaceSel usage for char array. Pin
Graham Bradshaw11-Apr-05 12:05
Graham Bradshaw11-Apr-05 12:05 
GeneralRe: UNICODE COMPILATION ReplaceSel usage for char array. Pin
kristna11-Apr-05 22:55
kristna11-Apr-05 22:55 
GeneralMFC Worker Thread Problem Pin
CNewbie11-Apr-05 11:00
CNewbie11-Apr-05 11:00 
GeneralRe: MFC Worker Thread Problem Pin
Orkun GEDiK11-Apr-05 11:50
Orkun GEDiK11-Apr-05 11:50 
GeneralRe: MFC Worker Thread Problem Pin
CNewbie11-Apr-05 11:56
CNewbie11-Apr-05 11:56 
GeneralRe: MFC Worker Thread Problem Pin
Blake Miller11-Apr-05 12:33
Blake Miller11-Apr-05 12:33 
GeneralRe: MFC Worker Thread Problem Pin
toxcct11-Apr-05 20:55
toxcct11-Apr-05 20:55 
GeneralRe: MFC Worker Thread Problem Pin
CNewbie11-Apr-05 20:58
CNewbie11-Apr-05 20:58 
GeneralRe: MFC Worker Thread Problem Pin
Iain Clarke, Warrior Programmer11-Apr-05 12:37
Iain Clarke, Warrior Programmer11-Apr-05 12:37 
GeneralImage resize in GDI+ Pin
smicikli11-Apr-05 10:18
smicikli11-Apr-05 10:18 
GeneralATL configuration Pin
act_x11-Apr-05 9:47
act_x11-Apr-05 9:47 
GeneralComing out of StandBy application crashes Pin
casid11-Apr-05 9:32
casid11-Apr-05 9:32 
GeneralRe: Coming out of StandBy application crashes Pin
Blake Miller11-Apr-05 12:49
Blake Miller11-Apr-05 12:49 
Generalmemory leaks vs. the OS Pin
Maximilien11-Apr-05 9:30
Maximilien11-Apr-05 9:30 
GeneralRe: memory leaks vs. the OS Pin
Rick York11-Apr-05 10:17
mveRick York11-Apr-05 10:17 

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.