分类:
2008-10-13 16:09:16
using System; using System.Drawing; using System.Threading; using System.Windows.Forms; using Mircorsoft.VisualBasic; class App{ // Application entry point public static void Main(){ // Run a Window Forms message loop Application.Run(new SingleThreadedForm()); } } // A Form-derived typed class SingleThreadedForm: Form{ // Constructor method public SingleThreadedForm(){ // Create a text box text.Location = new Point(10, 10); text.Size = new Size(50, 20); Controls.Add(text); // Create a button button.Text = "Beep"; button.Size = new Size(50, 20); button.Location = new Point(80, 10); // Register Click event handler button.Click += new EventHandler(OnClick); Controls.Add(button); } // Method called by the button's Click event void OnClick(Object sender, EventArgs args){ // Get an int from a string Int32 count = 0; try { count = Int32.Parse(text.Text); } catch (FormatException) {} // Count to that member Count(count); } // Method beeps once per second void Count(Int32 seconds){ for(Int32 index = 0; index < seconds; index++){ Interaction.Beep(); Thread.Sleep(1000); } } // Some private fields by which to reference controls Button button = new Button(); TextBox text = new TextBox(); } using System; using System.Drawing; using System.Threading; using System.Windows.Forms; using Mircorsoft.VisualBasic; class App{ // Application entry point public static void Main(){ // Run a Window Forms message loop Application.Run(new FlawedMultiThreadedForm()); } } // A Form-derived typed class SingleThreadedForm: Form{ // Constructor method public SingleThreadedForm(){ // Create a text box text.Location = new Point(10, 10); text.Size = new Size(50, 20); Controls.Add(text); // Create a button button.Text = "Beep"; button.Size = new Size(50, 20); button.Location = new Point(80, 10); // Register Click event handler button.Click += new EventHandler(OnClick); Controls.Add(button); } // Method called by the button's Click event void OnClick(Object sender, EventArgs args){ // Get an int from a string Int32 count = 0; try { count = Int32.Parse(text.Text); } catch (FormatException) {} // Count to that member WaitCallback async = new WaitCallback(Count); ThreadPool.QueueUserWorkItem(async, count); } // Async Method beeps once per second void Count(Object param){ Int32 seconds = (Int32)param; for(Int32 index = 0; index < seconds; index++){ Interaction.Beep(); Thread.Sleep(1000); } } // Some private fields by which to reference controls Button button = new Button(); TextBox text = new TextBox(); }
using System; using System.Drawing; using System.Windows.Forms; using Microsoft.VisualBasic; class App{ // Application entry point public static void Main(){ // Run a Windows Forms message loop Application.Run(new CorrectMultiThreadedForm()); } } // A Form-dervied type class CorrectMultiThreadedForm: Form{ // Constructor method public CorrectMultiThreadedForm(){ // Create a textbox text.Location = new Point(10, 10); text.Size = new Size(50, 20); Controls.Add(text); // Create a button button.Text = "Beep"; button.Size = new Size(50, 20); button.Location = new Point(80, 10); // Regiester Click event handler button.Click += new EventHandler(OnClick); Controls.Add(button); // Cache a delegate for repeated reuse enableControls = new BooleanCallback(EnableControls); } // Method called by the button's Click event void OnClick(Object sender, EventArgs args){ // Get an int from a string Int32 count = 0; try{ count = Int32.Parse(text.Text); } catch(FormatException) {} // Count to that number EnableControls(false); WaitCallback async = new WaitCallback(Count); ThreadPool.QueueUserWorkItem(async, count); } // Async method beeps once per second void Count(Object param){ Int32 seconds = (Int32)param; for(Int32 index = 0; index < seconds; index++){ Interaction.Beep(); Thread.Sleep(1000); } Invoke(enableControls, new Object[]{true}); } void EnableControls(Boolean enable){ button.Enabled = enable; text.Enabled = enable; } // A delegate type and matching field delegate void BooleanCallback(Boolean enable); BooleanCallback enableControls; // Some private fields by which to reference controls Button button = new Button(); TextBox text = new TextBox(); }