Solved

A progress control thread

Posted on 2009-05-15
4
563 Views
Last Modified: 2013-11-20
Hiya

I have an SDI application in which I am pressing a button on the toolbar and lengthy calculations are performed in the CDocument derived class.

What I would like to happen is that once the button is pressed a progress control appears and displays the progress of the calculations.

My idea is to set up some of thread which creates the progress control and in some way the document notifies the thread of the progess.

I'm not too familiar with threads so need quite a bit of help to set this up. Can anybody give me the steps on how to do this?

John
0
Comment
Question by:IssacJones
[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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
alb66 earned 200 total points
ID: 24393238
In my opinion, the best solution is to perform the lenghty caulcation in the worker thread, posting some message to the the main thread in order to update the user interface.
This is a good tutorial:
"Threads in MFC Part I: Worker Threads"
http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=435
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 50 total points
ID: 24393413
You might want to look at
http://www.experts-exchange.com/Programming/System/Windows__Programming/MFC/Q_21264076.html?sfQueryTermInfo=1+10+doevent
it is not another thread but a simple way to do what you want without threads.
0
 

Author Comment

by:IssacJones
ID: 24393967
Hi guys

Thanks for your help. I've taken your advice alb66 and performing the calculations (modifying a bitmap) within a worker thread. It all looks okay but there is one thing that I'm uncertain how to do i.e. what if I want to terminate the calculations from the calling dialog which has the progress control? That is, how do I stop the calculations and end the thread before the calculations are complete?

At the moment I have to wait until the thread has completed. When it does this it posts a message from the documet to the dialog to say it has finished. It then deletes the thread immediately after posting the message from within the document.

John
0
 
LVL 19

Assisted Solution

by:alb66
alb66 earned 200 total points
ID: 24394045
You can share a CEvent between the event (you can pass it to worker thread as parameter, or (worst) you can declare it as global).

// declare it as member of the dialog
CEvent      m_evExit

// initalize it in the constructor
CYourDlg::CYourDlg() : m_evExit( FALSE, TRUE )
{
}

// pass it to the thred when you start it
AfxBeginThread( YourThread,  (LPVOID)(HANDLE)m_evExit, ...


//In the dialog you can signal it
      m_evExit.SetEvent();


//In the worker thread you can test it:
void  YourThread(  LPVOID pParam )
{
   HANDLE hExit = (HANDLE) pParam;

  while( 1 )
  {
     if ( WaitForSingleObject( evExit, 0 ) == WAIT_OBJECT_0 )
       return;

     // do something usefull
  }
}
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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