[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

multi threaded application?

Posted on 2005-05-12
9
Medium Priority
?
287 Views
Last Modified: 2006-11-18
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);
0
Comment
Question by:samroyc
  • 5
  • 3
9 Comments
 
LVL 5

Expert Comment

by:J-A-L
ID: 13987382
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
0
 
LVL 6

Expert Comment

by:rushtoshankar
ID: 13987447
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
0
 

Author Comment

by:samroyc
ID: 13987809
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
0
Industry Leaders: 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!

 
LVL 6

Accepted Solution

by:
rushtoshankar earned 2000 total points
ID: 13993218
simply try this...

first create an OnTimer function using classwizard for that dialog.
in that function copy your code ..

void CTest4Dlg::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);
}

now this function is inactive... now in the original code ..

          time( &ltime0 );
// we removed the following code and copied in timer function.
>          do
>          {
>               time( &ltime );
>               ShowImage(strImage);
>          }
>          while(ltime<ltime0+10);
// instead of this original code here do the follwoing .
            SetTimer (gIntTimerId, 200,0);// this will create new thread and execute that code. after executing that function
                                                         // kills itself. that implies your code has been executed only once.
                                                         // first value is the timer ID.. any non-zero value(same should be given to killtimer).
                                                         // second value is time out value in millisecs. (any value > 100)

try this.. hope this helps you
0
 

Author Comment

by:samroyc
ID: 14031863
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.
0
 
LVL 6

Expert Comment

by:rushtoshankar
ID: 14033382
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.
0
 

Author Comment

by:samroyc
ID: 14036219
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);
}
0
 
LVL 6

Expert Comment

by:rushtoshankar
ID: 14091808
use 15000  thousand as timeout value (answer to your first question).
0
 
LVL 6

Expert Comment

by:rushtoshankar
ID: 14110769
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Make the most of your online learning experience.
Six Sigma Control Plans
Progress

830 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