Question : Multithreading in C# .NET

I'm relatively new to software development in .NET, and I'm wanting to use multithreading in my application to prevent the GUI from freezing while a method with a long DB query executes.
My instance method sets an instance field which should not be referenced until the instance method is complete.  How can I do this?

Attached is some code to demostrate what I'm doing:



I got as far as replacing:

myObject1.queryRecent();

With:

Thread secondThread = new Thread(new ThreadStart(myObject1.queryRecent));
secondThread.Start();

But obviously, label1.Text = myObject1.recentString;  is executed while the method is still running.

How can I prevent this line executing until the queryRecent method has finished executing?

Thanks in advance,
Matt
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
public partial class Form1 : Form
{
   Class1 myObject1 = new Class1();
   public Form1()
   {
       InitializeComponent();
   }

   private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
   {
       if (e.KeyChar == 13)
       {
           myObject1.myString = textBox1.Text;

           //Run this method in a second thread, so the GUI doesn't freeze while it executes:
           myObject1.queryRecent();

           //Don't execute until myObject1.queryRecent is complete
           label1.Text = myObject1.recentString;
} } }


class Class1
{
    public string myString = "initial myString";     //set externally
    public string recentString = "initial recentString";	//set by queryString
    public string allString = "initial allString";    //set by queryAll

    public void queryRecent()
    {
        //simulate a long database query:
        Thread.Sleep(3000);  
        recentString = myString + ". Some more text";
    }

    public void queryAll()
    {
         //simulate a long database query:
         Thread.Sleep(5000); 
         allString = myString + ". Some other text";
    } 
}

Answer : Multithreading in C# .NET

You can use a BackgroundWorker() like this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
    public partial class Form1 : Form
    {

        private System.ComponentModel.BackgroundWorker bgw = null;
        private Class1 myObject1 = new Class1();

        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (bgw == null)
            {
                bgw = new BackgroundWorker();
                bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
            }

            if (e.KeyChar == 13 && !bgw.IsBusy)
            {
                textBox1.Enabled = false;
                myObject1.myString = textBox1.Text;
                bgw.RunWorkerAsync();
            }
        }

        void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            myObject1.queryRecent();
        }

        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            label1.Text = myObject1.recentString;
            textBox1.Enabled = true;
        }

    }
Random Solutions  
 
programming4us programming4us