title: WCF ASynchronous calls within a Windows forms Application using a timer
body: <p>I am new to developing with Windows forms applications and I have an issue I could do with some help / guidance on the best practice approach I should be taking.</p>
  
  <p>Background:</p>
  
  <p>I have an forms based application that needs to refresh every minute with data from a WCF service and then update the UI with the latest results in a List View control. I have this all working in principal but the application crashes intermittently and I need to resolve this. I have stripped out the main parts of the code below. The actual nuts and bolts are probably not required.</p>
  
  <p>Code:</p>
  
  <pre><code>public partial class Form1 : Form
  {
  readonly MatchServiceReference.MatchServiceClient _client = new MatchServiceReference.MatchServiceClient();
  
  public Form1()
      {
          _client.GetMMDataCompleted += new EventHandler&lt;GetMMDataCompletedEventArgs&gt;(_client_GetMMDataCompleted);
      }    
  
  //Arm Timer
  private void ArmTimer(object sender, EventArgs e)
      {
          aTimer = new Timer();
  
          aTimer.Elapsed += OnTimedEvent;
  
          aTimer.Interval = 60000;
          aTimer.Enabled = true;
      }
  
  //Timer elapsed event
   private void OnTimedEvent(object source, ElapsedEventArgs e)
      {
          try
          {
              LoadMatches();
  
              if (LastUpdated.InvokeRequired)
              {
                  LastUpdated.Invoke(new MethodInvoker(delegate { LastUpdated.Text = e.SignalTime.ToString(); }));
              }
          }
          catch (Exception exception)
          {
  
          }
  
      }
  
  //Load matches
  private void LoadMatches()
      {
          try
          {
              aTimer.Enabled = false;
              _client.GetMMDataAsync();
          }
          catch (Exception e)
          {
              //EventLog.WriteEntry("Application", e.InnerException.ToString(), EventLogEntryType.Error);
              aTimer.Enabled = true;
          }
      }
  
  void _client_GetMMDataCompleted(object sender, GetMMDataCompletedEventArgs e)
      {
          if (e.Result != null)
          {
  
              var matches = e.Result;
  
              Debug.WriteLine("Matches received from service");
  
              if (!IsHandleCreated &amp;&amp; !IsDisposed) return;
  
              // Invoke an anonymous method on the thread of the form.
              Invoke((MethodInvoker) delegate
                                         {
                                             try
                                             {
                                                 LoadTheMonitorMatches(matches);
                                             }
                                             catch (Exception exception)
                                             {
                                                 Debug.WriteLine("INNER EXCEPTION" + exception.InnerException);
                                                 Debug.WriteLine("EXCEPTION MESSAGE" + exception.Message);
                                             }
                                         });
          }
          aTimer.Enabled = true;
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Exception:</p>
  
  <p>The exception I am getting is as follows -</p>
  
  <blockquote>
    <p>Exception Info: System.Reflection.TargetInvocationException
    Stack:
       at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
       at MM.MatchServiceReference.GetMMDataCompletedEventArgs.get_Result()
       at MM.Form1._client_GetMMDataCompleted(System.Object, MM.MatchServiceReference.GetMMDataCompletedEventArgs)
       at MM.MatchServiceReference.MatchServiceClient.OnGetMMDataCompleted(System.Object)
       at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
       at System.Threading.ExecutionContext.runTryCode(System.Object)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
       at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</p>
  </blockquote>
  
  <p>Any help / advice with this would be greatly appreciated.</p>
  
  <p>Cheers</p>