简体   繁体   English

监控C#线程 - 哪个/什么时候做什么

[英]Monitoring C# Threads - Which does what/when

As everybody, I am used to debugging my code in VS in step-by-step mode. 作为每个人,我习惯于在逐步模式下调试VS中的代码。 Well, now that I have an application with many Background Workers everywhere, I am not in Kansas anymore. 好吧,既然我到处都有很多背景工作者的申请,我不再在堪萨斯州了。

What is the most efficient way to debug threaded applications and be able to monitor each and every thread to keep track of what's happening all over the code? 调试线程应用程序的最有效方法是什么,并能够监视每个线程以跟踪代码中发生的情况?

As of now, I stick to good ol' debugging using separate logger instances for each Thread, but this is slowly becoming a nightmare and I'll soon be drowning into my own logs. 截至目前,我坚持使用单独的记录器实例为每个线程进行良好的调试,但这正在慢慢变成一场噩梦,我很快就会淹没在我自己的日志中。

Don't try to debug everything all at once. 不要试图一次调试所有内容。 Narrow your focus to a particular behavior in one thread or pair of threads that interact around some mutex lock. 将焦点缩小到一个线程或一对线程中的特定行为,这些线程围绕某些互斥锁进行交互。 If accessing a shared resource is the problem, set breakpoints around use of that resource (which should be in common code, not all over the place). 如果访问共享资源是问题,请设置使用该资源的断点(应该使用公共代码,而不是整个地方)。

If you just want to see that thread 3 completed before thread 1, or that thread 2 used up all its work items and is sitting idle, use logs for that. 如果您只是想在线程1之前看到线程3完成,或者线程2用完了所有工作项并且处于空闲状态,请使用日志。

You can also use the VS Threads view to see what each thread is doing whenever the process is stopped at any breakpoint on any thread. 您还可以使用VS Threads视图查看每个线程在任何线程上的任何断点处停止进程时正在执行的操作。 This can give you some insight into what all the threads are doing at any given instant. 这可以让您深入了解所有线程在任何给定时刻所做的事情。

A small tip that might ease your pain is to use Visual Studio to freeze threads that you are not interested in. Then when you tell the debugger to continue, the frozen threads will never execute and will not hit breakpoints and confuse you. 一个可能减轻您痛苦的小技巧是使用Visual Studio来冻结您不感兴趣的线程。然后,当您告诉调试器继续时,冻结的线程将永远不会执行,也不会遇到断点并使您感到困惑。

Maybe you can use this method to allow only the threads you are debugging to work. 也许您可以使用此方法仅允许您正在调试的线程工作。 Eg keep one thread that enqueues and one thread that dequeues active, but freeze everything else. 例如,保留一个入队的线程和一个出列的线程,但冻结其他所有线程。

You can freeze/thaw threads from Visual Studio's Threads window, by right-clicking on a thread. 通过右键单击一个线程,可以从Visual Studio的Threads窗口冻结/解冻线程。

Write it correctly the first time. 第一次正确写。

Joking aside, the trick to debugging is to break it down into manageable parts. 开玩笑说,调试的诀窍是将其分解为可管理的部分。 Tackle one worker task at a time, make absolutely sure it does what it's supposed to. 一次解决一个工人的任务,确保它完成它应该做的事情。

Once you've done that, debugging issues in the main thread is a lot easier, because you can pretty much ignore the background workers and just presume they're yielding correct results when they should be. 一旦你完成了这一点,主线程中的调试问题就容易多了,因为你几乎可以忽略后台工作者,只是假设他们应该得到正确的结果。

The only place left that's harder to debug than a single-threaded application is the interconnect between the threads, which shouldn't be much more difficult if you're using the libraries the way you should be. 剩下比单线程应用程序更难调试的地方是线程之间的互连,如果你按照应该的方式使用库,这应该不会困难得多。

I Stumbled upon a detailed MSDN article about Debugging Multithreaded Applications wich was of great help. 我偶然发现了一篇关于调试多线程应用程序的详细MSDN文章,这篇文章非常有帮助。 Thanx for all the previous answers that guided me towards the right track. 所有以前的答案都引导我走向正确的轨道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM