如何运行的东西在STA线程? [英] How to run something in the STA thread?

查看:290
本文介绍了如何运行的东西在STA线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序我做了一些异步通信(与服务器)。在回调函数中我最终从服务器的结果产生墨水presenter对象。这需要运行的线程是STA,这显然它目前不是。因此,我得到了以下异常:

  

无法创建的水墨presenter汇编定义的实例[..]调用线程必须STA,因为许多UI组件都需要这个。

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

 公共无效SearchForFooAsync(字符串searchString)
{
    VAR来电=新的函数功能:LT;字符串,富>(_ patientProxy.SearchForFoo);
    caller.BeginInvoke(searchString,新的AsyncCallback(SearchForFooCallbackMethod),NULL);
}
 

我怎样才能让回调 - 这将做墨水presenter创作 - 是STA?或者调用的XamlReader分析在一个新的STA线程。

 公共无效SearchForFooCallbackMethod(IAsyncResult的AR)
{
    无功富= GetFooFromAsyncResult(AR);
    VAR油墨presenter = XamlReader.Parse(foo.Xaml)作为油墨presenter; //&所述;! - 需要STA
    [..]
}
 

解决方案

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

 线程的线程=新主题(MethodWhichRequiresSTA);
    thread.SetApartmentState(ApartmentState.STA); //设置线程STA
    thread.Start();
    的Thread.join(); //等待线程结束
 

唯一的问题是,你的结果对象必须同时在某种程度上跳水过去了。你可以使用一个私有字段为,或进入沿参数传递到线程。在这里,我在一个私有字段设置foo的数据,并启动STA线程变异油墨presenter!

 私人变种FOO;
公共无效SearchForFooCallbackMethod(IAsyncResult的AR)
{
    富= GetFooFromAsyncResult(AR);
    线程线程=新主题(ProcessInk presenter);
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    的Thread.join();
}

私人无效ProcessInk presenter()
{
    VAR油墨presenter = XamlReader.Parse(foo.Xaml)作为油墨presenter;
}
 

希望这有助于!

In my WPF application I do some async communication (with server). In the callback function I end up creating InkPresenter objects from the result from server. This requires the running thread to be STA, which apparently it currently isn't. Therefore I get the following exception:

Cannot create instance of 'InkPresenter' defined in assembly [..] The calling thread must be STA, because many UI components require this.

Currently my async function call is like this:

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

How can I make the callback - which will do the InkPresenter creation - be STA? Or invoke the XamlReader parsing in a new STA thread.

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

解决方案

You can start STA Threads like so:

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

The only problem is that your result object must be passed along somehow.. You can use a private field for that, or dive into passing along parameters into threads. Here I set the foo data in a private field and start up the STA Thread to mutate the 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;
}

Hope this helps!

这篇关于如何运行的东西在STA线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆