Solved

A progress control thread

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

19 Experts available now in Live!

Get 1:1 Help Now