Moving To .NET 2.0

by Eric Bergman-Terrell



Listing One



// Update the current time display.

private void UpdateTime()

{

  // Check to see if this method is called from thread that created this Form.

  Debug.Assert(!InvokeRequired);

  currentTimeLabel.Text = DateTime.Now.ToLongTimeString() + 

                    " (" + Thread.CurrentThread.Name + ")";

}

private delegate void UpdateTimeDelegate();

// Update the current time display, but ensure that the update is performed 

// by the same thread that created this Form.

private void UpdateTimeFromGUIThread()

{

  UpdateTimeDelegate updateTimeDelegate = 

      new UpdateTimeDelegate(UpdateTime);

  Invoke(updateTimeDelegate);

}

// Update the time when the user presses the Update from GUI Thread button.

private void updateFromGUIThreadButton_Click(object sender,System.EventArgs e)

{

  UpdateTime();

}

// Update the time from a worker thread from a worker thread. This is 

// against the Win32 threading rules!

private void updateFromWorkerThreadButton_Click(

                      object sender, System.EventArgs e)

{

  Thread thread = new Thread(new ThreadStart(UpdateTime));

  thread.Name = "Worker Thread 1";

  thread.Start();

}

// Update the time from a worker thread, but use Invoke to ensure that the 

// update is done by the thread that created the Form.

private void updateFromWorkerThreadInvokeButton_Click(

                    object sender, System.EventArgs e)

{

  Thread thread = 

     new Thread(new ThreadStart(UpdateTimeFromGUIThread));

  thread.Name = "Worker Thread 2";

  thread.Start();

}





Listing Two



internal static class NullableTypeUtils

{

  // Return true if the object is a nullable value type.

  static public bool IsNullableType(object theObject)

  {

    // Nullable value types implement the INullableValue interface.

    INullableValue iNullableValue = theObject as INullableValue;

    return iNullableValue != null;

  }

  // Return true if the object is either an object with a null value or a 

  // nullable reference type with a null value.

  static public bool IsNull(object theObject)

  {

    bool result = false;

    if (theObject == null)

    {

      result = true;

    }

    else

    {

      INullableValue iNullableValue = theObject as INullableValue;

      if (iNullableValue != null && !iNullableValue.HasValue)

      {

        result = true;

      }

    }

    return result;

  }

  // Return the nullable value type's value or "null".

  static public string ToString(object theObject)

  {

    Debug.Assert(IsNullableType(theObject));

    string result = "";

    // Nullable value types implement the INullableValue interface.

    INullableValue iNullableValue = theObject as INullableValue;

    if (iNullableValue != null)

    {

      if (iNullableValue.HasValue)

      {

        result = iNullableValue.ToString();

      }

      else

      {

        result = "null";

      }

    }

    return result;

  }

}





Listing Three



 ...

partial class Form1 : Form

{

  public Form1()

  {

    InitializeComponent();

  }

  private void button1_Click(object sender, EventArgs e)

  {

    MessageBox.Show("Hello World");

  }

}





Listing Four



 ...

partial class Form1

{

  ...

  private void InitializeComponent()

  {

    System.Windows.Forms.Label label1;

    this.button1 = new System.Windows.Forms.Button();

    label1 = new System.Windows.Forms.Label();

    this.SuspendLayout();

    // 

    // label1

    // 

    label1.AutoSize = true;

    label1.Location = new System.Drawing.Point(31, 34);

    label1.Name = "label1";

    ...

    // 

    // button1

    // 

    this.button1.Location = new System.Drawing.Point(112, 146);

    this.button1.Name = "button1";

    ...

    this.button1.Click += new System.EventHandler(this.button1_Click);

    ...

  }

  ...

  private System.Windows.Forms.Button button1;

}











3



