作者: 剑圣大法师
查看: 1612|回复: 1
打印 上一主题 下一主题

多线程 委托 界面卡死

[复制链接]
跳转到指定楼层
楼主
剑圣大法师 发表于 2018-8-8 19:59:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
查看: 1612|回复: 1
各位前辈大家好,我刚写C#程序,遇到点问题请多赐教。
想做日志查询功能,由于日志文件比较大,几万行,所以考虑将费时的操作放在子线程内执行,使用streamReader读文件的每一行,一旦符合条件调用BeginInvoke通知textBox,然后更新显示文本。
但是仍然卡死,原因在哪呢?

这是主窗口
[C#] 纯文本查看 复制代码
logTextBox1 = new logTextBox();                              
foreach (string logFilePtah in Paths)
                    {                       
                        Thread objThread = new Thread(new ThreadStart(delegate
                        {
                            logTextBox1.logFilter(logFilePtah, logFilterCriteria);
                        }));
                        objThread.Start();
                    }
                    logTextBox1.Show();



点击按钮会弹出查询结果的新窗口
[C#] 纯文本查看 复制代码
 public void logFilter(String LogFilePath, LogFilterCriteria logFilterCriteria)
        {
            try
            {
                if (File.Exists(LogFilePath))
                {
                    using (FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        using (StreamReader sr = new StreamReader(fs))
                        {
                            string nextLine;
                            while ((nextLine = sr.ReadLine()) != null)
                            {
                                if (logFilterCriteria.ModuleName != null)
                                {
                                    //add ".(" to filter some unexpected situation
                                    if (!nextLine.Contains(logFilterCriteria.ModuleName + ".(")) continue;
                                }
                                if (logFilterCriteria.FuncState != null)
                                {
                                    if(logFilterCriteria.FuncState != "EXIT FUNCTION")
                                    {
                                        if (!nextLine.Contains(logFilterCriteria.FuncState)) continue;
                                    }
                                    else
                                    {
                                        if (!nextLine.Contains(logFilterCriteria.FuncState)) { continue; }
                                        else if (nextLine.Contains("NORMAL")) { continue; }
                                        else { }
                                    }    
                                }
                                if (logFilterCriteria.WarningLevel != null)
                                {
                                    if (!nextLine.Contains(logFilterCriteria.WarningLevel)) continue;
                                }
                                if (logFilterCriteria.Keyword1 != null)
                                {
                                    if (!nextLine.Contains(logFilterCriteria.Keyword1)) continue;
                                }
                                if (logFilterCriteria.Keyword2 != null)
                                {
                                    if (!nextLine.Contains(logFilterCriteria.Keyword2)) continue;
                                }
                                 
                                if (logFilterCriteria.Checked1 || logFilterCriteria.Checked2)
                                {
                                    Regex regDateTime = new Regex(@"[\d]{4}\-[\d]{2}\-[\d]{2}\s[\d]{2}\:[\d]{2}\:[\d]{2}");
                                    Match matDateTime = regDateTime.Match(nextLine);
                                    if (matDateTime.Success)
                                    {
                                        dtDateTime = Convert.ToDateTime(matDateTime.Value);
                                    }
                                    else
                                    {
                                        continue;
                                    }
                                    if (logFilterCriteria.Checked1 && !logFilterCriteria.Checked2)
                                    {
                                        if (DateTime.Compare(dtDateTime, logFilterCriteria.StartTime) < 0) { continue; }
                                    }
                                    else if (!logFilterCriteria.Checked1 && logFilterCriteria.Checked2)
                                    {
                                        if (DateTime.Compare(dtDateTime, logFilterCriteria.EndTime) > 0) { continue; }
                                    }
                                    else
                                    {
                                        if (DateTime.Compare(logFilterCriteria.StartTime, logFilterCriteria.EndTime)>0)
                                        {
                                            //MessageBox.Show("Start-time must be ahead of end-time!");
                                            break;
                                        }
                                        if (DateTime.Compare(dtDateTime, logFilterCriteria.EndTime) > 0 || 
                                            DateTime.Compare(dtDateTime, logFilterCriteria.StartTime) < 0)
                                        {
                                            continue;
                                        }
                                    }
                                }
                                this.BeginInvoke(updateTxt, nextLine);
                                //textBox1.AppendText(nextLine + "\r\n");
                            }
                            sr.Dispose();
                        }
                        fs.Dispose();
                    }

                }
                else
                {
                    //MessageBox.Show("Not found log file");
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播
回复 论坛版权

使用道具 举报

沙发
milest 发表于 2018-8-31 09:06:34 | 只看该作者
这样不容易看出
您需要登录后才可以回帖 登录 | 加入CSkin博客

本版积分规则

QQ|申请友链|小黑屋|手机版|Archiver|CSkin ( 粤ICP备13070794号

Powered by Discuz! X3.2  © 2001-2013 Comsenz Inc.  Designed by ARTERY.cn
GMT+8, 2024-12-5 10:08, Processed in 2.570163 second(s), 29 queries , Gzip On.

快速回复 返回顶部 返回列表