Form1.cs in Async Delegate Example 0 using System; 1 using System.Drawing; 2 using System.Collections; 3 using System.ComponentModel; 4 using System.Windows.Forms; 5 using System.Data; 6 using System.Threading; // for Thread.Sleep 7 8 namespace HarborMist 9 { 10 11 public class Form1 : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.Button button1; 14 private System.Windows.Forms.ProgressBar progressBar1; 15 private System.Windows.Forms.Timer timer1; 16 private System.Windows.Forms.TextBox textBox1; 17 private System.Windows.Forms.ProgressBar progressBar2; 18 private System.Windows.Forms.Button buttonExit; 19 private System.Windows.Forms.Label label1; 20 private System.ComponentModel.IContainer components; 21 22 23 public Form1() 24 { 25 // required for Windows Form Designer support 26 InitializeComponent(); 27 28 } 29 30 31 32 private string threadIdentity() 33 { 34 string myString = ""; 35 if (Thread.CurrentThread.IsThreadPoolThread) 36 myString = "Thread pool thread"; 37 else 38 myString = "Form thread"; 39 myString = myString + ", priority="; 40 myString = myString + Thread.CurrentThread.Priority.ToString(); 41 if (Thread.CurrentThread.IsBackground) 42 myString = myString + " (background)"; 43 else 44 myString = myString + " (foreground)"; 45 return myString; 46 } 47 48 49 50 [STAThread] 51 static void Main() 52 { 53 Application.Run(new Form1()); 54 } 55 56 57 58 private void button1_Click(object sender, System.EventArgs e) 59 { 60 try 61 { 62 myDelegateForWorkerThread = new delegateForWorkerThread(workerCode); 63 myDelegateForWorkerThread.BeginInvoke(10, new AsyncCallback(codeForAfterWorkerCode), null); 64 } 65 catch 66 { 67 MessageBox.Show("Failed to create worker thread."); 68 } 69 } 70 71 72 private delegate string delegateForWorkerThread(int param); 73 74 private delegate void delegateForFormUpdate(string myMessage, int myCount); 75 76 private delegate void delegateForTextBox1Update(string theAddendum); 77 78 private delegateForWorkerThread myDelegateForWorkerThread; 79 80 81 private string workerCode(int howManyUpdates) 82 { 83 int count; 84 for (count=1;count<=howManyUpdates;count++) 85 { 86 if (progressBar2.InvokeRequired) 87 { 88 progressBar2.Invoke(new delegateForFormUpdate(workerGUIupdate), 89 new object[]{"loop count = ",count}); 90 } 91 else 92 { 93 workerGUIupdate("I am not a worker thread.", count); 94 } 95 Thread.Sleep(600); 96 } 97 return threadIdentity() + "\r\nworker loop ran " + (count-1).ToString() + " times"; 98 } 99 100 101 private void workerUpdateTextBox1(string myAddition) 102 { 103 textBox1.Text = textBox1.Text + myAddition; 104 } 105 106 107 // called after the asynchronous work is done 108 private void codeForAfterWorkerCode(IAsyncResult results) 109 { 110 string myMessage = "\r\n" + 111 myDelegateForWorkerThread.EndInvoke(results) + "\r\n" + 112 "progress bar value = " + progressBar2.Value.ToString(); 113 114 if (textBox1.InvokeRequired) 115 { 116 progressBar2.Invoke(new delegateForTextBox1Update(workerUpdateTextBox1), 117 new object[] { myMessage }); 118 } 119 else 120 { 121 workerUpdateTextBox1(myMessage); 122 } 123 } 124 125 126 private void workerGUIupdate(string theMessage, int theCount) 127 { 128 int x = this.progressBar2.Value + 1; 129 if (x > this.progressBar2.Maximum) x = 1; 130 this.progressBar2.Value = x; 131 this.textBox1.Text = theMessage + theCount.ToString(); 132 return; 133 } 134 135 136 private void timer1_Tick(object sender, System.EventArgs e) 137 { 138 timer1.Enabled = false; 139 int x = progressBar1.Value + 10; 140 if (x > progressBar1.Maximum) x = 0; 141 progressBar1.Value = x; 142 timer1.Enabled = true; 143 } 144 145 146 private void Form1_Load(object sender, System.EventArgs e) 147 { 148 timer1.Interval = 100; 149 timer1.Enabled = true; 150 label1.Text = threadIdentity() + " is updating this progress bar."; 151 } 152 153 154 private void buttonExit_Click(object sender, System.EventArgs e) 155 { 156 this.Close(); 157 } 158 159 160 private void Form1_Resize(object sender, System.EventArgs e) 161 { 162 this.label1.Top = this.progressBar1.Top + this.progressBar1.Height; 163 } 164 165 166 } // end class