Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

A progress control thread

Posted on 2009-05-15
4
Medium Priority
?
564 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 800 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 200 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 800 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

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.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

664 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