运行对话框事件 [英] Dialog events while its running

查看:62
本文介绍了运行对话框事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
这是santosh,
想使用mfc开发搜索应用程序.
一个简单的基于对话框的应用程序,其中用户输入要搜索的文件名,搜索驱动器和搜索类型.

现在我很好地实现了此应用程序.
但是文件搜索需要很多时间.
就像我要搜索C:\ test.txt中存在的Test.txt.
几秒钟后我得到了文件,但搜索仍在进行.
扫描所有文件和文件夹后停止.


所以我想在单击取消搜索"的对话框上放置取消"按钮.

我怎么得到的.

我使用递归逻辑进行搜索

谢谢Advanced

Hi all,
This is santosh,
want to developed search application using mfc.
a simple dialog based application in which user enter name of file to be search,drive of search,and type of search.

now i implement this application very well.
but serch of file takes lots of time.
like i want to search Test.txt present in C:\test.txt.
in in few second i got the file but search still going on.
stop when all files and folder are scanned.


so i want to placed Cancel button on the dialog on click of cancel search should stop.

how i get that.

i used recursive logic for serching

thanks in advanced

推荐答案

您必须为此使用线程.在此示例中,我仅使用一个按钮来启动和取消线程.

在对话框的.h中,添加:
You must use a thread for that. In this example, I used only one button to start and cancel the thread.

in the .h of your dialog, add:
//handle on your thread function
HANDLE m_hThread;
//your thread funtion
static DWORD WINAPI ThreadFunction(LPVOID param);
//will be turned to TRUE if the user wants to cancel
BOOL m_Cancel;



插入对话框的.cpp中,添加:



int the .cpp of your dialog, add:

//in the constructor
CYourDialog::CYourDialog()
{
    //initialize your handle
    m_hThread = NULL;
    //and the boolean variable
    m_Cancel =  FALSE;
}

//in the button Start/Cancel handler
void CYourDialog::OnButtonStartOrCancelClicked()
{
    if (m_hThread == NULL)
    {
        //no thread is running, so let''s start one

        m_Cancel = FALSE;
        //start your thread
        //we pass a pointer to the dialog so we can access easily its members
        m_hThread = CreateThread(NULL, 0, ThreadFunction, this, 0, NULL);
        //change the button text to "Cancel"
        SetDlgItemText(IDC_BUTTON_START_OR_CANCEL, "Cancel");
    }
    else
    {
        //a thread is running, so let''s cancel it
        m_Cancel = TRUE;
        //wait for the thread to finish
        //it is not necessary to really wait the end of the thread here
        //I''m just showing a simple way to handle the start/cancel button
        WaitForSingleObject(m_hThread, INFINIE);
        //the thread finished
        CloseHandle(m_hThread);
        m_hThread = NULL;
        //change the button text back to "Start"
        SetDlgItemText(IDC_BUTTON_START_OR_CANCEL, "Start");
    }
}

//the thread function
DWORD WINAPI CYourDialog::ThreadFunction(LPVOID param)
{
    //param is actually a pointer to our dialog
    //use it only to access member data,
    //don''t use it to retrieve or change User Interface stuff!!
    CYourDialog* This = (CYourDialog*)param;

    //do your operations there
    for (...)
    {
        ...
        //sometimes, check the m_Cancel variable to know
        //if the user clicked on Cancel button
        if (This->m_Cancel)
        {
            //the user canceled the operation
            //break the loop or do whatever you want to cancel the operation
            break;
        }
        ...
    }

    return 0;
}



不明白,请您提供详细信息
如果您对线程一无所知,请以该链接为起点.
他们有很多关于在CodeProjet或Internet上进行线程化的文章.
使用CreateThread()API创建线程 [



Not understand please can you give details of this
If you don''t know anything about threads, then have a look to this link as a start.
They are lots of articles on threading on CodeProjet or internet.
Creating Threads using the CreateThread() API[^]


使用布尔值-称为"AbortRequested".
首先将其设置为false.
在您的应用程序中,在每个循环的结尾和每个递归例程的开始,请检查"AbortRequested"是否为true.如果是这样,请退出例程.
单击按钮时,将"AbortRequested"设置为true.
Use a boolean value - call it "AbortRequested".
Start off with it false.
In your application, at the end of every loop and the start of every recursive routine, check if "AbortRequested" is true. If so, exit the routine.
On button click, set "AbortRequested" to true.


您还可以尝试在计时器的反应中实现搜索滴答"
然后在过程结束时或按取消"按钮终止计时器...:)
You could also try to implement a searching "tick" inside the timer''s reaction
and just kill the timer at the end of the process or by the cancel button... :)


这篇关于运行对话框事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆