コード例 #1
0
        private void MulitipleFile()
        {
            #region 路径

            var selectedDirectory = Tb_SourceDataPath.Text.Trim();
            if (string.IsNullOrEmpty(selectedDirectory))
            {
                MessageBox.Show("路径不能空撒", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Tb_SourceDataPath.Focus();
                return;
            }

            #endregion

            var resultSaveType = (ResultSaveType)Cb_ResultSaveType.SelectedValue;
            Btn_Calcualte.Enabled = false;
            Btn_Calcualte.Text    = "正在计算...";
            var type = (int)Cb_DataType.SelectedValue;

            Task.Run(() =>
            {
                var files = Directory.GetFiles(selectedDirectory, "*.dat");
                foreach (var file in files)
                {
                    _recordStack = new ConcurrentBag <InputData>();

                    var fileinfo = new FileInfo(file);

                    var dataList = FileHelper.ReadBaseData(file);

                    #region 计算

                    Debug.WriteLine($"file:{fileinfo.Name}.一类:{dataList.Count(c => c.DataType == DataType.Yilei)},二类:{dataList.Count(c => c.DataType == DataType.Erlei)},三类:{dataList.Count(c => c.DataType == DataType.Sanlei)}");
                    if (type > 0)
                    {
                        var dataType = (DataType)type;
                        dataList     = dataList.Where(c => c.DataType == dataType).ToList();
                    }
                    Lb_Total.Invoke(new Action(() =>
                    {
                        Lb_Total.Text = dataList.Count.ToString();
                    }));

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    Parallel.ForEach(dataList, inputData =>
                    {
                        Calculate(inputData, config);

                        Lb_Finished.Invoke(new Action(() => { Lb_Finished.Text = _recordStack.Count(c => c.T > 0 || c.T < 0).ToString(); }));
                        Lb_TotalTime.Invoke(new Action(() => { Lb_TotalTime.Text = $"(正在计算:{fileinfo.Name})"; }));
                        Debug.WriteLine($"正在计算{fileinfo.Name},当前执行完成了{_recordStack.Count}.耗时:{stopwatch.ElapsedMilliseconds / 1000.0}s)");
                    });

                    Lb_Finished.Invoke(new Action(() => { Lb_Finished.Text = _recordStack.Count().ToString(); }));

                    SaveFile(file, dataList, resultSaveType);

                    #endregion
                }
            }).ContinueWith(task =>
            {
                //文件已经保存了再设置按钮的可用状态
                Btn_Calcualte.Invoke(new Action(() =>
                {
                    Btn_Calcualte.Enabled = true;
                    Btn_Calcualte.Text    = "计算";
                }));
                Lb_TotalTime.Invoke(new Action(() => { Lb_TotalTime.Text = "计算完毕,请查看文件内容"; }));
                Lb_Total.Invoke(new Action(() => { Lb_Total.Text = "0"; }));
                Lb_Finished.Invoke(new Action(() => { Lb_Finished.Text = "0"; }));

                var openFileConfirm = MessageBox.Show("计算完成。是否打开结果文件夹", "提示", MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Information);
                if (openFileConfirm == DialogResult.Yes)
                {
                    Process.Start("explorer.exe", selectedDirectory);
                }
            });
        }
コード例 #2
0
        private void SingleFile()
        {
            _recordStack = new ConcurrentBag <InputData>();
            var resultSaveType = (ResultSaveType)Cb_ResultSaveType.SelectedValue;

            #region 路径

            var path = Tb_SourceDataPath.Text.Trim();
            if (string.IsNullOrEmpty(path))
            {
                MessageBox.Show("路径不能空撒", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Tb_SourceDataPath.Focus();
                return;
            }

            var dataList = FileHelper.ReadBaseData(path);
            if (dataList.Count == 0)
            {
                MessageBox.Show("指定的文件没有读取到合法数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            #endregion

            #region 计算

            Btn_Calcualte.Enabled = false;
            Btn_Calcualte.Text    = "正在计算...";

            Debug.WriteLine($"一类:{dataList.Count(c => c.DataType == DataType.Yilei)},二类:{dataList.Count(c => c.DataType == DataType.Erlei)},三类:{dataList.Count(c => c.DataType == DataType.Sanlei)}");
            var type = (int)Cb_DataType.SelectedValue;
            if (type > 0)
            {
                var dataType = (DataType)type;
                dataList = dataList.Where(c => c.DataType == dataType).ToList();
            }

            Lb_Total.Text = dataList.Count().ToString();
            Task.Run(() =>
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                var total   = 30;
                var average = Math.DivRem(dataList.Count, total, out var mod);
                if (mod != 0)
                {
                    average += total;
                }

                for (int j = 0; j < total; j++)
                {
                    var subTemp = dataList.Skip(j * average).Take(average).ToList();
                    Debug.WriteLine($"j={j},count={subTemp.Count}");

                    var result = Parallel.ForEach(subTemp, inputData =>
                    {
                        Calculate(inputData, config);

                        if (_recordStack.Count % 1000 == 0)
                        {
                            Lb_Finished.Invoke(new Action(() => { Lb_Finished.Text = _recordStack.Count(c => c.T > 0 || c.T < 0).ToString(); }));

                            Lb_TotalTime.Invoke(new Action(() => { Lb_TotalTime.Text = $"(耗时:{stopwatch.ElapsedMilliseconds / 1000.0}s)"; }));
                            Debug.WriteLine($"当前执行完成了{_recordStack.Count}.耗时:{stopwatch.ElapsedMilliseconds / 1000.0}s)");
                        }
                    });
                }

                Lb_Finished.Invoke(new Action(() => { Lb_Finished.Text = _recordStack.Count().ToString(); }));

                var savedFile = SaveFile(path, dataList, resultSaveType);

                //文件已经保存了再设置按钮的可用状态
                Btn_Calcualte.Invoke(new Action(() =>
                {
                    Btn_Calcualte.Enabled = true;
                    Btn_Calcualte.Text    = "计算";
                }));

                Lb_TotalTime.Invoke(new Action(() =>
                {
                    Lb_TotalTime.Text = string.IsNullOrEmpty(Lb_TotalTime.Text)
                        ? "计算完毕,请查看文件内容."
                        : Lb_TotalTime.Text.Insert(1, "计算完毕,请查看文件内容.");
                }));

                var openFileConfirm = MessageBox.Show("计算完成。是否打开结果文件夹", "提示", MessageBoxButtons.YesNo,
                                                      MessageBoxIcon.Information);
                if (openFileConfirm == DialogResult.Yes)
                {
                    Process.Start("explorer.exe", $@"/select,{savedFile}");
                }
            });

            #endregion
        }