Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

newbie on multiple threading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Danny Ni - 11 Mar 2008 08:44 GMT
Hi,

I have a WinForm application, when the Form load, I would like to create 2
worker threads that will load data from different SQL tables and populate 2
ListBoxes on the form.

Here is what I wantto do: change the cursor to hourglass when the first
worker thread starts and return the cursor to normal when both worker
threads end. But I don't know how to do it.

TIA
Marc Gravell - 11 Mar 2008 09:01 GMT
Following shows an example that starts 2 workers, sets the cursor when
both have exited, and demonstrates jumping back to the UI thread to
perform the actual UI update (you can't touch the UI from the worker
thread):

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
static class Program
{
   static void Main()
   {
       Application.Run(new MyForm());
   }
}
class MyForm : Form
{
   BackgroundWorker worker1, worker2;
   public MyForm() { InitializeComponent(); }
   private int busyCount = 0;
   private void BeginBusy()
   {
       busyCount++;
       if (busyCount > 0) this.Cursor = Cursors.WaitCursor;
   }
   private void EndBusy()
   {
       busyCount--;
       if (busyCount <= 0) this.Cursor = Cursors.Default;
   }
   private void InitializeComponent()
   {
       worker1 = new BackgroundWorker();
       worker1.DoWork += new DoWorkEventHandler(worker1_DoWork);
       worker2 = new BackgroundWorker();
       worker2.DoWork += new DoWorkEventHandler(worker2_DoWork);
   }

   void worker2_DoWork(object sender, DoWorkEventArgs e)
   {
       Invoke((MethodInvoker)BeginBusy);
       Thread.Sleep(5000);
       Invoke((MethodInvoker)delegate
       {
           // update UI
           Text += "; worker 2 complete";
           EndBusy();
       });
   }

   void worker1_DoWork(object sender, DoWorkEventArgs e)
   {
       Invoke((MethodInvoker)BeginBusy);
       Thread.Sleep(9000);
       Invoke((MethodInvoker) delegate {
           // update UI
           Text += "; worker 1 complete";
           EndBusy();
       });
   }
   protected override void OnLoad(EventArgs e)
   {
       base.OnLoad(e);
       worker1.RunWorkerAsync();
       worker2.RunWorkerAsync();
   }
}
Marc Gravell - 11 Mar 2008 09:04 GMT
Actually my last post was unnecessarily complex - you don't really
need BackgroundWorker in this case...

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;
static class Program
{
   static void Main()
   {
       Application.Run(new MyForm());
   }
}
class MyForm : Form
{
   private int busyCount = 0;
   private void BeginBusy()
   {
       busyCount++;
       if (busyCount > 0) this.Cursor = Cursors.WaitCursor;
   }
   private void EndBusy()
   {
       busyCount--;
       if (busyCount <= 0) this.Cursor = Cursors.Default;
   }

   void worker2_DoWork(object state)
   {
       Invoke((MethodInvoker)BeginBusy);
       Thread.Sleep(5000);
       Invoke((MethodInvoker)delegate
       {
           // update UI
           Text += "; worker 2 complete";
           EndBusy();
       });
   }

   void worker1_DoWork(object state)
   {
       Invoke((MethodInvoker)BeginBusy);
       Thread.Sleep(9000);
       Invoke((MethodInvoker) delegate {
           // update UI
           Text += "; worker 1 complete";
           EndBusy();
       });
   }
   protected override void OnLoad(EventArgs e)
   {
       base.OnLoad(e);
       ThreadPool.QueueUserWorkItem(worker1_DoWork);
       ThreadPool.QueueUserWorkItem(worker2_DoWork);
   }
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.