Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Abort background process....

Posted on 1997-07-29
7
Medium Priority
?
295 Views
Last Modified: 2013-11-20
I have application which takes a few menutes to load data into tree control, I need to give user a chance to abort this process. I would like to display modal dialog with some status data and button "Abort" which user should press to abort loading. The problem is how to make application load data and be able to respond to "Abort" button command in modal dialog. Any help would be appriciated.
0
Comment
Question by:gelbert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 3

Expert Comment

by:vinniew
ID: 1302885
create a class to to the work.
create a global function called, say, MyThreadProc.

start it with:


int g_bTurnMeOff = FALSE;

CDialog::OnButton()
{
if(m_bThreadHasntStarted) {
m_pWinThread = AfxBeginThread(...MyThreadProc...);
} else {
// send message to your working class with a global variable, data segment, or something similar.
g_bTurnThreadOff = TRUE;
}

MyThreadProc(void *Handle)
{
WorkingClass MyWorkingClass;
// use the handle to send messages to the dialog
while(g_bTurnThreadOff == FALSE) {

MyWorkingClass.dosomething();
}
}


BTW, I've got a better example somewhere in the question history files.  

Post a more specific question if you need to.

V


0
 
LVL 8

Author Comment

by:gelbert
ID: 1302886
Thanks for answer but I can not make my function which reads data a worker thread because it has some UI actions. My idea was to create UI thread and run dialog on top of it. But I could not make it work. There were an artical in MSJ some time ago about how to do it but I can not find it.
0
 
LVL 7

Expert Comment

by:faster
ID: 1302887
You can do it this way:

The main thread will take care of the loading, before the time-consuming process begins, it can create a new thread, this thread can create a modeless dialog box (I don't why you want it to be model), after the working thread show the dialog box, it can create its own message loop and just wait it to end.  The main thread will send messages to the modeless dialog box when necessary (it should be able to get the handle of it).  The dialog box's control proc will handle the user cancel: terminate the dialog (and the thread's message loop) and also send some message to the main thread to notify the event (or set some variable so that the main thread will know somehow).

This method seems to be working well for me and I think it is quite flexible.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:vinniew
ID: 1302888
UI:

That's why you pass the window handle as the arg to the worker thread process.

0
 
LVL 8

Author Comment

by:gelbert
ID: 1302889
Thanks guys,

   I tried to do something like this in y original version but it did not work.
Faster, could you please show some code as an example?

0
 
LVL 7

Accepted Solution

by:
faster earned 200 total points
ID: 1302890
OK, just an example:

Main thread:

hProgressDlg = NULL;  // set the handle of the modeless dialog box to NULL first
hRptThrd=(HANDLE) _beginthreadex(NULL, 0, ProgressReport, NULL, 0, &tid);  // create the thread for model dialog box
while (hProgressDlg == NULL)      {  // wait until the modeless dialog box is created
    Sleep(0);
}
                        
r = MainProcess();  // do the main process

if (hProgressDlg) // if the dialog is still there, destroy it
    SendMessage(hProgressDlg, WM_DESTROY, 0, 0);

WaitForSingleObject(hRptThrd,INFINITE);  // wait for the child thread to terminate
CloseHandle(hRptThrd);  // close the handle of child thread


Child thread:

unsigned int WINAPI ProgressReport(void *ptr)
{
      MSG msg;

      hProgressDlg = CreateDialog(hUIDll, MAKEINTRESOURCE(IDD_PROGRESSDLG), hwnd, (DLGPROC) pfnInProgress); // create the modeless dialog
      
      ShowWindowAsync(hProgressDlg, SW_SHOWNORMAL);  // show it

        // create message loop
      while (GetMessage(&msg, NULL, NULL, NULL) == TRUE)      {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
      }
        // destroy the dialog box if necessary
      if (hProgressDlg)      {
            DestroyWindow(hProgressDlg);
            hProgressDlg = NULL;
      }

        // end thread
      _endthreadex(0);

      return 0;
}


BOOL CALLBACK pfnInProgress(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
      switch (iMsg)      {
            case      WM_COMMAND:      
                  switch (LOWORD(wParam))      {
                        case      IDCANCEL:
                              bCancel = TRUE;
                              DestroyWindow(hDlg);
                              hProgressDlg = NULL;
                              return TRUE;
                  }
                  break;
            case      WM_DESTROY:
                  PostQuitMessage(0);
                  return TRUE;
      }
      return FALSE;
}


The main thread shall check bCancel to decide whether abort the process.  Of course, you can use some other way to notify the main thread.

Hope this helps
0
 
LVL 8

Author Comment

by:gelbert
ID: 1302891
Thanks for answer. It is not really MFC code but I can get there myself
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question