赞
踩
private string filePaths;
/// <summary>
/// 文件类型
/// </summary>
public string FilePaths
{
get { return filePaths; }
set
{
//filePaths = value;
//RaisePropertyChanged(FilePaths);
Set(ref filePaths, value);//这里是比对当前值后以前值有无变化,有变化则跟新
}
}
string[] test={"1","","2","","3"};
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
Definitions.SetPaths[Convert.ToInt32(setPaths.FileNumbers) - 1].FilePaths = fd.FileName;
}));
public void Send()
{
Task.Run(async () =>
{
return await sport.SendFile(FilePaths, FileTransmitType.XModem_1K, 5);
}).GetAwaiter().OnCompleted(() => { });
}
await
//串口通信部分发送文件的代码 /// <summary> /// 发送文件 /// </summary> /// <param name="fileName">文件路径</param> /// <param name="type">传输协议类型</param> /// <param name="retryCount">重试次数</param> /// <param name="sendProgress">发送进度,int1为发送数据包长度,send2为当前发送包的编号</param> /// <param name="token">取消令牌,超时等待时可以在外部取消等待。在调用异步方法时,上次的任务可能还没结束,再次调用会导致当前任务读取不到数据。这种场景建议调用前先取消之前的任务。</param> /// <returns></returns> public async Task<bool> SendFile(string fileName, FileTransmitType type, int retryCount, Action<int, int> sendProgress = null, CancellationToken token = default) { if (fileName != null) { var result = false; if (type == FileTransmitType.XModem || type == FileTransmitType.XModem_1K) { if (_fileTransmit != null && _fileTransmit is XmodemHelper) { ((XmodemHelper)_fileTransmit).XModemType = type; } else { _fileTransmit = new XmodemHelper(this, type); //_fileTransmit.LoggerEvent += OnLoggerEvent; } result = await _fileTransmit.SendFile(fileName, retryCount, sendProgress, token); } return result; } throw new ArgumentNullException("文件名不能为空"); }
wpf不卡顿界面调用使用
/// <summary> /// 开始测试并保存数据 /// </summary> public void saveDataTest() { //string message = null; //bool processTest = false; if (PhaseShifterSN == null || DipAngleTest == null || PortNumbers == 0 || StatusFile==null) { MessageBox.Show("请检查移相器序列号,校准状态文件,移相器端口数,移相器测试倾角为空"); return; } string s2pPath = "D:\\PhaseShifterTest\\"+ PhaseShifterSN+"\\" + DipAngleTest + "\\"; if (instrs == null) { MessageBox.Show("请连接矢网"); return; } //else //{ // processTest = true; //} Task.Run(async () => { return await StartTest(s2pPath); }).GetAwaiter().OnCompleted(() => { }); } private async Task<bool> StartTest(string s2pPath) { int number = 0; string message = null; SaveDataEnabled = false; RaceSwitch raceSw = new RaceSwitch(); if (raceSw.Connect(false, out message)) { ResultInstrument = message; instrs.LoadCSA(StatusFile); for (int i = 1; i < PortNumbers; i++) { number = i + 1; if (raceSw.Select_Switch_port(1, number, 50, out message, false)) { Application.Current.Dispatcher.Invoke(new Action(() => { ResultInstrument = message; })); Thread.Sleep(2000); string path = s2pPath + DipAngleTest + "_" + i + ".S2P"; if (!instrs.SaveS2P(path, out message)) { Application.Current.Dispatcher.Invoke(new Action(() => { ResultInstrument = message; MessageBox.Show(s2pPath + ",保存失败," + message, "提示"); SaveDataEnabled = true; })); return await Task.FromResult(true); } } else { Application.Current.Dispatcher.Invoke(new Action(() => { MessageBox.Show("开关切换失败,\r\n" + "端口1" + ",端口" + number + "\r\n是否重新切换", "提示"); SaveDataEnabled = true; })); return await Task.FromResult(true); ; } } } else { Application.Current.Dispatcher.Invoke(new Action(() => { MessageBox.Show("矩阵开关连接失败," + message); SaveDataEnabled = true; })); } Application.Current.Dispatcher.Invoke(new Action(() => { MessageBox.Show("保存成功"); PhaseShifterSN = ""; PortNumbers = 0; DipAngleTest = ""; SaveDataEnabled = true; })); return await Task.FromResult(true); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。