2016-07-24 63 views
0

我试图在Ubuntu 14.04和Raspian (Raspberry Pi 2B)中使用系统调用“sleep”来延迟代码执行,例如, 5秒然而,令人惊讶的是,系统前面的所有代码在Delay内调用“sleep”在运行时都没有执行。这是造成这个问题的简单代码:系统调用睡眠不能正常工作

using System; 
using Gtk; 
using Mono.Unix.Native;    

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

     entry1.Alignment = 0.5f; 

     // This code is not executed: 
     double result = Math.Pow (2.0, 2.0); 
     entry1.Text = result.ToString(); 
     // End of code not executed 

     // Code executed: 
     Delay (5); 
     entry1.Text = "Button-A"; 
    } 

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

    private Int16 Delay (UInt32 value) 
    { 
     Mono.Unix.Native.Syscall.sleep (value);  
     return 0; 
    } 
} 

难道我误解的Linux基本的东西还是我面临的一个编译器错误?感谢您对该主题的任何提示!

回答

0

您在主UI线程上阻塞。

改为使用Task.Delay代替使用系统exec来阻止/延迟执行。

await Task.Delay(5000);