Link to home
Start Free TrialLog in
Avatar of samroyc
samroyc

asked on

multi threaded application?

I have  an MFC based dialog application which sends images to a device after receiving these images over the network.
while the images are being sent to the device the app is frozen-I cannot use any of the other radio buttons.
Is there anyway I can keep it locally responsive while it receives data and sends it to the device.basic literature serach
suggests thati need multi threaded application,I have no expierence with that,So any help or refernces will be appreciated.
thanks
here is a part of the code where the app is no longer locally responsive
time( &ltime0 );
            do
            {
                  time( &ltime );
                  ShowImage(strImage);
            }
            while(ltime<ltime0+10);
Avatar of J-A-L
J-A-L

Not sure if you have this available, but possibly in the loop provide more time slices to your computer to process system messages.  I know in other languages you can add something like

do
          {
               time( &ltime );
               ShowImage(strImage);
               ProcessMessages();
          }


I don't know what the C equivalent is in your case... but maybe you can find something? hehe

Application->ProcessMessages(); // borland C++

Hope this helps

Jeff
time( &ltime0 );
          do
          {
               time( &ltime );
               ShowImage(strImage);
          }
          while(ltime<ltime0+10);

from this above code ...  
the line time (&ltime); calls that function itself and create a recursion. so it cannot come back.
i don't what you intended to do ...but try changing the do ... while to while.

if I am wrong please feel free to ask more questions
Avatar of samroyc

ASKER

Thankyou for your interest and response
time_t ltime, ltime0;// define ltime and ltime0 of the type time
 time( &ltime0 );    //gives the time at ltime0,
          do
          {
               time( &ltime );
               ShowImage(strImage); //function specific to the device control
          }
          while(ltime<ltime0+1);     //loop stops when ltime is incremented by 10 seconds
The program does come out of the loop after 10 seconds.My query is if I can still keep lthe application responsive
to messages during these 10 seconds as eventually the 10 second span will be much longer
printing out time(&ltime0) at beginning gives 115912276
printing out time(&ltime0) at end gives 115912277
ASKER CERTIFIED SOLUTION
Avatar of rushtoshankar
rushtoshankar
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samroyc

ASKER

Thanks for the suggested code...
where do I define gIntTimerID?
I get compiler error messages
 error C2065: 'gIntTimerId' : undeclared identifier
 error C2061: syntax error : identifier 'KillTimer'

here is what I tried..
{void CD1100DemoDlg::OnTimer(UINT nIDEvent)

// TODO: Add your message handler code here and/or call default
      time_t ltime, ltime0;
      time( &ltime0 );
          do
          {
               time( &ltime );
               ShowImage(strImage); //function specific to the device control
          }
              KillTimer(gIntTimerId);
          CDialog::OnTimer(nIDEvent);
}

and
strImage="recvdfile.ddc";
SetTimer (gIntTimerId, 200,0);
instead of the original loop.
Declare the variable globally or in the 'CD1100DemoDlg' class.

gIntTimerId --> i put 'g' infront of the variable to indicate that it is a global integer variable.
initialize this variable in 'CD1100DemoDlg::OnInitDialog()' function to a non-zero value.
Avatar of samroyc

ASKER

Thank you.I have a couple of questions about the code
1)Since I have a do-while loop inside of OnTimer,which runs for 10 secs what is the time out value good for?
2)Also if calling OnTimer starts a new thread-then should I not be able to Process other messages
(for ex the dialog app will respond to other click of radio buttons).I cant.
Here is what I tried..
gIntTimerId=10;(defined in the CD1100DemoDialogClass)
In the main code
SetTimer(gIntTimerId,200,0);
OnTimer(gIntTimerId);
void CD1100emoDialog::OnTimer(UINT nIDEvent)
{
     // TODO: Add your message handler code here and/or call default
          do
          {
               time( &ltime );
               ShowImage(strImage); //function specific to the device control
          }
          KillTimer (gIntTimerId);
          CDialog::OnTimer(nIDEvent);
}
use 15000  thousand as timeout value (answer to your first question).
Just I want to tell you one thing ..

time out can be anything base on your application.
I will explain it.

let me assume your ShowImage displays some image or some value that is read from a device.
while reading from the device, do the following steps instead of reading all the values from the device at one stretch.
1. Read some values from the device and return from the function.
2. If you do so, after the specified interval (mentioned in SetTimer Function), this function will be called again.
3 .This time you can continue your operation where you left before. you can be very accurate in miilsecs.

I hope this will help you. don't hesitate to ask questions.