如何调试多线程C#Windows应用程序 [英] How to debug a multithreaded C# windows application

查看:588
本文介绍了如何调试多线程C#Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我有一个Windows应用程序,该应用程序使用具有多个线程的开源库(asterisk.net),但是当我在调试时进入代码时,线程之间的执行线会发生变化.这使我难以调试.如何使它逐行?

解决方案

您可以在调试时使用线程"窗口来实现此目的.如果要切换到线程,请右键单击所需的线程,然后单击切换到线程".如果找不到线程,请给它起一个名称以更快地找到它:

 yourThread.Name = " ; 


现在,该线程以该名称显示在线程"窗口中.

如果您不想切换到线程,则可以通过右键单击一个线程(或选择多个线程并单击鼠标右键)并选择冻结"来冻结不需要的线程.如果您不想再冻结它,请选择解冻".请注意,冻结线程意味着它不会运行,因此,如果要在线程之间同步数据,请不要使用Freeze.这将是最好的建议.一开始仅使用一个线程进行调试似乎也很困难,对吧?但是你习惯了.多线程调试也是如此.

您还应该了解,如果您在设计多线程代码时遇到问题,将无法找到正在使用的调试程序.测试也一样.您的设计应在理论上进行验证.原因之一是:代码可能具有竞赛条件,这意味着对执行顺序的不正确依赖.当您调试此类代码时,您会大大延迟一个线程相对于另一个线程,并且操作顺序会发生变化,因此该代码的行为与没有调试器时的行为不同.请参阅:
http://en.wikipedia.org/wiki/Race_condition [ 解决方案

You can achieve this by using the Threads window while debugging. If you want to switch to a thread, right-click on the desired thread and click "Switch To Thread". If you cannot find your thread, then give it a name to find it faster:

yourThread.Name = "Thread Name";


Now, the thread appears in the Threads window with this name.

If you don''t want to switch to threads, you can freeze the threads that you don''t need by right-clicking on a thread (or selecting multiple threads and right-clicking) and choosing "Freeze". If you don''t longer want to freeze it, choose "Thaw". Note that freezing a thread means that it doesn''t run, so if you want to synchronize data between threads, don''t use Freeze.


Keep debugging with "difficulties"; it would be the best advice. Just debugging, with a single thread, also seemed difficult at first, right? But you used to it. Same thing with multi-threading debugging.

You should also understand that if you have problems with the design of multi-threading code, you won''t be able to locate the using debugging. Same goes about testing. You design should be theoretically validated. One of the reasons for that is this: the code could have race conditions, which means incorrect dependency on the order of execution. When you debug such code, you strongly delay one thread relative to another one, and the order of operations changes, so the code won''t behave the same was as without the debugger. Please see:
http://en.wikipedia.org/wiki/Race_condition[^].

Worse, due to race conditions, you can have the code which only seems to behave correctly, but will fail eventually (say, when the application is already deployed to the customer :-)). The failure can be as bad as it can possibly be, such as, for example, a deadlock or something else.

At the same time, wide class of applications can be build based on simple well-known patterns of threading which never fail. This is all the matter of knowledge and experience.

—SA


这篇关于如何调试多线程C#Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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