title: C# ListView doesn't update
body: <p>I reckon the problem of C# win forms not being repainted well in certain circumstances is covered in different places, however, I didn't manage to solve my problems by using the simple snippets I found on the web.</p>
  
  <p>My problem : on a form I have a listView, which I associate to a custom data holder (2 columns, a key and a last update date). From diverse places, I need to call the updateTime(key) method which then replicated the changes in the GUI. The model gets changed but my listView never.</p>
  
  <p><strong>I have a form containing a ListView that looks like that :</strong></p>
  
  <pre><code>partial class VolsPane : UserControl, IGUIPane 
  {
     private ListView listView1;
     private ListModel listModel1;     //ListModel is 100% homemade
     ...
     public VolsPane()
     {
         ...
         listModel1.setList(listView1);
     }
  }
  </code></pre>
  
  <p><strong>And the class holding data for my listView is like that :</strong></p>
  
  <pre><code>class ListModel
  {
      private Dictionary&lt;string, DateTime&gt; Underlying;
      private ListView list;
  
      ...
  
      public ListModel(string nexusKey)
      {
          ...
      }
  
      ...
  
      public void setList(ListView list)
      {
          this.list = list;
      }
  
  
      public void updateTime(string ric)
      {
          Underlying[ric] = DateTime.UtcNow;
          updateView();
      }
  
      public void updateView()
      {
          this.list.Clear();
          this.list.Items.AddRange(this.underlyingToListItems());
      }
  
      ...
  
      public ListViewItem[] underlyingToListItems()
      {
          ListViewItem[] res = new ListViewItem[Underlying.Keys.Count];
          int i = 0;
          foreach (string ric in Underlying.Keys)
          {
              res[i] = new ListViewItem(new string[] { ric, Underlying[ric].ToString("MMM-dd hh:mm:ss") });
              i++;
          }
          return res;
      }
  
  }
  </code></pre>
  
  <p>I do realize the problem is in my updateView(). In debug, the code goes there definitely. Believing the problem was to be solved with an asynchronous "invoke", I referred to this post which appeared to be a reference : <a href="https://stackoverflow.com/questions/2367718/c-automating-the-invokerequired-code-pattern">Stack overflow : Automating the invoke...</a></p>
  
  <p><strong>Then tried this :</strong></p>
  
  <pre><code>    private void updateView()
      {
          if (this.list.InvokeRequired)
          {
              this.list.Invoke(new MethodInvoker(() =&gt; { updateView(); }));
          }
          else
          {
              this.list.Items.Clear();
              //this.list.Clear();
              this.list.Items.AddRange(this.underlyingToListItems());
          }
      }
  </code></pre>
  
  <p>It builds but has no effect. In debug mode, never goes in the 'if' branch, always the 'else'.</p>
  
  <p><strong>Then this :</strong></p>
  
  <pre><code>    private void updateView()
      {
          this.list.Invoke((MethodInvoker)delegate
          {
              this.list.Items.Clear();
              //this.list.Clear();
              this.list.Items.AddRange(this.underlyingToListItems());
          });
      }
  </code></pre>
  
  <p>I get an "InvalidOperationException : Invoke or BeginInvoke cannot be called on a control until the window handle has been created."</p>
  
  <p>What is the obvious thing I must be missing here ? Or is my problem actually not the one i think ?</p>
  
  <p>Thanks guys !</p>