?
Solved

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

Posted on 2004-09-27
12
Medium Priority
?
169 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
[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
  • 3
  • 3
  • 2
  • +1
12 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 1000 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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

801 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