I have application which has some networking code which runs asynchronously. I have attached some events to be thrown when no connection to server and I'm creating some "operation failed" form when this happens. The problem is that my form hangs after creation. I read about that and I tried to do with:
public void ShowView()
{
if (this.InvokeRequired)
{
Action a = new Action(ShowView);
this.Invoke(a);
}
else this.Show();
}
And problem was still present. Than I found that if control was not been created that InvokeRequired returns false. So I at my initialization code added:
this.show();
this.hide();
This way it seems to be working. But it is pretty ugly and when my app starts I can see for a moment my form being shown and than disappears. How should I make my form to create all of it controls without showing it, or is there some better solution to this problem?
EDIT: More information. I'm using MVP design pattern. I have Presenter which have dependency to IView. My form implements IView. IView has this ShowView() and HideVIew() methods which I call from my presenter. My presenter receives event from another thread. So Where should I do this thread jumping or how should I solve this?
EDIT2: Here sample app illustrating problem:
public partial class Form1 : Form
{
Form2 form;
public Form1()
{
InitializeComponent();
form = new Form2();
}
private void button1_Click(object sender, EventArgs e)
{
//form.Show();
//form.Hide();
Thread t = new Thread(new ThreadStart(ShowForm2));
t.Start();
}
private void ShowForm2()
{
if (form.InvokeRequired)
{
Action a = new Action(ShowForm2);
form.Invoke(a);
}
else
{
form.Show();
Thread.Sleep(5000);
}
}
}
Can you tell me on this concrete problem what to change?