Solved

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

Posted on 2004-09-27
12
162 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
Comment Utility
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
Comment Utility
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
Comment Utility
Threads create additional overhead and gotchas, though...
0
 

Author Comment

by:Galilea
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

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

Author Comment

by:Galilea
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
Check out MFC sample MTRECALC.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

11 Experts available now in Live!

Get 1:1 Help Now