与Application.DoEvents()相当的iPhone;

与Application.DoEvents()相当的iPhone;,iphone,multithreading,xamarin.ios,Iphone,Multithreading,Xamarin.ios,iPHone:我们使用MonoTouch,但Obj-C的答案是可以的 我的单例域对象需要一段时间来获取所有数据,因此它在线程中内部运行部分获取 我需要通知用户界面域已经完成。目前我是这样做的。有更好的办法吗?在WinForms中,我将调用Application.DoEvents()而不是线程睡眠 PlanDomain domain = PlanDomain.Instance (); while (domain.IsLoadingData) { Thread.Sleep (100); /

iPHone:我们使用MonoTouch,但Obj-C的答案是可以的

我的单例域对象需要一段时间来获取所有数据,因此它在线程中内部运行部分获取

我需要通知用户界面域已经完成。目前我是这样做的。有更好的办法吗?在WinForms中,我将调用Application.DoEvents()而不是线程睡眠

PlanDomain domain = PlanDomain.Instance ();
while (domain.IsLoadingData)
{
    Thread.Sleep (100);  //this is the main UI thread
}

TableView.Hidden = false;
TableView.Source = new TableSource (this);
TableView.ReloadData ();

我想看看是否可以使用performSelectorOnMainThread:将Tableview代码移到它自己的方法中,并在加载完数据后调用它

委托在这种情况下也很有用——让您的PlanDomain在完成委托函数时回调委托函数

同样,如果您想要更独立的东西,可以考虑使用NSS通知。当您编写的代码不需要知道或关心它完成后将发生什么时,这些都是最有用的——它只需要向世界宣布它完成了它的工作。(当一件事完成后,可能有不止一件事对行动感兴趣时,最有用


在其中任何一种情况下,你都不想睡觉——只要让runloop来处理就行了。

一般来说,你希望避免DoEvents。objective-c中的iPhone类似物如下所示:

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate date]];

最好是从AlertView(一个加载对话框)派生一些东西,除非您想执行后台加载

然后,这将在一个单独的
线程中运行任务,您将加入该线程,然后关闭对话框。这里有一个示例,但没有任何线程功能,但添加起来很简单:

public class LoadingView : UIAlertView
{
    private UIActivityIndicatorView _activityView;

    public void Show(string title)
    {
        Title = title;
        Show();

        // Spinner - add after Show() or we have no Bounds.
        _activityView = new UIActivityIndicatorView(UIActivityIndicatorViewStyle.WhiteLarge);
        _activityView.Frame = new RectangleF((Bounds.Width / 2) - 15, Bounds.Height - 50, 30, 30);
        _activityView.StartAnimating();
        AddSubview(_activityView);

        // I haven't tested this, it should display the dialog still.
        Thread thread = new Thread(DoWork);
        thread.Start();
        thread.Join();

        Hide();
    }

    private void DoWork()
    {
        PlanDomain domain = PlanDomain.Instance ();
        while (domain.IsLoadingData)
        {
            // Maybe replace domain.IsLoadingData with an Event for when it finishes.
            // And then use a Mutex (WaitHandle) to communicate back
        }
    }

    public void Hide()
    {
        DismissWithClickedButtonIndex(0, true);
    }
}

为了进一步了解休息室的答案,这里有一个C代码示例:

NSRunLoop.Current.RunUntil(DateTime.Now.AddMilliseconds(2000));
这将暂停2000毫秒,但确保UI更新。我只是在一些测试代码中使用了这一点