在非UI线程Silverlight 5浏览器应用程序中创建UserControl

Geo*_*rge 11 c# silverlight multithreading silverlight-5.0

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

有一节课

public class ActivityControl:UserControl {

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

我需要创建此类的多个实例,并在运行时调用方法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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法创建多个UI线程以避免无效的跨线程异常?

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

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

Nko*_*osi 3

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

我已经能够在 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);    
}
Run Code Online (Sandbox Code Playgroud)

这是实现。

/// <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;
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以沿着构造函数 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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您应该能够在不阻塞主线程和冻结 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();
            });    
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果对调度程序进行多次调用导致任何性能问题,您可以将整个流程移至一个 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();
            }    
        });
    }
}
Run Code Online (Sandbox Code Playgroud)