Solved

How to update display and do the computation at the same time.

Posted on 2004-09-27
12
163 Views
Last Modified: 2013-11-20
I have to do some computation while printing out different steps of it. In the same application I have a real time clock which uses ONTIMER event however, it freezes once the computation starts so cannot use it to update edit control. How can I update the edit control with the text saying what it is doing at the same time as doing the computation?

Thank you.
0
Comment
Question by:Galilea
  • 3
  • 3
  • 2
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 12161510
You can do that by calling the following function frequenty:

void DoEvents () {

    MSG msg;

   while   (   PeekMessage (   &msg,   NULL,   0,  0,  PM_REMOVE))
           {
               DispatchMessage     (   &msg);
           }
}

This will take care that your messages are dispatched and handled correctly, keeping the UI responding.
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 total points
ID: 12161521
You have to create a new thread for computation.
The simplest answer is:

DWORD IdThread;
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)YourComputingFunction, 0, 0, &IdThread);

Your function must be declared as
DWORD WINAPI YourComputingFunction(LPVOID);

and implemented as:

DWORD WINAPI YourComputingFunction(LPVOID lParam)
{
   // your calculus here
}

If you want to know more about multithreading, read codeproject articles:
http://www.codeproject.com/threads/

0
 
LVL 86

Expert Comment

by:jkr
ID: 12161602
Threads create additional overhead and gotchas, though...
0
 

Author Comment

by:Galilea
ID: 12162013
Jaime, just tried it,
Do I have to put the computation on the thread or the updating. At the moment I have the following:

::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ComputingFunction, 0, 0, &IdThread);

CPDRecorder_alphaApp::m_LogTrace.WriteLine("Finished Acquisition");
UpdateData();
ss.Format("%s\tFinish ACQ\r\n", COleDateTime::GetCurrentTime().Format("%d/%m/%y, %H:%M:%S "));
m_strStatus += ss;  
UpdateData(FALSE);

// Create filename string
sTime.Format("%s", COleDateTime::GetCurrentTime().Format("%H%M%S"));
for (numChan = 1; numChan<5; numChan++)
{
      ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SavingFunction, 0, 0, &IdThread1);
      UpdateData();
      ss.Format("%s\tCH%i: Timestamp:%i Saved\r\n", COleDateTime::GetCurrentTime().Format("%d/%m/%y, %H:%M:%S "),
            numChan, iFrameCount);
      m_strStatus += ss;  
      UpdateData(FALSE);
      fclose(outfile);
      UpdateData();
      ss.Format("%s\tCH%i: Saved\r\n", COleDateTime::GetCurrentTime().Format("%d/%m/%y, %H:%M:%S "),
            numChan);
      m_strStatus += ss;  
      UpdateData(FALSE);
      CPDRecorder_alphaApp::m_LogTrace.WriteLine("CH%i Saved", numChan);
}

Now, this probably sounds odd (it sounds odd to me) but if I do not put the thread ComputingFunction, and just put in in serial, the UpdateData does not occur, it just jumped onto the acquisition without updating. Now that I have put the thread, it jumps to the acquisition without having finished computing... does this makes sense. If so, how can I fix it?
0
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.

 

Author Comment

by:Galilea
ID: 12162021
... by the way, it's not all the code, just the extracts which I thought were of interest.
0
 

Author Comment

by:Galilea
ID: 12162070
Should I use jkr's technique, however, not too sure how I'm supposed to use it, sorry jkr
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12162125
It's a bit confussing, but I guess you want to show some progress information of your calculus somewhere.
You can do it from your calculation thread passsing some messages to your dialog, or you can put progress information in some global variable or object, and update dialog "asynchronously" with a timer in your dialog that reads this global information.
0
 
LVL 86

Expert Comment

by:jkr
ID: 12162189
>>however, not too sure how I'm supposed to use it

If this is your computing loop, use

for (numChan = 1; numChan<5; numChan++)
{
    DoEvents ();

    // do calculations
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12167018
Check out MFC sample MTRECALC.
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

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 74
isEverywhere  challenge 19 72
Window placement 17 75
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 250
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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

22 Experts available now in Live!

Get 1:1 Help Now