Search code examples
c#monodevelopgtk#

how to show text with time difference? (monodevelop c#)


I made a GTK# 2.0 Project from MonoDevelop, and modified MainWindow.cs like this;

using System;
using System.Threading;
using Gtk;

public partial class MainWindow : Gtk.Window
{
    public MainWindow() : base(Gtk.WindowType.Toplevel)
    {
        Build();
    }

    protected void OnDeleteEvent(object sender, DeleteEventArgs a)
    {
        Application.Quit();
        a.RetVal = true;
    }

    protected void OnButtonClicked(object sender, EventArgs e)
    {
        textview.Buffer.Text = "Hello, world!";
        Thread.Sleep(2500);
        textview.Buffer.Text += Environment.NewLine;
        textview.Buffer.Text += "Hello, world!";
    }
}

what I intended is: first "Hello, world!" is shown, and 2 seconds and half later, another "Hello, world!" is shown at next line.

but, what actually happend when I pressed the button: two "Hello, world" is shown simultaneously, after a term of 2 seconds and half.

then how can I show two lines with a time difference?


Solution

  • Use Task.Delay instead. You need to mark the method you call it from async.

    await Task.Delay(3000);