I'm working on a C# application.
I have an object used like a dialog box :
public partial class MyDialog : Form
The main windows is :
public class MyForm : Form
I can use it by :
public class ProgramApp
{
public static MyForm MainForm { get { return _mainForm; } }
}
In one function I can use both with :
MyDialog dlg = new MyDialog();
dlg.ShowDialog(ProgramApp.MainForm);
First call works good, but at the second one I have a System.InvalidOperationException with HResult = 0x80131509 and message :
Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on.
(translated from french, original message :)
"Opération inter-threads non valide : le contrôle 'MainForm' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé."
What can I do to fix that ?
Edit :
I added this piece of code :
MyDialog dlg = new MyDialog();
bool needInvokeDlg = dlg.InvokeRequired;
bool needInvokeForm = ProgramApp.MainForm.InvokeRequired;
dlg.ShowDialog(ProgramApp.MainForm);
First attempt both are false. On second, needInvokeForm is true. How can I do an Invoke to set ProgramApp.MainForm as a parameter ?