简体   繁体   中英

changing text of button with timeout in c#

How can I change the text of button with timeout? I tried out with the following code but it is not working.

private void button1_Click(object sender, EventArgs e)
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    if (button1.Text == "Start")
    {
        //do something
        button1.Text = "stop"
        if (sw.ElapsedMilliseconds > 5000)
        {
            button1.Text = "Start";

        }
    }

How can I correct my code?

You need to use Timer instead:

Timer t = new Timer(5000); // Set up the timer to trigger on 5 seconds
t.SynchronizingObject = this; // Set the timer event to run on the same thread as the current class, i.e. the UI
t.AutoReset = false; // Only execute the event once
t.Elapsed += new ElapsedEventHandler(t_Elapsed); // Add an event handler to the timer
t.Enabled = true; // Starts the timer

// Once 5 seconds has elapsed, your method will be called
void t_Elapsed(object sender, ElapsedEventArgs e)
{
    // The Timer class automatically runs this on the UI thread
    button1.Text = "Start";
}

Stopwatch is only for measuring how much time has passed since you called Start().

If you're using C# 5

private async void button1_Click(object sender, EventArgs e)
{
    button1.Text = "Stop";

    await Task.Delay(5000);

    button1.Text = "Start";
}

I think you can reach your goal by using Timer

Example of using Timer

public partial class FormWithTimer : Form
{
    Timer timer = new Timer();

    public FormWithTimer()
    {
        InitializeComponent();

        // Everytime timer ticks, timer_Tick will be called
        timer.Tick += new EventHandler(timer_Tick);
        timer.Interval = (1000) * (1); // Timer will tick every second
        timer.Enabled = true; // Enable the timer
    }

    //    .......    

    showForm() // declaration
    {
        timer.start();
        //    .......
        timer.stop();
    }            

    void timer_Tick(object sender, EventArgs e)
    {
        //hide form...through visibility
    }
}

Use this instead of Stopwatch:

private void button1_Click(object sender, EventArgs e)
{
    button1.Text = "stop"
    aTimer = new System.Timers.Timer(5000);

    aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    aTimer.Enabled = true;
}

// Specify what you want to happen when the Elapsed event is  raised. 
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
    button1.Text = "Start";
    var atim = source as Timer;
    if (atim != null)
       atim.Elapsed  -= OnTimedEvent;
}

You could use a timer. In this example the text of the button changes to "Stop" after 5 seconds.

private Timer timer = new Timer();

private void button1_Click(object sender, EventArgs e)
{
    timer.Interval = 5000; // interval length
    timer.Tick += TimerOnTick; 
    timer.Enabled = true; // activate timer
    button1.Text = "Start";
}

private void TimerOnTick(object sender, EventArgs eventArgs)
{
    timer.Enabled = false; // deactivate timer
    button1.Text = "Stop";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM