如何在MFC中破坏ReadDirectoryChangesW函数?

问题描述 投票:1回答:1

在MFC中,我在主对话框中添加了一个按钮,并在此按钮中添加了ReadDirectoryChangesW功能。如果单击该按钮,将监视指定的文件夹。问题是,如果单击按钮,对话框将无法工作,因为程序仍在ReadDirectoryChangesW功能中运行。我希望在自由使用对话框的同时能够监视文档,有什么解决方法吗(请参考代码)

void CGGTransferDlg::OnBnClickedAutoStartButton(CString dir)
{
    HANDLE dwRootDirChangeHandle = CreateFileA(
        dir, /* pointer to the file name */
        FILE_LIST_DIRECTORY,                /* (this is important to be FILE_LIST_DIRECTORY!) access (read-write) mode */
        FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,  /* (file share write is needed, or else user is not able to rename file while you hold it) share mode */
        NULL, /* security descriptor */
        OPEN_EXISTING, /* how to create */
        FILE_FLAG_BACKUP_SEMANTICS, /* file attributes */
        NULL /* file with attributes to copy */
        );
    if (dwRootDirChangeHandle == INVALID_HANDLE_VALUE)
    {
        printf("error: %d", GetLastError());
        return;
    }

    char notify[1024];
    memset(notify, 0, 1024);
    DWORD cbBytes;
    FILE_NOTIFY_INFORMATION *pNotify = (FILE_NOTIFY_INFORMATION *)notify;
    char str1[MAX_PATH];

    CString NewResultFileLocal, NewResultFileSever, FileExtension;
    while (1)
    {

        if (ReadDirectoryChangesW(dwRootDirChangeHandle, &notify, sizeof(notify),
            TRUE, FILE_NOTIFY_CHANGE_FILE_NAME /*| FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE
                                               | FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY*/, &cbBytes, NULL, NULL))//Fourth Parameter = TRUE, Get Subdirectory
        {
            int i = 0;
            memset(str1, 0, MAX_PATH);
            WideCharToMultiByte(CP_ACP, 0, pNotify->FileName, pNotify->FileNameLength / 2, str1, 99, NULL, NULL);

            NewResultFileLocal = str1;//char to cstring 
            NewResultFileLocal = dir + NewResultFileLocal;
            NewResultFileSever = str1;
            int n = NewResultFileSever.ReverseFind('\\');
            n = NewResultFileSever.GetLength() - n;
            NewResultFileSever = NewResultFileSever.Right(n - 1);

            FileExtension = PathFindExtension(NewResultFileSever);
            if (FileExtension == "aso")
                break;

            switch (pNotify->Action)
            {
            case FILE_ACTION_ADDED:
                Upload(NewResultFileLocal, NewResultFileSever);

                break;

            case FILE_ACTION_MODIFIED:
                printf("The file was modified. This can be a change in the time stamp or attributes.\n");
                break;
            case FILE_ACTION_REMOVED:
                LogSave("The file was removed from the directory: " + NewResultFileLocal);
                break;
            case FILE_ACTION_RENAMED_NEW_NAME:
                printf("The file was renamed and this is the new name.\n");
                break;
            case FILE_ACTION_RENAMED_OLD_NAME:
                printf("The file was renamed and this is the old name.\n");
                break;
            default:
                printf("Unknown command.\n");
            }

        }
    }
    ::CloseHandle(dwRootDirChangeHandle);
}
winapi mfc readdirectorychangesw
1个回答
0
投票

您的UI无效,因为您正在UI线程内的同步阻塞循环中使用ReadDirectoryChangesW()。因此,UI无法处理其他操作。所以不要那样做。而是将循环移动到单独的工作线程,并在检测到更改时通知您的主UI。另外,异步使用ReadDirectoryChangesW(),以便在准备终止线程时可以中断循环。

© www.soinside.com 2019 - 2024. All rights reserved.