繁体   English   中英

如何在 STA 线程中运行某些东西?

[英]How to run something in the STA thread?

在我的 WPF 应用程序中,我进行了一些异步通信(与服务器)。 在回调函数中,我最终从服务器的结果中创建了 InkPresenter 对象。 这要求正在运行的线程是 STA,而目前显然不是。 因此我得到以下异常:

无法创建程序集 [..] 中定义的“InkPresenter”的实例调用线程必须是 STA,因为许多 UI 组件需要这样做。

目前我的异步函数调用是这样的:

public void SearchForFooAsync(string searchString)
{
    var caller = new Func<string, Foo>(_patientProxy.SearchForFoo);
    caller.BeginInvoke(searchString, new AsyncCallback(SearchForFooCallbackMethod), null);
}

我怎样才能使回调 - 这将创建 InkPresenter - 成为 STA? 或者在新的 STA 线程中调用 XamlReader 解析。

public void SearchForFooCallbackMethod(IAsyncResult ar)
{
    var foo = GetFooFromAsyncResult(ar); 
    var inkPresenter = XamlReader.Parse(foo.Xaml) as InkPresenter; // <!-- Requires STA
    [..]
}

您可以像这样启动 STA 线程:

    Thread thread = new Thread(MethodWhichRequiresSTA);
    thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
    thread.Start(); 
    thread.Join(); //Wait for the thread to end

唯一的问题是您的结果对象必须以某种方式传递。您可以为此使用私有字段,或者深入研究将参数传递给线程。 在这里,我将 foo 数据设置在一个私有字段中并启动 STA 线程来改变inkpresenter!

private var foo;
public void SearchForFooCallbackMethod(IAsyncResult ar)
{
    foo = GetFooFromAsyncResult(ar); 
    Thread thread = new Thread(ProcessInkPresenter);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join(); 
}

private void ProcessInkPresenter()
{
    var inkPresenter = XamlReader.Parse(foo.Xaml) as InkPresenter;
}

希望这可以帮助!

您可以使用Dispatcher类在 UI-Thread 上执行方法调用。 Dispatcher 提供静态属性 CurrentDispatcher 来获取线程的调度程序。

如果创建 InkPresenter 的类对象是在 UI-Thread 上创建的,则 CurrentDispatcher 方法返回 UI-Thread 的 Dispatcher。

在 Dispatcher 上,您可以调用 BeginInvoke 方法在线程上异步调用指定的委托。

在 UI 线程上调用它应该足够了。 因此,使用BackgroundWorker并在RunWorkerAsyncCompleted ,然后您可以创建inkPresenter。

这有点黑客,但我会使用XTATestRunner所以你的代码看起来像:

    public void SearchForFooAsync(string searchString)
    {
        var caller = new Func<string, Foo>(_patientProxy.SearchForFoo);
        caller.BeginInvoke(searchString, new AsyncCallback(SearchForFooCallbackMethod), null);
    }

    public void SearchForFooCallbackMethod(IAsyncResult ar)
    {
        var foo = GetFooFromAsyncResult(ar); 
        InkPresenter inkPresenter;
        new XTATestRunner().RunSTA(() => {
            inkPresenter = XamlReader.Parse(foo.Xaml) as InkPresenter;
        });
    }

作为奖励,可以像这样捕获 STA(或 MTA)线程中抛出的异常:

try
{
    new XTATestRunner().RunSTA(() => {
        throw new InvalidOperationException();
    });
}
catch (InvalidOperationException ex)
{
}

我刚刚使用以下内容从 STA 线程获取剪贴板内容。 以为我会发帖以帮助将来的某人...

string clipContent = null;
Thread t = new Thread(
    () =>
    {
        clipContent = Clipboard.GetText();
    });
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();

// do stuff with clipContent

t.Abort();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM