Chinaunix首页 | 论坛 | 博客
  • 博客访问: 818937
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

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(); }

--------------------next---------------------

阅读(306) | 评论(0) | 转发(0) |
0

上一篇:Figures

下一篇:Figure 2 动态生成XSLT

给主人留下些什么吧!~~