如何在一个WPF应用程序中同时使用线程和计时器? [英] How to use both thread and timer in one wpf application?

查看:183
本文介绍了如何在一个WPF应用程序中同时使用线程和计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wcf服务开发wpf复制.在此应用程序中使用线程和计时器. > 线程未运行.它已被挂起."
我该怎么办?

I am developing a wpf apllication using wcf services..In this application am using thread and timer..When i run the application its runnin,but when i want to suspend the thread am getting an error like the following
"Thread is not running..Its already suspended.."
What should i do?

推荐答案

首先,您不应该暂停线程.该方法被标记为已弃用,因此您应获得编译器使用它的警告.您为什么忽略此警告?不仅如此,您甚至试图挂起已经暂停的线程.

您可以使用均匀的等待句柄对象来限制线程的执行,而不是做这种不好的事情和不安全的事情.请参阅:
http://msdn.microsoft.com/en-us/library/system.threading. eventwaithandle.aspx [ ^ ].

当然,您始终可以在同一应用程序中使用计时器和线程.但是,许多初学者滥用计时器和使用不足的线程.在很多情况下,用计时器以尴尬,难以实现或不安全的方式解决问题时,会直接使用带有附加线程的可靠解决方案.如果您全面详细地解释了问题,则可以获得更多详细信息.

—SA
First of all, you should not suspend a thread. This method is marked deprecated, so you should get a compiler''s warning for using it. Why did you ignore this warning? More than that, you even tried to suspend already suspended thread.

Instead of doing this bad and unsafe thing, you can throttle thread execution using a even wait handle object. Please see:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^].

Of course, you can always use timer and threads in the same application. However, many beginners abuse timers and under-use threading. There are way too many cases when a problem solved with a timer in an awkward, hard to implement or unsafe way has a straightforward reliable solution with an additional thread. You could get more detail information if you explained your problem in comprehensive detail.

—SA


这篇关于如何在一个WPF应用程序中同时使用线程和计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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