Solved

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

Posted on 2004-09-27
12
165 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 86
conditional code and condition difference 9 82
java ^ examples 8 66
Line meaning 9 86
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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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