O'Reilly Forums: Need Help With Multithreading In Gui - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need Help With Multithreading In Gui

#1 User is offline   cembo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 08-February 12

Posted 08 February 2012 - 09:37 PM

Hi guys. I need help with understanding multithreading in GUI. It is not that easy as in console app :) Let say i have very basic Form1 with two textboxes and three buttons (textBox1, textBox2, button1, button2 and button3). There are also two functions (countDown1 and countDown2) which are for updating particular textboxes ... after press button1 (or on formload) function countDown1() will strart updating textBox1 in Main thread and also function countDown2() updating textBox2 in extra thread. Buttons 2 and 3 are for cancelling updating in particular textboxes (button2 - textBox1, button3 - textBox2). Thank you guys, it is great help for me. Please insert your code into this template, it will be very helpful for me to understand principle. Try to keep code as simple as is possible. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
namespace Threading_Example_1
{
    public partial class Form1 : Form
    {
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
             
        }
 
 
        private void countDown1()
        {
            for (int i = 20000; i < 30000; i++)
            {
                textBox1.Text = i.ToString();
                textBox1.Refresh();
            }
        }
 
        private void countDown2()
        {
            for (int i = 0; i < 10000; i++)
            {
                textBox2.Text = i.ToString();
                textBox2.Refresh();
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
 
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
 
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
             
        }
 
 
    }
}

0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 34
  • Joined: 18-May 11

Posted 09 February 2012 - 06:02 PM

You will need to drag 2 backgroundworker controls from your toolbox onto your form. Then make sure to tie the eventhandlers created in the following code to the proper events through their properties.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Threading_Example_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker2.WorkerReportsProgress = true;
            backgroundWorker2.WorkerSupportsCancellation = true;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 20000; i < 30000; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                    worker.ReportProgress(1, i.ToString());
                }
            }
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 0; i < 10000; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                    worker.ReportProgress(2, i.ToString());
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
                backgroundWorker1.RunWorkerAsync();

            if (backgroundWorker2.IsBusy != true)
                backgroundWorker2.RunWorkerAsync();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
                backgroundWorker1.CancelAsync();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (backgroundWorker2.WorkerSupportsCancellation == true)
                backgroundWorker2.CancelAsync();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            textBox1.Text = (e.UserState.ToString());
        }

        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            textBox2.Text = (e.UserState.ToString());
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
                textBox1.Text = "Canceled!";
            else if (e.Error != null)
                textBox1.Text = "Error: " + e.Error.Message;
            else
                textBox1.Text = "Done!";
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
                textBox2.Text = "Canceled!";
            else if (e.Error != null)
                textBox2.Text = "Error: " + e.Error.Message;
            else
                textBox2.Text = "Done!";
        }
    }
}

This post has been edited by Sikta_26785: 09 February 2012 - 06:03 PM

0

#3 User is offline   cembo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 08-February 12

Posted 09 February 2012 - 10:46 PM

View PostSikta_26785, on 09 February 2012 - 06:02 PM, said:

You will need to drag 2 backgroundworker controls from your toolbox onto your form. Then make sure to tie the eventhandlers created in the following code to the proper events through their properties.

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Threading_Example_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker2.WorkerReportsProgress = true;
            backgroundWorker2.WorkerSupportsCancellation = true;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 20000; i < 30000; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                    worker.ReportProgress(1, i.ToString());
                }
            }
        }

        private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 0; i < 10000; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                    worker.ReportProgress(2, i.ToString());
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy != true)
                backgroundWorker1.RunWorkerAsync();

            if (backgroundWorker2.IsBusy != true)
                backgroundWorker2.RunWorkerAsync();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (backgroundWorker1.WorkerSupportsCancellation == true)
                backgroundWorker1.CancelAsync();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (backgroundWorker2.WorkerSupportsCancellation == true)
                backgroundWorker2.CancelAsync();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            textBox1.Text = (e.UserState.ToString());
        }

        private void backgroundWorker2_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            textBox2.Text = (e.UserState.ToString());
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
                textBox1.Text = "Canceled!";
            else if (e.Error != null)
                textBox1.Text = "Error: " + e.Error.Message;
            else
                textBox1.Text = "Done!";
        }

        private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
                textBox2.Text = "Canceled!";
            else if (e.Error != null)
                textBox2.Text = "Error: " + e.Error.Message;
            else
                textBox2.Text = "Done!";
        }
    }
}




... thanks man :) Just one question. Why did you chose backgroundworker instead of delegates and invocation ...
0

#4 User is offline   Sikta_26785 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 34
  • Joined: 18-May 11

Posted 10 February 2012 - 04:14 PM

Because you asked for as simple as possible.
0

#5 User is offline   cembo 

  • New Member
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 08-February 12

Posted 11 February 2012 - 06:40 PM

View PostSikta_26785, on 10 February 2012 - 04:14 PM, said:

Because you asked for as simple as possible.



... that's true. Very nice example, now i can see how it works ... Would you be so kind and make Thread class solution for this scenario. What is the main difference between BW and Thread class. How can i decide which one to use ... Thanks ...

This post has been edited by cembo: 11 February 2012 - 06:56 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users