【问题标题】:Create UserControl in non-UI thread Silverlight 5 browser application在非 UI 线程 Silverlight 5 浏览器应用程序中创建 UserControl
【发布时间】:2016-09-22 14:19:12
【问题描述】:

我有一个 Silverlight 5 浏览器应用程序。

有一门课

public class ActivityControl:UserControl {

    public void LoadSubControls() {
        //Creates Other UserControls, does calculations and is very slow..No refactoring..
    }
}

我需要创建这个类的多个实例并在运行时调用方法 LoadSubControls。

public class BasicContainer:UserControl  {

    public void CreateMultipleActivityControls() {

        for (int i = 0; i < 1000; i++) {

            ActivityControl c = new ActivityControl();  ====> I need to call this in a different thread but causes Invalid Cross Thread Exception

            c.LoadSubControls();
        }
    }
}

有什么方法可以创建多个 UI 线程来避免无效的跨线程异常?

出于性能原因,我需要多线程,因为方法调用非常慢并且 UI 冻结。

有没有办法在 Silverlight 中调用方法 SetSyncronizationContext(即 [SecurityCritical])?

【问题讨论】:

  • 所有控件都直接在gui中看到了吗?你可以让它们变得懒惰吗?
  • 在“LoadSubControls”方法中,我会测量安排子控件的 UpdateLayout 以生成图像。你的意思是我不应该运行 InitializeComponent()?
  • 我还会将您的方法拆分成更小的块,并且每次您确实需要在 ui 中放置控件时才回调 ui 线程。

标签: c# multithreading silverlight silverlight-5.0


【解决方案1】:

无法避免在 UI 线程上创建这些控件,但您可以利用任务并行库 (TPL) 中的 System.Threading.Tasks.Task 来允许异步操作。

我已经能够在 Silverlight 5 中用这样的结构做这样的事情。最初的想法是查看 Caliburn.Micro 的源代码。

以下是适用于您想要的子集。

public interface IPlatformProvider {
    /// <summary>
    ///  Executes the action on the UI thread asynchronously.
    /// </summary>
    /// <param name = "action">The action to execute.</param>
    System.Threading.Tasks.Task OnUIThreadAsync(Action action);    
}

这是实现。

/// <summary>
/// A <see cref="IPlatformProvider"/> implementation for the XAML platfrom (Silverlight).
/// </summary>
public class XamlPlatformProvider : IPlatformProvider {
    private Dispatcher dispatcher;

    public XamlPlatformProvider() {
       dispatcher = System.Windows.Deployment.Current.Dispatcher;
    }

    private void validateDispatcher() {
        if (dispatcher == null)
            throw new InvalidOperationException("Not initialized with dispatcher.");
    }

    /// <summary>
    ///  Executes the action on the UI thread asynchronously.
    /// </summary>
    /// <param name = "action">The action to execute.</param>
    public Task OnUIThreadAsync(System.Action action) {
        validateDispatcher();
        var taskSource = new TaskCompletionSource<object>();
        System.Action method = () => {
            try {
                action();
                taskSource.SetResult(null);
            } catch (Exception ex) {
                taskSource.SetException(ex);
            }
        };
        dispatcher.BeginInvoke(method);
        return taskSource.Task;
    }
}

您可以沿着构造函数 DI 路由传入提供程序,也可以使用这样的静态定位器模式。

/// <summary>
/// Access the current <see cref="IPlatformProvider"/>.
/// </summary>
public static class PlatformProvider {
    private static IPlatformProvider current = new XamlPlatformProvider();

    /// <summary>
    /// Gets or sets the current <see cref="IPlatformProvider"/>.
    /// </summary>
    public static IPlatformProvider Current {
        get { return current; }
        set { current = value; }
    }
}

现在您应该能够在不阻塞主线程和冻结 UI 的情况下进行调用

public class BasicContainer : UserControl {
    public async Task CreateMultipleActivityControls() {
        var platform = PlatformProvider.Current;
        for (var i = 0; i < 1000; i++) {
            await platform.OnUIThreadAsync(() => {    
                var c = new ActivityControl();     
                c.LoadSubControls();
            });    
        }
    }
}

如果对调度程序进行多次调用导致任何性能问题,您可以将整个过程移至一个 acync 调用。

public class BasicContainer : UserControl {
    public async Task CreateMultipleActivityControls() {
        var platform = PlatformProvider.Current;
        await platform.OnUIThreadAsync(() => {
            for (var i = 0; i < 1000; i++) {                    
                var c = new ActivityControl();     
                c.LoadSubControls();
            }    
        });
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多