如何运行的东西在STA线程?

编程入门 行业动态 更新时间:2024-10-22 05:01:52
本文介绍了如何运行的东西在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线程?

本文发布于:2023-11-17 07:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   东西   STA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!