private void button4_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(FilePath)) { return; } try { //由于Consol的处理时间比较长,所以用异步方法 Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var task = consolHandler.GetShipmentAsync(FilePath);//获取Shipment ConsolBtn.Text = "Process waiting...."; task.ContinueWith((t) => { stopwatch.Stop(); var ts = stopwatch.Elapsed; string elapsedTime = String.Format("Shipment 提取完成,用时 {0}.{1:000}秒", ts.Seconds, ts.Milliseconds); Consol = t.Result; MessageBox.Show(elapsedTime); var str = "Consol"; var fun = new Action <string>((text) => { this.ConsolBtn.Text = text; }); if (this.InvokeRequired) { this.Invoke(fun, str); } //触发事件 consolHandler.RaiseExtractCompleted(Consol.BookingConfirmationReference, Consol.WayBillNumber); }); float second = 0f; var fun2 = new Action(() => { ConsolBtn.Text = "Processing " + String.Format("{0:N2} ", second) + " Seconds"; }); var clockTask = Task.Run(async() => { while (!task.IsCompleted) { await Task.Delay(100); if (this.ConsolBtn.InvokeRequired) { second += 0.1f; this.ConsolBtn.Invoke(fun2); } } }); } catch (NullReferenceException err) { MessageBox.Show("Null引用错误:" + err.Message); } catch (FileNotFoundException ferr) { MessageBox.Show(ferr.Message); } }
private void button1_Click_1(object sender, EventArgs e) { var dlgReslt = openFileDialog1.ShowDialog(); if (dlgReslt == DialogResult.OK) { var files = openFileDialog1.FileNames; var actions = new Action[files.Length]; for (var i = 0; i < files.Length; i++) { var fn = files[i]; actions[i] = new Action(async() => { Consol = await consolHandler.GetShipmentAsync(fn); var newf = consolHandler.ConvertInstanceToFile(Consol, @"XML\Consol" + DateTime.Now.Ticks + ".xml"); Console.WriteLine("任务完成:" + newf); }); } Parallel.Invoke(actions); } }
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("请选择文件"); } }