Click here to Skip to main content
15,887,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to instantiate class with parameters - syntax please ? Pin
leon de boer6-Oct-16 19:04
leon de boer6-Oct-16 19:04 
QuestionWhy can't I assign a variable's address to an enum? Pin
arnold_w30-Sep-16 0:18
arnold_w30-Sep-16 0:18 
AnswerRe: Why can't I assign a variable's address to an enum? Pin
Richard MacCutchan30-Sep-16 0:52
mveRichard MacCutchan30-Sep-16 0:52 
AnswerRe: Why can't I assign a variable's address to an enum? Pin
leon de boer30-Sep-16 3:22
leon de boer30-Sep-16 3:22 
QuestionWaitForSingleObject best practices Pin
ForNow27-Sep-16 14:26
ForNow27-Sep-16 14:26 
AnswerRe: WaitForSingleObject best practices Pin
leon de boer27-Sep-16 18:18
leon de boer27-Sep-16 18:18 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 2:18
ForNow28-Sep-16 2:18 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer28-Sep-16 8:16
leon de boer28-Sep-16 8:16 
It's a low priority Hotwatch .. it will always be better to do as a thread and it isn't hard
// Define a structure to pass to thread
typedef struct _MYWATCHERDATA {
	HWND AppWindow;             // Application window to post message to
	UINT MsgId;			        // Your private message ID will be WM_APP + some number
	WPARAM CmdId;               // Command ID .. perhaps you have multiple of these watchers
	char* FolderToWatch;        // The string nominating the folder to watch
	HANDLE dwChangeHandles[2];  // These will be thread wait handles (1 for it, 1 for me to force exit)
	BOOL threadExitComplete;	// Thread exit completed
} WATCHERDATA;

// This is the thread routine
// Pass an initialized structure in on LPARAM Of CreateThread
DWORD WINAPI NotifyMeOfFolderChangeThread(LPVOID lpParam)    // thread procedure
{
	BOOL exitSignal = FALSE;                                 // Preset exit signal to false
	WATCHERDATA* watchData = (WATCHERDATA*)lpParam;	         // Typecast the lParam to your structure you pass in

	// Watch Handle[0] belongs to this thread 
	watchData->dwChangeHandles[0] =
		FindFirstChangeNotification(watchData->FolderToWatch, // folder path passed in
			FALSE,										      // no subfolders
			FILE_NOTIFY_CHANGE_FILE_NAME);				      // watch for renaming, creating, or deleting a file
	
	if (INVALID_HANDLE_VALUE == watchData->dwChangeHandles[0])// Error something is dorked probably folder doesn't exist
	{
		DWORD dwError = ::GetLastError();
		// handle error
		return dwError;
	}

	while (exitSignal == FALSE) {
		// Okay we wait on the notification signal or exernal exit signal
		DWORD dwWaitStatus = WaitForMultipleObjects(2, 
			watchData->dwChangeHandles, FALSE, INFINITE);
		
		switch (dwWaitStatus) {
			// Wait exit was from directory change
			case WAIT_OBJECT_0:
				// Post off message back to application window.
				PostMessage(watchData->AppWindow, watchData->MsgId,
					watchData->CmdId, (LPARAM)watchData->FolderToWatch);
				FindNextChangeNotification(watchData->dwChangeHandles[0]);
				break;
			// External thread signal forcing thread to break out and exit
			case WAIT_OBJECT_0 + 1:
				exitSignal = TRUE;           // Yep time to exit
				break;
		}
	}
	watchData->threadExitComplete = TRUE; // Thread has finished with watchData .. tell app that
	return 0;
}


The use of the thread involves, initialize, use, wait for thread to exit .. cleanup
// Initialize a WATCHERDATA structure
char* watchDirectory = "c:\\temp\\";                                // Some directory to watch
WATCHERDATA* myWatch = (WATCHERDATA*) malloc(sizeof(WATCHERDATA)); // Allocate memory
myWatch->AppWindow = myAppHandle;                                  // Application window handle
myWatch->MsgId = WM_APP  + 100;									   // Some private application message ID 
myWatch->CmdId = 1;										           // Some private id we might have multiple watches
int len = strlen(watchDirectory) + 1;                               // size of string
myWatch->FolderToWatch = (char*) malloc(len);					   // Allocate string space 
strcpy_s(myWatch->FolderToWatch, len, watchDirectory);			   // Copy string to allocated memory        
myWatch->dwChangeHandles[0] = 0;								   // Clear thread handle one .. thread itself uses that
myWatch->dwChangeHandles[1] = CreateEvent(0, FALSE, FALSE, "EXIT");// Set thread handle two for us to force exit
myWatch->threadExitComplete = FALSE;	                           // Thread will set this on complete
																   
// Use thread
HANDLE workerThread = CreateThread(NULL, 0, NotifyMeOfFolderChangeThread, myWatch, 0, NULL);

// Change thread priority
SetThreadPriority(workerThread, THREAD_PRIORITY_LOWEST);

// This will do a simple 10 sec wait .. simulating doing something
Sleep(10000);

// Ok manually terminate the thread
SetEvent(myWatch->dwChangeHandles[1]);

// Wait for thread to signal it has finished with myWatch
while (myWatch->threadExitComplete == FALSE) {};

// Okay you are free to cleanup myWatch
free(myWatch->FolderToWatch);
free(myWatch);

In vino veritas

GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 11:41
ForNow28-Sep-16 11:41 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 15:57
ForNow28-Sep-16 15:57 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer28-Sep-16 19:20
leon de boer28-Sep-16 19:20 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer30-Sep-16 9:42
leon de boer30-Sep-16 9:42 
GeneralRe: WaitForSingleObject best practices Pin
ForNow30-Sep-16 9:52
ForNow30-Sep-16 9:52 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 14:31
professionalJoe Woodbury24-Oct-16 14:31 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 14:29
professionalJoe Woodbury24-Oct-16 14:29 
QuestionDevice on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 8:18
David MacKinnon27-Sep-16 8:18 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 10:41
jeron127-Sep-16 10:41 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 11:38
David MacKinnon27-Sep-16 11:38 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 11:44
jeron127-Sep-16 11:44 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 12:10
David MacKinnon27-Sep-16 12:10 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 13:28
jeron127-Sep-16 13:28 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon28-Sep-16 12:30
David MacKinnon28-Sep-16 12:30 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron129-Sep-16 4:19
jeron129-Sep-16 4:19 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon29-Sep-16 4:43
David MacKinnon29-Sep-16 4:43 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
leon de boer27-Sep-16 17:18
leon de boer27-Sep-16 17:18 

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.