加载表格然后在定义的时间段后显示一条消息

时间:2018-08-08 08:11:53

标签: c# async-await wait thread-sleep sharpdevelop

我正在尝试制作一个可以关闭的游戏启动器,限制为10分钟

问题陈述:我要加载表单,用户应该能够进行活动,然后应用程序应在10分钟后关闭

当我在主要方法中进行任何睡眠或等待时,表单本身未加载,我仅收到消息框,然后应用程序关闭。

  public MainForm()
    {

        InitializeComponent();

        MessageBox.Show("Welcome");

        Task.Run(async () =>
                 {
                    await Task.Delay(60000);
                 }).GetAwaiter().GetResult();

        MessageBox.Show("one minute left");

                    Task.Run(async () =>
                 {
                    await Task.Delay(60000);
                 }).GetAwaiter().GetResult();

                    MessageBox.Show("time over");
                    Application.Exit();

    }    

5 个答案:

答案 0 :(得分:1)

按如下方式使用System.Windows.Forms.Timer

Timer = new System.Windows.Forms.Timer();
Timer.Interval = TIME_IN_MILLISECONDS;
Timer.Tick += Timer_Tick;

private void Timer_Tick(object sender, EventArgs e)
{
   //desired behavior
}

系统计时器异步运行,在选定的间隔后它将触发您的方法

答案 1 :(得分:1)

使用Microsoft的Reactive Framework(NuGet“ System.Reactive.Windows.Forms”并添加float sizeInPixels = getResources().getDimension(R.dimen.my_value); ),然后您可以执行以下操作:

using System.Reactive.Linq;

答案 2 :(得分:0)

如果要以class App extends React.Component {}的方式启动任务,则无需调用fire and forget,因为这会阻塞线程。因此,基本上,您创建的所有这些任务都没有意义。您可以简单地使用GetAwaiter().GetResult();。使用Timer时,可以使用Windows FormsSystem.Windows.Forms.Timer。我将使用System.Threading.Timer来实现这种行为。

答案 3 :(得分:0)

为什么不只是从事件处理程序安排任务:

private async void OnApplicationStarted(object sender, RoutedEventArgs e)
{
    await Task.Delay((int)TimeSpan.FromMinutes(9).TotalMilliseconds);

    MessageBox.Show("1 minute left");

    await Task.Delay((int)TimeSpan.FromMinutes(1).TotalMilliseconds);

    MessageBox.Show("time over");
    Application.Exit();
}

答案 4 :(得分:0)

我将Form.Load事件与异步事件处理程序一起使用。这已经显示了您的表单,因此用户将能够看到该程序已启动。

async void OnFormLoading(object sender, ...)
{
    // Notify the operator that the form is loading:
    this.ShowSplashScreen();

    // start the delay, but do not wait yet
    Task delayTask = Task.Delay(TimeSpan.FromSeconds(5));

    // do other useful initializations, this might be shorter or longer than 5 seconds
    DoOtherInitializations();

    // now wait for the delay task to finish:
    await delayTask;

    this.HideSplashScreen();
}

或者更好的方法:显示一个启动表单,该表单显示您正在启动,并且只能取消:

在MainForm类中:

private async void OnFormLoading(object sender, ...)
{
     // show an intro screen while doing initializations:
     using (var startupForm = new FormStartup()
     {
          var dlgResult = startupForm.ShowDialog(this);

          if (dlgResult == DialogResult.Cancel)
          {
               // operator cancelled starting up:
               this.Close();
          }
          else
          {
               // continue starting up
          }
    }
}

     }
}

class FormStartup
{
    public void FormStartup() {...}

    private async void OnFormLoading(object sender, ...)
    {
        Task delayTask = Task.Delay(TimeSpan.FromSeconds(5));
        DoOtherInitializations();
        await delayTask;
    }
}
相关问题