I've this situation:
Form "Menu" where you can click "New" and an insert form (with showdialog) will appear. If you insert data, I make an INSERT query on the database and I close this form. After showDialog() statement, I have a lot of methods who performs a lot of operations in a sequential way (MUCH IMPORTANT) and sometimes update a dataGridView. After all this computation, I simulate press of "new button" in order to allow user to insert a new item. This is an example:
private void buttonNew_Click(object sender, EventArgs e)
{
DialogResult dr = new DialogResult();
InsertForm form = new InsertForm();
dr = form.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
Method1();
Method2();
if (dataGrid2.RowCount > 0)
{
Method3();
Method4();
Method5();
Method6();
Method7();
Method8();
}
bNew.PerformClick();
}
}
The problem is that the "New" form (bNew.PerfomClick()) appear after a 2-3 seconds and I can't wait so much time. So I tried to create a method who include Method1 to Method8, run it in a new Thread and execut bNew.PerfomClick(), but this doesn't works because a lot of my methods update a datagridView.
Is there a way to solve this problems? Sorry for my bad english.
------------UPDATE------------------
Now I'm trying this code:
delegate string OperazioniDelegate();
private string Operazioni()
{
if (!InvokeRequired)
{
Method1();
Method2();
............
}
else
Invoke(new OperazioniDelegate(Operazioni));
return "";
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Operazioni();
}
In this way, new insert form is showed instantly but his UI is blocked until the backgroundWorker End work..