2011-10-09 28 views
0

我想翻译下面的C#代码段VB:C#到VB.NET转换错误,建议要求

public bool ShowHandlerDialog(string message) 
     { 
      Message = message; 
      Visibility = Visibility.Visible; 

      _parent.IsEnabled = false; 

      _hideRequest = false; 
      while (!_hideRequest) 
      { 
       // HACK: Stop the thread if the application is about to close 
       if (this.Dispatcher.HasShutdownStarted || 
        this.Dispatcher.HasShutdownFinished) 
       { 
        break; 
       } 

       // HACK: Simulate "DoEvents" 
       this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); 
       Thread.Sleep(20); 
      } 

      return _result; 
     } 

但翻译是在这一行给了一个错误:

this.Dispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { })); 

翻译是:

Me.Dispatcher.Invoke(DispatcherPriority.Background, New ThreadStart(Function() Do End Function)) 

这似乎并没有正确地转换新的ThreadStart后的位。可有人请解释什么是“delegate {}”确实在

new ThreadStart(delegate {})); 

,我怎么可能纠正翻译错误?感谢您的任何建议!

回答

1

该行只是启动一个新线程并等待它完成。 “委托{}”代码只是一种匿名/内联方法(我认为VB.NET不支持)。就好像你会基本上指向一个空方法一样。例如,在c#事件处理程序可以被绑定到匿名(内联)委托方法为这样:

this.OnClick += (EventHandler)delegate(object sender, EventArgs ea) { 
    MessageBox.Show("Click!"); 
}; 

注释以上说[// HACK:模拟“的DoEvents”]。只需用DoEvents for VB.NET替换这两行,即可设置。这样可以让其他线程在继续之前完成工作,从而提高响应速度。

希望这会有所帮助!

+0

很好的答案,谢谢! – TripleAntigen