WPF多线程 [英] WPF multithreading

查看:129
本文介绍了WPF多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力尝试让多线程在WPF中发挥作用

I'm scratching my head in trying to get the multithreading to work as I want in WPF

我有一个称为Manager的对象(具有单例),该对象进行了大量的处理和查找.我希望它在与UI分开的线程中运行,UI会调用Manager上的方法来执行处理并触发UI应对此做出反应的事件

I have an object (with a singleton) called Manager which does alot of processing and lookups. I want it to run in a separate thread from the UI, The UI will call methods on Manager to do the processing and fire events that the UI should react to

我正在从Windows窗体转换此应用程序,我认为这不是问题,因为textbox事件是在另一个线程中自动触发的.在WPF中并非如此,在WPF中一切似乎都停留在UI线程上.

I'm converting this app from windows forms where I dont think this was a problem since the textbox event was automatically fired in a different thread. Not so in WPF where everything seems to stay on the UI thread.

如何使对象在不同的​​线程中活动",然后如何调用其方法.我尝试在Window构造函数中使用它将其生成到新线程中

How do I make an object "live" in a different thread and then how do I call its methods. I tried spawning it to a new thread using this in the Window constructor

        Thread managerThread = new Thread(new ThreadStart(ManagerStartingPoint));
        managerThread.SetApartmentState(ApartmentState.STA);
        managerThread.IsBackground = true;
        managerThread.Start();

和:

private void ManagerStartingPoint()
    {
        ManagerSingleton.Manager = new Manager();
        MediatorSingleton.Mediator = new Mediator();
    }

在文本框textchanged事件上,我调用Manager.SetItemText(e.Value),该逻辑上应该在新线程上调用对吗?仍然,当我键入并且事件触发UI结结巴巴"时,在文本框中的键入会受到影响.我需要异步调用该方法还是其他方法?

On the textbox textchanged event I call Manager.SetItemText(e.Value) which logically should be called on the new thread right? Still when I type and the event triggers the UI "stutters" and typing in the textbox is affected. Do I need to call the method asynchronously or something?

推荐答案

您要做的就是在另一个线程上创建一个新对象.创建对象后,线程完成执行并变为空闲状态.

All you're doing is creating a new object on another thread. After you create the object, the thread completes execution and becomes idle.

对象实例不在线程上存在".实例只是指向内存中结构的指针.仅仅因为您在线程 A 中创建实例并不意味着该实例中的所有方法都将在线程 A 上运行.

Object instances don't "live" on threads. An instance is just a pointer to a structure in memory. Just because you create an instance in thread A doesn't mean that all methods in that instance will run on thread A.

您想要做的是创建一个可以从任何线程调用的类,该类使用其自己的内部托管线程(或某些此类构造)执行执行.

What you want to do is create a class that you can call from any thread but which uses its own internally managed thread (or some such construct) to perform execution.

您的经理"应该封装一个线程,或者使用ThreadPool来执行处理".完成后,管理器将必须与您的UI通信(使用Dispatcher将该通信编组回UI线程),以指示其已完成执行.

Your "manager" should encapsulate a thread, or use the ThreadPool, to perform your "processing." When done, the manager will have to communicate back to your UI (using the Dispatcher to marshall this communication back onto the UI thread) indicating it has completed execution.

这篇关于WPF多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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