Dwight Schrute
Dwight Schrute

Reputation: 369

Update of Windows Form - C#

Explanation:

Structure: Windows form - three components: Text box, Text box Response and button.

Problem: I am moving motor with C# windows form: I am starting the motor and reversing
the direction of movement of motor with single button click with 10 second delay in the middle. i.e. I start the motor, have 10 sec delay and then reverse the motor. I want to display "Start" at the beginning and "End" at the end of the 10 second delay. I have tried using thread but it does not work. But I am only able to see "Finish" not "Start" in text box. The code is below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace SampleThreadProgram
{
     public partial class Form1 : Form
     {
          static EventWaitHandle _waitHandle = new AutoResetEvent(false);
          delegate void SetTextCallback(string text);

     void SetText(string text)
     {
          if (textBox.InvokeRequired)
          {
             SetTextCallback d = new SetTextCallback(SetText);
             BeginInvoke(d, new object[] { text });
          }
          else
          {
             textBox.Text = text;
          }
     }

    void UpdateTextBox(string message)
    {
        SetText(message);
        _waitHandle.Set();
    }

    void Wait()
    {
        for (ulong i = 0; i < 10000; i++)
        {
            for (ulong j = 0; j < 100000; j++)
            {
            }
        }

    }
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {

        UpdateTextBox("Start");
        _waitHandle.WaitOne();
        Thread.Sleep(10000);
        UpdateTextBox("Finish");

    }

    }
}

Upvotes: 1

Views: 3561

Answers (2)

Servy
Servy

Reputation: 203811

  1. You should not use a big long for loop to make the computer wait for a while. Use Thread.Sleep at the very least.
  2. You should use a BackgroundWorker to do what you're trying to do. Set the start text in the button click event, then start the background worker. You can have the DoWork event do some work (in this case sleeping) and use the WorkerCompleted event to update the UI.

The nice thing about using the background worker is that you don't need to worry about updating code form non-UI threads. In the button click event you can update the text of the textbox directly, and the BackGroundWorker thread will already ensure that the Completed event runs in the UI thread, so even there you can directly access UI controls. The BGW is specifically designed to make this exact case easier.

Upvotes: 4

zimdanen
zimdanen

Reputation: 5626

There's not enough time for the UI to update. Add an Application.DoEvents(); after the SetText(message); in UpdateTextBox.

Upvotes: 0

Related Questions