WinForms Tab Control Causing Cross-Thread Operation

154 Views Asked by At

This is the error I am receiving. I have a button on a winforms control (as depicted below). I have an async void method that is attached to the click event handler (as depicted below) Error Button on second tab

The issue that I am having is that this code runs perfectly fine when I first load in this form, I can click the button 1,000+ times and there is no cross threading errors that occur.

The ONLY time that I get this error is when I do the following

  1. hide the form form.Hide() (with keeping a reference to the form)
  2. show the form for a second time form.ShowDialog()
  3. click the refresh button

It seems like all of the buttons on the form will then have some sort of cross-threading issue when calling into UI controls from async method calls. It is almost as if hiding and then re-showing the dialog does something to make new controls on a new thread BUT it never calls the Initialize method again.

Does anyone know why I am having an issue?

EDIT This is also a VSTO excel addin project. The form is getting created by a ribbon button click and I hold on to a reference of the form in the ribbon class to re-show it

0

There are 0 best solutions below