Hi,
You can call back to a main thread method using a call back delegate. Pass the methods pointer using a delegate to the thread. Call back using the delegate. For updating text controls in the main thread using method invoker (which again use a delegate).
As a example say I have 2 picture box and two text box. when I click a button i load the two picture box through 2 threads and callback the update text box methods as shown below. You can use background worker in the place of threads as well.
public partial class Form1 : Form
{
private delegate void TextboxPopulate(string txt);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TextboxPopulate del1 = new TextboxPopulate(populateTextBox1Invoker);
ParameterizedThreadStart threadStart1 = new ParameterizedThreadStart(LoadPictureBox1);
Thread thread1 = new Thread(threadStart1);
thread1.Start(del1);
TextboxPopulate del2 = new TextboxPopulate(populateTextBox2Invoker);
ParameterizedThreadStart threadStart2 = new ParameterizedThreadStart(LoadPictureBox2);
Thread thread2 = new Thread(threadStart2);
thread2.Start(del2);
}
private void LoadPictureBox1(object textBoxPolpulateDelegate)
{
pictureBox1.Image = new Bitmap("Flower1.jpg");
object[] args=new object[1];
args[0]="Rose";
Delegate del1 = (Delegate)textBoxPolpulateDelegate;
del1.DynamicInvoke(args);
}
private void LoadPictureBox2(object textBoxPolpulateDelegate)
{
pictureBox2.Image = new Bitmap("Flower2.jpg");
object[] args = new object[1];
args[0] = "Tulip";
Delegate del1 = (Delegate)textBoxPolpulateDelegate;
del1.DynamicInvoke(args);
}
private void populateTextBox1Invoker(string txt)
{
if (textBox1.InvokeRequired == true)
{
this.Invoke((MethodInvoker)delegate
{
textBox1.Text = txt;
});
}
else
{
textBox1.Text = txt;
}
}
private void populateTextBox2Invoker(string txt)
{
if (textBox2.InvokeRequired == true)
{
this.Invoke((MethodInvoker)delegate
{
textBox2.Text =txt;
});
}
else
{
textBox2.Text = txt;
}
}
}
Alternatively you can put all the queries in a async call, when the queries executing you can continue loading the images asynchronously. Once the images loaded you the queries also might be finished and ready to display.
An asynchronous example....
add a button to the above example and have delegate declaration at the top (global to form/ form instance variable)
private delegate string QueryDelegate();
say this delegate for an example busy method and finally it returns a string.
the busy method...
private string myQueryProcess()
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(1000);
}
return "Query Completed";
}
now in the button click event...
private void button2_Click(object sender, EventArgs e)
{
QueryDelegate del1 = new QueryDelegate(myQueryProcess);
IAsyncResult result= del1.BeginInvoke(null,null);
pictureBox1.Image = new Bitmap("Flower1.jpg");
pictureBox2.Image = new Bitmap("Flower2.jpg");
string resultStr=del1.EndInvoke(result);
MessageBox.Show(resultStr);
}
you can see the images will be loaded without waiting for the busy method to be finished. the busy method will finish after 5 seconds.