在Xamarin.mac的主线程中发生了奇怪的事情

时间:2014-04-07 15:09:29

标签: macos ui-thread xamarin.mac

我有一种方法,并且发生了一些奇怪的事情

  public void UpdateProgressBar(double newValue)
  {                       
        // Make sure we don't go over the maximum value for the progress bar
        if (newValue > _maxValueProgressBar)
            newValue = _maxValueProgressBar;

        // Make sure we don't go under the minimum value for the progress bar
        if (newValue < _minValueProgressBar)
            newValue = _minValueProgressBar;

         if (!NSThread.Current.IsMainThread)
             {
             using (var pool = new NSAutoreleasePool())
             pool.BeginInvokeOnMainThread(delegate
                {
                    JobProgressBar.DoubleValue = newValue;
                });
             }
           else               
               // jobProgressBar.DoubleValue = newValue;
        // InvokeOnMainThread (() => JobProgressBar.DoubleValue=newValue);
  }

正如你所看到的那样,必须从Mainthread调用此方法,但是当我对其他两个方法中的任何一个进行评论时,我的应用程序崩溃了。我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

我通常只使用InvokeOnMainThread,没有理由使用NSAutoReleasePool。你试过下面的代码吗?

public void UpdateProgressBar(double newValue)
{                       
    // Make sure we don't go over the maximum value for the progress bar
    if (newValue > _maxValueProgressBar)
        newValue = _maxValueProgressBar;

    // Make sure we don't go under the minimum value for the progress bar
    if (newValue < _minValueProgressBar)
        newValue = _minValueProgressBar;

    InvokeOnMainThread (() => JobProgressBar.DoubleValue=newValue);
}