Click to See Complete Forum and Search --> : Thread that polls textfiles from folder


Johan M
May 21st, 1999, 07:02 AM
I have a thread running that should determine if a change has occurred in a folder.
The program looks like this:

class Prg
{
static UINT poll_thread(LPVOID);
private:
CString pref_jobdir_m;
...
};

void Prg::test() {
pref_jobdir_m = "c:/jobs/";
CWinThread *thread_handle = AfxBeginThread(Prg::poll_thread, &pref_jobdir_m);
}

UINT Prg::poll_thread(LPVOID parameter) {
char *dir_name = (char*)parameter; // Question 1: is this correct ?!?
HANDLE watch_handle = FindFirstChangeNotification(dir_name, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
if (watch_handle != INVALID_HANDLE_VALUE) {
while (TRUE) {
if (WaitForSingleObject(watch_handle, INFINITE) == WAIT_OBJECT_0)
{
MessageBeep(-1);
// Do some extensive time consuming search here of the checked directory
FindNextChangeNotification(watch_handle); // Question 2: Do this before or after the search?
}
}
FindCloseChangeNotification(watch_handle); // the thread won't reach this line anyway...
}
return 0;
}



However, it doesn't work as it seems like the thread can't interpretate my path pref_jobdir_m correctly.
I have two question:
1) I want to pass the path pref_jobdir_m to the thread. However, converting parameter to
a char* doesn't work. How do I do this (check the comment)

2) Should I call FindNextChangeNotification() before or after the thread searches though the
watched directory? I don't want to miss any new events. What if two events appears in a short time?

By the way, I am not using ReadDirectoryChangesW as the compiler won't recognize it. I have Win NT 4.0...

sally
May 21st, 1999, 07:17 AM
Do not include the ending slash in the path name and it should work
I burnt my fingers on it myself recently


pref_jobdir_m = "c:/jobs";




Sally

Sally
May 21st, 1999, 07:17 AM
Do not include the ending slash in the path name and it should work
I burnt my fingers on it myself recently


pref_jobdir_m = "c:/jobs";




Sally

Johan M
May 21st, 1999, 07:51 AM
I tried to change that line. Well, it doesn't work either.
This code works, but it is not what I need:

void Juke_prg::test() {
pref_jobdir_m = "ThisIsJustATest"; // Just any string, do not bother
CWinThread *thread_handle = AfxBeginThread(Juke_prg::poll_thread, &pref_jobdir_m);
}
UINT Juke_prg::poll_thread(LPVOID parameter) {
char *dir_name = "c:/jobs"; // assign it directly. This is not what I desire, I want to use parameter
HANDLE watch_handle = FindFirstChangeNotification(dir_name, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
if ...
...
return 0;
}



I want to access pref_jobdir_m via reference from thread. This does not work.

void Juke_prg::test() {
pref_jobdir_m = "c:/jobs"; // This parameter should be use by the thread...
CWinThread *thread_handle = AfxBeginThread(Juke_prg::poll_thread, &pref_jobdir_m);
}
UINT Juke_prg::poll_thread(LPVOID parameter) {
char *dir_name = (char*)parameter; // ...but it doesn't work
HANDLE watch_handle = FindFirstChangeNotification(dir_name, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
if ...
...
return 0;
}



Might it be any error in the declaration of pref_jobdir_m, or is it a conversion that is wrong?

/Johan M

Johan M
May 21st, 1999, 08:42 AM
I found the error! This is the correct version:

class Prg
{
static UINT poll_thread(LPVOID);
private:
CString pref_jobdir_m;
...
};
void Prg::test() {
pref_jobdir_m = "c:/jobs";
CWinThread *thread_handle = AfxBeginThread(Prg::poll_thread, pref_jobdir_m.GetBuffer(0));
}
UINT Prg::poll_thread(LPVOID parameter) {
HANDLE watch_handle = FindFirstChangeNotification((char*)parameter, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
if (watch_handle != INVALID_HANDLE_VALUE) {
while (TRUE) {
if (WaitForSingleObject(watch_handle, INFINITE) == WAIT_OBJECT_0)
{
MessageBeep(-1);
// Do some extensive time consuming search here of the checked directory
FindNextChangeNotification(watch_handle); // Question 2: Do this before or after the search?
}
}
FindCloseChangeNotification(watch_handle); // the thread won't reach this line anyway...
}
return 0;
}



The CString pointer should ofcourse have been obtained with CString::GetBuffer(0)!
But I still not know the answer to:

2) Should I call FindNextChangeNotification() before or after the thread searches though the
watched directory? I don't want to miss any new events. What if two events appears in a short time?

Thanks for your time,
Johan M

sally
May 22nd, 1999, 06:43 AM
I don't think it really matters if you do it before or after, you still have to wait while you process the notification.
You COULD try to do your processing in a seperate thread......

Sally

Sally
May 22nd, 1999, 06:43 AM
I don't think it really matters if you do it before or after, you still have to wait while you process the notification.
You COULD try to do your processing in a seperate thread......

Sally

RajM
May 24th, 1999, 06:39 PM
Try this(my assumption is pref_jobdir_m is a CString..is it?)


UINT Juke_prg::poll_thread(LPVOID parameter) {
CString *dir_name = ( CString *)parameter; // ...but it doesn't work
HANDLE watch_handle = FindFirstChangeNotification(*dir_name, FALSE, FILE_NOTIFY_CHANGE_FILE_NAME);
if ...
...
return 0;
}

Johan M
May 26th, 1999, 02:34 AM
I have solved the problem already, using the method CString::GetBuffer(0);
But thanks anyway!

/Johan M