title: Thread Dispatcher Sleep in WPF
body: <p>I'm trying to create a Kinect + WPF Application. The Kinect part works fine; now I want to create a method to check if the page is idle (no user is interacting with the program for a specific amount of time). If it is idle for 5 seconds, the screen will be locked. This is the algorithm:</p>
  
  <ol>
  <li>Start Thread (I need to use Dispatcher as the thread needs to modify WPF elements)</li>
  <li>Check if user is using. If not, then increase Count by 1.</li>
  <li>If Count == 9 (which means that already 5 seconds have passed), lock the screen and set Count to 0 again.</li>
  <li>Sleep for 500 milliseconds.</li>
  <li>Repeat Step 2-4</li>
  </ol>
  
  <p>This is my code. The method "startLockHandler" is invoked when the application starts.</p>
  
  <pre><code> public void startLockHandler()
   {
       Application.Current.Dispatcher.BeginInvoke(new ThreadStart(() =&gt; lockHandler()), null);
   }
  
   public void lockHandler()
      {
          while (true)
          {
              if (myState.isSkeletonTracked == false) //if skeleton is no longer tracked 
              {
                  if (myState.ActionAllowed == true) //if the page is not in transition
                  {
                      lockCount++;
  
                      if (lockCount &gt;= 10)
                      {
                          lockCount = 0;
  
                          myState.ActionAllowed = false;
  
                          //LOCKING MECHANISM INSERTED HERE. NEED TO MODIFY SOME WPF ELEMENTS
                          myState.ActionAllowed = true;
                      }
                  }
              }
  
              else
              {
                  lockCount = 0;
              }
              Console.WriteLine("lockHandler: THREAD SLEEP CALLED");
              Thread.Sleep ( 500 );
  
          }//end while
      }//end method lockHandler 
  </code></pre>
  
  <p>When I run the application, the application hangs right after it is started. What I think happened is that Thread.Sleep (500) instructs the main thread to sleep (Please correct me if I'm wrong). My question is how can I specify which thread to be put to sleep?
  BTW, I'm not an expert in C# and a newbie in Threading :(</p>
  
  <p>Thanks for your attention and hopefully you can help me :)</p>