Click here to Skip to main content
15,119,786 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to instantiate class with parameters - syntax please ? Pin
leon de boer6-Oct-16 20:04
Memberleon de boer6-Oct-16 20:04 
QuestionWhy can't I assign a variable's address to an enum? Pin
arnold_w30-Sep-16 1:18
Memberarnold_w30-Sep-16 1:18 
AnswerRe: Why can't I assign a variable's address to an enum? Pin
Richard MacCutchan30-Sep-16 1:52
mveRichard MacCutchan30-Sep-16 1:52 
AnswerRe: Why can't I assign a variable's address to an enum? Pin
leon de boer30-Sep-16 4:22
Memberleon de boer30-Sep-16 4:22 
QuestionWaitForSingleObject best practices Pin
ForNow27-Sep-16 15:26
MemberForNow27-Sep-16 15:26 
AnswerRe: WaitForSingleObject best practices Pin
leon de boer27-Sep-16 19:18
Memberleon de boer27-Sep-16 19:18 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 3:18
MemberForNow28-Sep-16 3:18 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer28-Sep-16 9:16
Memberleon de boer28-Sep-16 9: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 12:41
MemberForNow28-Sep-16 12:41 
GeneralRe: WaitForSingleObject best practices Pin
ForNow28-Sep-16 16:57
MemberForNow28-Sep-16 16:57 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer28-Sep-16 20:20
Memberleon de boer28-Sep-16 20:20 
GeneralRe: WaitForSingleObject best practices Pin
leon de boer30-Sep-16 10:42
Memberleon de boer30-Sep-16 10:42 
GeneralRe: WaitForSingleObject best practices Pin
ForNow30-Sep-16 10:52
MemberForNow30-Sep-16 10:52 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 15:31
professionalJoe Woodbury24-Oct-16 15:31 
GeneralRe: WaitForSingleObject best practices Pin
Joe Woodbury24-Oct-16 15:29
professionalJoe Woodbury24-Oct-16 15:29 
QuestionDevice on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 9:18
MemberDavid MacKinnon27-Sep-16 9:18 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 11:41
Memberjeron127-Sep-16 11:41 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 12:38
MemberDavid MacKinnon27-Sep-16 12:38 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 12:44
Memberjeron127-Sep-16 12:44 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon27-Sep-16 13:10
MemberDavid MacKinnon27-Sep-16 13:10 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron127-Sep-16 14:28
Memberjeron127-Sep-16 14:28 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon28-Sep-16 13:30
MemberDavid MacKinnon28-Sep-16 13:30 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
jeron129-Sep-16 5:19
Memberjeron129-Sep-16 5:19 
GeneralRe: Device on RS232 only responds to first send request (Visual C++) Pin
David MacKinnon29-Sep-16 5:43
MemberDavid MacKinnon29-Sep-16 5:43 
AnswerRe: Device on RS232 only responds to first send request (Visual C++) Pin
leon de boer27-Sep-16 18:18
Memberleon de boer27-Sep-16 18: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.