wpf中的多线程 [英] Multi threading in wpf

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

问题描述

我需要创建多线程

像线程名称1,2,3 .. - 100

当线程1完成时线程2开始反之亦然线程100 ..是有可能创建??

解决方案

首先,问题的主题与WPF或任何其他UI无关,可以用任何方法完成。



更重要的是,这很有可能,并且完全无法实现多线程的目的。更准确地说,它相当于1个线程而不是100个,当这个1线程因此简单地调用那些100个线程的body方法时,一次一个。将存在一个区别:1个线程会比100更快地执行它,原因很明显:创建和初始化线程执行的开销很大。这个想法可以实现,但完全没有意义。



至于在WPF中使用线程,你可能需要学习一件事。由于您只能通过一个UI线程调用与当前正在执行的UI相关的任何方法或属性,而其他线程无法执行此操作,您需要知道委托UI-执行的调用技术UI线程的相关代码,通过 System.Windows.Threading.Dispatcher.Invoke System.Windows.Threading.Dispatcher.BegingInvoke 。请参阅:

http://msdn.microsoft .com / zh-cn / library / system.windows.threading.dispatcher.aspx [ ^ ]。



请同时查看我过去的答案:

Control.Invoke()vs. Control.BeginInvoke() [ ^ ],

Treeview扫描仪问题和MD5 [ ^ ]。



-SA

I need to create Multithread
Like thread name 1,2,3.. - 100
when Thread 1 finished Thread 2 start Vice versa thread 100 ..is it possible to create??

解决方案

First of all, the main topic of the quesiton has nothing to do with WPF or any other UI, could be done with any.

More importantly, this is quite possible and would totally defeat the purpose of multithreading. More exactly, it would be equivalent to 1 thread instead of 100, when this 1 thread simply consequently call the body methods of those 100 would-be-threads, one at a time. There will be one difference: 1 thread would do it faster then 100, by an apparent reason: considerable overhead of creation and initialization of a thread execution. The idea can be implemented but makes no sense at all.

As to using threads in WPF, you might need to learn one thing. As you can only call any methods or properties related to the currently executing UI through only one, UI thread, and other threads cannot do it, you would need to know invocation technique which delegates the execution of UI-related code to UI thread, which is done via System.Windows.Threading.Dispatcher.Invoke or System.Windows.Threading.Dispatcher.BegingInvoke. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.aspx[^].

Please also see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA


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

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