title: C# in WPF: DispatcherTimer - System.UnauthorizedAccessException: Invalid cross-thread access
body: <p>I am trying to implement a timeout for my webclient. All that the client is doing is checking if there is a connection to a server. That part works fine, but by default does not timeout. That's why I am trying to implement a timer which cancels the Async-call. Here is the code:</p>
  
  <pre><code>namespace WPCordovaClassLib.Cordova.Commands
  {
      public class BasicAuth : BaseCommand
      {
          //Create timer to control timeout
          DispatcherTimer timeoutTimer = new DispatcherTimer();
          WebClient webClient = new WebClient();
  
      public void get(string options)
      {
          //Parse data that gets passed into the plugin
          string[] passedData = JSON.JsonHelper.Deserialize&lt;string[]&gt;(options);
  
          //save data to variables
  
          try
          {
              //Set interval for timer, add event handler and start timer
              timeoutTimer.Interval = TimeSpan.FromSeconds(5);
              timeoutTimer.Tick += new EventHandler(timeout);
              timeoutTimer.Start();
          }
          catch (Exception e)
          {
              System.Diagnostics.Debug.WriteLine(e);
          }
          try
          {
              webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
              //Define credentials, set header etc.
              webClient.DownloadStringAsync(uri);
          }
          catch (Exception ex)
          {
              //Catch exception and react
          }
      }
  
      void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
      {
          try{
              //Do stuff
          } catch{
              //Do stuff
          }
          finally
          {
              timeoutTimer.Stop();
          }
      }
  
      private void timeout(Object sender, EventArgs e)
      {
          timeoutTimer.Stop(); //Stop timer from beeing executed again
          webClient.CancelAsync(); //Cancel Async download
          //handle timeout
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>So the idea is to start a timer and if that timer ticks for the first time (after 5 seconds in this case) stop the timer, cancel the Async-part and handle the timeout.
  Unfortunately all I get is the following Exception when I create the timer:</p>
  
  <blockquote>
    <p>An Exception (first chance) of type "System.IO.FileNotFoundException" occured in mscorlib.ni.dll.</p>
    
    <p>An Exception (first chance) of type "System.UnauthorizedAccessException" occured in System.Windows.ni.dll.</p>
    
    <p>System.UnauthorizedAccessException: Invalid cross-thread access.</p>
    
    <p>at MS.Internal.XcpImports.CheckThread()</p>
    
    <p>at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, String s)</p>
    
    <p>at System.Windows.Threading.DispatcherTimer.set_Interval(TimeSpan value)</p>
    
    <p>at WPCordovaClassLib.Cordova.Commands.BasicAuth.get(String options)</p>
  </blockquote>
  
  <p>I haven't really worked with C# before, so there might be a simpler or better solution for it.</p>