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

守給你的承諾、 提交于 2019-12-11 23:10:49

问题


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?


回答1:


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

await Task.Delay(3000);



回答2:


@Doruk

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 async void OnButtonClicked(object sender, EventArgs e)
    {
        textview.Buffer.Text = "Hello, world!";
        textview.Buffer.Text += Environment.NewLine;
        await Task.Delay(2500);
        textview.Buffer.Text += "Hello, world!";
    }
}


来源:https://stackoverflow.com/questions/49849841/how-to-show-text-with-time-difference-monodevelop-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!