问题
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