Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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;


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;
}


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[^]
 
Share this answer
 
v2
Comments
[no name] 3-Mar-11 2:48am    
Not understand please can you give details of this
Olivier Levrey 3-Mar-11 3:52am    
I updated my answer and gave you a link so you can learn about threads.
Espen Harlinn 4-Mar-11 12:36pm    
Good effort, my 5
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.
 
Share this answer
 
Comments
Olivier Levrey 2-Mar-11 8:29am    
This is not enough. OP needs to use a thread.
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... :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900