CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Thread that polls textfiles from folder

    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:oll_thread, &pref_jobdir_m);
    }

    UINT Prg:oll_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...


  2. #2
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Thread that polls textfiles from folder

    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


  3. #3
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Re: Thread that polls textfiles from folder

    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:oll_thread, &pref_jobdir_m);
    }
    UINT Juke_prg:oll_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:oll_thread, &pref_jobdir_m);
    }
    UINT Juke_prg:oll_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


  4. #4
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Re: Thread that polls textfiles from folder

    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:oll_thread, pref_jobdir_m.GetBuffer(0));
    }
    UINT Prg:oll_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


  5. #5
    Join Date
    May 1999
    Location
    Sydney, Australia
    Posts
    420

    Re: Thread that polls textfiles from folder

    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


  6. #6
    Join Date
    Apr 1999
    Posts
    37

    Re: Thread that polls textfiles from folder

    Try this(my assumption is pref_jobdir_m is a CString..is it?)


    UINT Juke_prg:oll_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;
    }



  7. #7
    Join Date
    May 1999
    Location
    L.A.
    Posts
    20

    Re: Thread that polls textfiles from folder

    I have solved the problem already, using the method CString::GetBuffer(0);
    But thanks anyway!

    /Johan M


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured