调试工作线程运行的方法 [英] Debugging a method run by a worker thread

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

问题描述

我有一个类和一个私有方法,该类和私有方法由如下所示的工作线程运行.

I have a class and a private method which is run by a worker thread like below.

公共课测试
    {
       公开测试()
        {
            System.Threading.Thread线程=新的System.Threading.Thread(mainprocess);
            thread.Start();
        }
        private void mainprocess()
        {
            //一些代码
         }
    }

public class test
    {
        public test()
        {
            System.Threading.Thread thread = new System.Threading.Thread(mainprocess);
            thread.Start();
        }
        private void mainprocess()
        {
            //some code
         }
    }

当我调试单元测试时,代码有时可以正常运行,有时甚至可以从任何地方引发异常.并且异常详细信息类似无法评估表达式".有时F10会导致异常,但在F11时运行会很好.

When i am debugging unit test, code sometimes run fine and sometimes just throw exception from anywhere..and exception details are something like-- "unable to evaluate expression". Sometimes F10 results in exception but run fine when F11.

任何人都可以建议是否由于线程而发生了这种情况?这是线程的常见行为吗?

Can anyone suggets if thats happening because of the thread? Is it common behaviour of a thread?


推荐答案

您是否有任何异常处理,即在任何地方尝试/捕获块? (或处理UnhandledException事件?).

Do you have any exception handling, i.e. try/catch blocks anywhere? (or handling the UnhandledException event?).

如果没有,请在日志中添加一些内容(尽管请 令人烦恼的例外情况)!

If not - put something in along with some logging (although bear vexing exceptions in mind)!

在例外情况下,查看StackTrace.这应该告诉您异常在代码中的何处产生.

In your exception, look at the StackTrace. This should tell you where in your code the exception is originating.

加-您正在使用什么版本的Visual Studio?对于多线程代码,更高版本(2015?)的调试效果更好.

Plus - what version of Visual Studio are you using? Later versions (2015?) have better debugging when it comes to multithreaded code.

最后,如果您使用的是.Net的更高版本,则可能要考虑使用异步/等待技术而不是线程.

Lastly, if you are using a later version of .Net you may want to consider using async/await technology rather than threads.


这篇关于调试工作线程运行的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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