Pause, how to detect window movement or control (associated with main class) event taking place during pause?.

I have several window forms object associated with this main window form1  (ie displaybox myDis = new displaybox();), if someone take control of window form (there is displaybox, measurementbox, FFT box all interlinked), it force program to hang and resulting premature timeout delay trigger (in software..see below). This resulted timesenstive program to assumed network failure (which is in fact not) from an instrument using network. (I could consider timeout feature within the TCP socket if there is no data arriving, but let consider this problem anyway)

The aim here is simply if someone moved the window or take control (which cause other inner program to hang) the pause routine become aware and set a flag hence will not generates premature timeout signal. I need to know how to detect such event as described here.

I use Application.Doevent() to keep control available (so I can halt activity process). I avoiding Sleep method because it hang program hence preventing control availability.  This routine is not effecient because it take CPU time during the loops.

=======================================================================
            private void button1_Click(object sender, System.EventArgs e)
            {
                  bool wincontrolstate=false;
                       string whichwindow = "".
                  textBox2.BackColor=Color.Green;
                  while(true)
                  {
                        Pause(10000, ref wincontrolstate, ref string whichwindow);
                        if (wincontrolstate==false) break;                  
                                //window associated with this object has moved or interfered, try pause again.
                        // performs networking activity, waiting to collecting data from an instrument (async operation).  
                  }
                  textBox2.BackColor=Color.Goldenrod;
            }

            public void Pause(int periodmSec, ref bool wincontrolstate, ref string whichwindow)
            {
                  DateTime starttime;
                  DateTime endtime;
                  TimeSpan ts;
                  while (true)
                  {
                        endtime=DateTime.Now;
                        ts = endtime.Subtract(starttime);
                        Application.DoEvents();
                        if (ts.TotalMilliseconds>periodmSec) break;
                        // if windowmoved or control is detected, set wincontrolstate=true, identify which window (ie window class name) and transfer into whichwindow and then break loop.
                  }
            }
      }
========================================================================

I hope to learn more of thread and delegate this afternoon, so I can seperates the window displaybox, measurementbox, FFT box, so that it avoid hanging the main timesenstive routine, but meanwhile let consider the problem above.

Thanks
riscyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
This is main problem in using DoEvents. This function doesn't return all time window has input messages, for example, when user is moving the window or scrollbar. You need to use multithreading instead of DoEvents.
riscyAuthor Commented:
What do you mean "doesn't return all time window as input message"?
AlexFMCommented:
Application.DoEvents function has the following algorithm (pseudo-code):

while (message queue is not empty)
{
    extract message from message queue
    handle message
}

If there is endless stream of messages, DoEvents doesn't return. This happens, for example, when user moves window using title bar, scrolls window using scrollbar. DoEvents returns only when user stops this and message queue becomes empty.
Generally, DoEvents is quick and cheap solution. There are many cases when it is acceptable. For example, you can see that possibly MS Word uses this approach to show graphics inside .doc file: all time user scrolls a document, images are not shown.
Real solution, however, is multithreading. In your case, you need to do this, because DoEvents gives unacceptable results.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

riscyAuthor Commented:
After bit of consideration with multithreading and found it create more complex solution since there is need to keep it sync without tying up the CPU resource.

It seem clear to me that sleep solution is not best solution since it freeze main program.
However once I create the thread seperates from the main program and use sleep instead of DoEvent. The main program continue active enabling control for use to operates, however once the thread sleep expired, how to return back to the location where it make the call.

I have broken up the routine where I used event based where once sleep routine expired, it create the event and excutes this routine. It messy but work....it just messy.

I need to sync with equipment which is slow in sending data via network (byte by byte basis), the routine use software timeout which force quit if there is no more bytes. I guess I would have to consider to overhaulling the network based routine to otpimise the operation, avoiding doevents. perhaps try out the network timeout (as long it does not lose socket).

Riscy
   
riscyAuthor Commented:
I going to try do some more experiment with threading, I just made a start last weekend.

Any useful training stuff with practical application would be welcome. Is there best book covering threads?.

Riscy.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.