Solved

A progress control thread

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 108
How to determine file rename from EventLog ? 2 55
centeredAverage challenge 8 180
mapAB Challlenge 35 160
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

821 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