Solved

A progress control thread

Posted on 2009-05-15
4
559 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now