监视C#线程-做什么/何时 [英] Monitoring C# Threads - Which does what/when

查看:135
本文介绍了监视C#线程-做什么/何时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像每个人一样,我习惯于以逐步方式在VS中调试代码.好吧,既然我的应用程序到处都有许多后台工作人员,那么我就不再在堪萨斯州了.

As everybody, I am used to debugging my code in VS in step-by-step mode. 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).

如果您只是想看到线程3在线程1之前完成,或者该线程2用尽了所有工作项并处于空闲状态,请使用日志.

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.

您还可以使用VS线程视图查看何时进程在任何线程上的任何断点处停止时每个线程在做什么.这样可以让您深入了解所有线程在给定瞬间的运行情况.

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. This can give you some insight into what all the threads are doing at any given instant.

这篇关于监视C#线程-做什么/何时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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