using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Core.ONH.Collection.comm;
namespace Core.ONH.Collection.Send
{
public partial class Form1 : Form
{
private string path = "";
public Form1(string[] Args)
{
if (Args.Length <= 0)
{
if (MessageBox.Show("无法找到文件路径,是否手动选择文件", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c://";
openFileDialog.Filter = "数据文件|*.xml";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
path = openFileDialog.FileName;
}
else
{
this.Close();
}
}
else
{
this.Close();
}
}
else
{
foreach (string args in Args)
{
path += " " + args.Trim();
}
}
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Task t3 = new Task(AnalysisData);
t3.Start();
}
private void AnalysisData()
{
try
{
string data;
// 分析ONH原始文件中有效数据
ONHDataHelper.AnalysisXml(path, out data);
//通过串口发送数据
SerialPortHelper.Instance().SendData(data, Environment.CurrentDirectory + "\\Send.ini");
SafeRefreshControl(this, Hide);
}
catch (Exception ex)
{
SafeRefreshControl(this, Hide);
MessageBox.Show(ex.Message);
LogHelper.WriteException(typeof(Program), ex);
}
finally
{
//删除临时文件数据
if (File.Exists(path))
File.Delete(path);
Process.GetCurrentProcess().Kill();
this.Close();
}
}
///
/// 安全刷新界面控件 当为子线程时委托刷新界面
///
///
///
public void SafeRefreshControl(Control control, Action action)
{
if (control.InvokeRequired)
{
control.Invoke(action);
}
else
action();
}
}
}