Exemplo n.º 1
0
        private void button2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(FilePath))
            {
                return;
            }
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            try
            {
                var task = bookingHandler.GetShipmentAsync(FilePath);//获取Shipment
                task.ContinueWith(t => {
                    Booking = t.Result;
                    stopwatch.Stop();
                    var ts             = stopwatch.Elapsed;
                    string elapsedTime = String.Format("Shipment 提取完成,用时 {0}.{1}秒",
                                                       ts.Seconds, ts.Milliseconds);
                    MessageBox.Show(elapsedTime);
                    //触发事件
                    bookingHandler.RaiseExtractCompleted(Booking.BookingConfirmationReference, Booking.WayBillNumber);
                });
            }
            catch (NullReferenceException err)
            {
                MessageBox.Show("Null引用错误:" + err.Message);
            }
            catch (FileNotFoundException ferr)
            {
                MessageBox.Show(ferr.Message);
            }
        }
Exemplo n.º 2
0
        private void mutiConvertBtn_Click(object sender, EventArgs e)
        {
            //转换多个文件开始
            if (openFileDialog1.FileNames.Length > 0)
            {
                tabControl1.TabPages[1].Focus();
                var conversion = (ConvertType)Enum.Parse(typeof(ConvertType), typeComBo.Text + string.Empty);
                var files      = openFileDialog1.FileNames;
                var actions    = new Action[files.Length];
                this.mutiConvertBtn.Enabled = !this.mutiConvertBtn.Enabled;
                //根据不同的选择类型进行不同的处理
                switch (conversion)
                {
                //Booking,Consol 流程与shipment 一样,不加注释
                case ConvertType.Shipment:
                    for (var i = 0; i < files.Length; i++)
                    {
                        var fn = files[i];
                        //如果不加入idx变量,等到运行的时候,后缀会全部变量 i的最大值
                        var idx = i + 1;
                        actions[i] = new Action(() =>
                        {
                            NsShipment.Shipment newshipment = shipmentHandler.GetShipment(fn);
                            //构造文件名
                            var fName = @"XML\Shipment" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss_") + idx + ".xml";
                            var newf  = shipmentHandler.ConvertInstanceToFile(newshipment, fName);
                            Console.WriteLine(string.Format("任务{0}完成:{1}", idx, newf));

                            this.Invoke(new Action(() => {
                                //单个任务完成后在ResultTv 添加提示信息
                                ResultTv.Nodes.Add(new TreeNode(string.Format("任务{0}完成:{1}", idx, newf)));
                            }));
                            //触发文件保存完成事件
                            shipmentHandler.RaiseSaveFileCompleled(newf);
                        });
                    }
                    break;

                case ConvertType.Booking:
                    for (var i = 0; i < files.Length; i++)
                    {
                        var fn  = files[i];
                        var idx = i + 1;
                        actions[i] = new Action(() =>
                        {
                            NsBooking.Shipment newshipment = bookingHandler.GetShipment(fn);
                            var fName = @"XML\Booking" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss_") + idx + ".xml";
                            var newf  = bookingHandler.ConvertInstanceToFile(newshipment, fName);
                            Console.WriteLine(string.Format("任务{0}完成:{1}", idx, newf));
                            this.Invoke(new Action(() =>
                            {
                                ResultTv.Nodes.Add(new TreeNode(string.Format("任务{0}完成:{1}", idx, newf)));
                            }));
                            bookingHandler.RaiseSaveFileCompleled(newf);
                        });
                    }
                    break;

                case ConvertType.Consol:
                    for (var i = 0; i < files.Length; i++)
                    {
                        var fn  = files[i];
                        var idx = i + 1;
                        actions[i] = new Action(() =>
                        {
                            NsConsol.Shipment newshipment = consolHandler.GetShipment(fn);
                            var fName = @"XML\Consol" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss_") + idx + ".xml";
                            var newf  = consolHandler.ConvertInstanceToFile(newshipment, @"XML\Consol" + fName + ".xml");
                            Console.WriteLine(string.Format("任务{0}完成:{1}", idx, newf));
                            this.Invoke(new Action(() =>
                            {
                                ResultTv.Nodes.Add(new TreeNode(string.Format("任务{0}完成:{1}", idx, newf)));
                            }));
                            consolHandler.RaiseSaveFileCompleled(newf);
                        });
                    }
                    break;

                default:
                    MessageBox.Show("请选择正确的转换类型");
                    break;
                }
                ResultTv.Nodes.Clear();
                mutiConvertBtn.Text = "请等待...";
                Stopwatch sw = new Stopwatch();
                sw.Start();
                Parallel.Invoke(async() => {
                    var tlist = new List <Task>();
                    foreach (var a in actions)
                    {
                        tlist.Add(Task.Run(a));
                    }
                    await Task.WhenAll(tlist.ToArray());
                    this.Invoke(new Action(() =>
                    {
                        sw.Stop();
                        MessageBox.Show("全部任务完成,总耗时:" + sw.Elapsed.TotalSeconds + "秒");
                        mutiConvertBtn.Text         = "开始转换";
                        this.mutiConvertBtn.Enabled = !this.mutiConvertBtn.Enabled;
                    }));
                });
            }
            else
            {
                MessageBox.Show("请选择文件");
            }
        }