Пример #1
0
        private void FilesWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List <FileInfo>  lf = (List <FileInfo>)e.Argument;
            DataTimeComparer dc = new DataTimeComparer();

            lf.Sort(dc);
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 0; i < lf.Count; i++)
            {
                using (BinaryReader br = new BinaryReader(lf[i].OpenRead()))
                {
                    byte[] b = br.ReadBytes((int)lf[i].Length);

                    int    NodeId;
                    string time;
                    byte[] data;

                    if (isCommType)//串口数据
                    {
                        if (DepackData(b, out NodeId, out time, out data))
                        {
                            if (NodeId != 0)//收到的节点号
                            {
                                try
                                {
                                    SourceDataClass.GetData(data);
                                    SourceDataClass.ParseToLst();
                                    if (SourceDataClass.datalist.Count > 0)
                                    {
                                        foreach (string[] s in SourceDataClass.datalist)
                                        {
                                            s[2] = time;
                                        }
                                        DetailLstOfData.AddRange(SourceDataClass.datalist);
                                    }
                                    if (SourceDataClass.statuslist.Count > 0)
                                    {
                                        foreach (string[] s in SourceDataClass.statuslist)
                                        {
                                            s[1] = time;
                                        }

                                        DetailLstOfStatus.AddRange(SourceDataClass.statuslist);
                                    }
                                    TimeLst.Add(time);
                                    TimeData.Add(time, data);
                                }
                                catch (Exception ex)
                                {
                                    Debug.WriteLine(ex.Message);
                                    worker.ReportProgress((i + 1) * 100 / lf.Count);
                                    continue;
                                }
                            }
                        }
                        else
                        {
                            FileInfo f = new FileInfo(lf[i].FullName);
                            Debug.WriteLine(lf[i].FullName + "解析错误");
                        }
                    }
                    else//网络数据
                    {
                        try
                        {
                            SourceDataClass.GetData(b);
                            int ret = SourceDataClass.ParseToLst();
                            time = lf[i].LastWriteTime.ToString();
                            if (ret == 1)
                            {
                                time += "(回环应答)";
                            }
                            if (SourceDataClass.datalist.Count > 0)
                            {
                                foreach (string[] s in SourceDataClass.datalist)
                                {
                                    s[2] = time;
                                }
                                DetailLstOfData.AddRange(SourceDataClass.datalist);
                            }
                            if (SourceDataClass.statuslist.Count > 0)
                            {
                                foreach (string[] s in SourceDataClass.statuslist)
                                {
                                    s[1] = time;
                                }

                                DetailLstOfStatus.AddRange(SourceDataClass.statuslist);
                            }
                            TimeLst.Add(time);
                            TimeData.Add(time, b);
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine(ex.StackTrace);
                            worker.ReportProgress((i + 1) * 100 / lf.Count);
                            continue;
                        }
                    }
                }
                worker.ReportProgress((i + 1) * 100 / lf.Count);
            }
        }