2 end sun end_sun 于 2017.09.03 09:28 提问

c# winform 多线程异常无法捕获,导致程序闪退

伪代码为:
try
{
if (helper.TogetherBookThread != null && helper.TogetherBookThread.IsAlive) { return; }

            helper.TogetherBookThread = new Thread(new ThreadStart(delegate()
            {
                try
                {
                    while (true)
                    {

                        if (helper.TicketQuery != null)
                        {

                            Dictionary<string, TicketOrderCSun> TicketOrderCSunDic = helper.TicketQuery.TicketOrderCSunDic;
                            foreach (var cSun in TicketOrderCSunDic.Values)
                            {


                                bool flag = helper.DetermineBookOrderNewAction(cSun, helper);
                                if (flag)
                                {
                                    Thread.CurrentThread.Abort();
                                }
                            }
                        }


                        Thread.Sleep(1);



                    }
                }
                catch (ThreadAbortException)
                {

                }
                catch (Exception ex)
                {

                    if (!(ex is ThreadAbortException))
                    {

                        NetTicketCommonUtil.WriteTicketInfo("TogetherBookDetermineBookOrderThread " + ex.Message + " source:" + ex.StackTrace);
                    }
                }




            }));
            helper.TogetherBookThread.IsBackground = true;
            helper.TogetherBookThread.Priority = ThreadPriority.Highest;
            helper.TogetherBookThread.Start();

            //helper.TogetherBookThread.IsBackground = true;
            //helper.TogetherBookThread.Priority = ThreadPriority.Highest;
            ////helper.TogetherBookThread.ApartmentState = ApartmentState.STA;
            //helper.TogetherBookThread.Start();
        }
        catch (Exception ex)
        {
            if (!(ex is ThreadAbortException))
            {

                NetTicketCommonUtil.WriteTicketInfo("TogetherBookDetermineBookOrderThreadAAA:" + ex.Message + " source:" + ex.StackTrace);
            }

        }

                    每隔一毫秒就在里面判断是否有执行的任务,如果,有任务执行,并且成功,就退出线程。但是经常出现闪退。程序外面我已经加了全局捕获。但是无法捕获闪退日志。我查看windows日志,提示。
                    Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at NetTicket.Actions.NetTicketHelper+<>c__DisplayClassc.b__a()
at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ThreadHelper.ThreadStart()
提示空指针异常。我里面都执行都是try catch过。不明白为什么闪退。全局都是无提示的。。找了很久不知道原因

2个回答

caozhy
caozhy   Ds   Rxr 2017.09.03 09:41

你用lambda表达式的?那样不太好调试,最好写成一个普通的方法作为委托给你的线程,另外变量要同步啊,我看你字典都不同步就这么调,里面有null

end_sun
end_sun   2017.09.03 09:56

不行哦,我这里捕获不了异常关键,要是知道异常就好了,程序直接闪退了。。

end_sun
end_sun 回复caozhy: 同样的问题,我也不想问两遍,我按错了。。囧想搞个悬赏的。。我改了,也无法调试。直接跑到windows日志里面去了
3 个月之前 回复
caozhy
caozhy lambda表达式不方便调试,让你先改过来自然就能调试了。你非但不听还直接点踩,真是醉了。另外同一个问题不要反复提问,否则问题删除,c币没收。
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片