c# - 如何在 SilverLight 中的某个时间(500 毫秒)调用某个方法

标签 c# silverlight silverlight-4.0 silverlight-3.0

我有两种方法,例如 Method1 和 Method2。如何在 Method1 完成后 500ms 调用 Method2?

 public void Method1()
 {

 }

 public void Method2()
 {

 }

最佳答案

使用 TimerBackgroundWorker . Timer 可能最适合您的简短描述,除非您想在 UI 线程上执行某些操作,在这种情况下 DispatchTimer更适合您,因为它会在 UI 线程上回调。

示例:

  public void Run_Method1_Then_Method2_500_Milliseconds_Later()
  {
      DispatcherTimer timer = new DispatcherTimer();
      timer.Interval = TimeSpan.FromMilliseconds(500);
      timer.Tick += (s, e) =>
      {
          // do some quick work here in Method2
          Method2(timer);
      };

      Method1();      // Call Method1 and wait for completion
      timer.Start();  // Start Method2 500 milliseconds later
  }

  public void Method1()
  {
      // Do some work here
  }

  public void Method2(DispatcherTimer timer)
  {
      // Stop additional timer events
      timer.Stop();
      // Now do some work here
  }

关于c# - 如何在 SilverLight 中的某个时间(500 毫秒)调用某个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9618342/

相关文章:

c# - 是否可以在 Windows Phone 7 中读取 .rar 或 .zip 文件?如何?

delphi - 将应用程序从 Delphi 迁移到 Silverlight C#

silverlight-4.0 - 创建登录屏幕

c# - 像 Dropbox 这样的同步服务,文件索引背后的理论?

c# - 为什么 DataType.Password 只在服务器端验证?

任何地方的 C# Winforms ListView DoubleClick 事件

Silverlight SelectedIndex 未正确数据绑定(bind)

c# - 是否有属性/方法可以确定 TcpListener 当前是否正在监听?

c# - Silverlight 应用程序的登录功能

c# - windows phone 7开发的MVVM框架推荐