当前位置:   article > 正文

C#使用异步或者多线程更新WPF中DataGrid的值_c#跨线程更新表格数据

c#跨线程更新表格数据

wpf使用异步更新界面值

1.要更新的DataGrid的值,如下面的FilePaths
private string filePaths;
/// <summary>
 /// 文件类型
 /// </summary>
 public string FilePaths
 {
     get { return filePaths; }
     set
     {
         //filePaths = value;
         //RaisePropertyChanged(FilePaths);
         Set(ref filePaths, value);//这里是比对当前值后以前值有无变化,有变化则跟新
     }
 }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
2.c# 数组中的空值,使用C#删除数组中的空白值
string[] test={"1","","2","","3"};
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();
  • 1
  • 2
3.异步更新
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
    {
           Definitions.SetPaths[Convert.ToInt32(setPaths.FileNumbers) - 1].FilePaths = fd.FileName;
   }));
   
  • 1
  • 2
  • 3
  • 4
  • 5
4.async 和 await
async
public void Send()
{
 Task.Run(async () =>
   {
       return await sport.SendFile(FilePaths, FileTransmitType.XModem_1K, 5);
     }).GetAwaiter().OnCompleted(() => { });
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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("文件名不能为空");
 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

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);
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/884957
推荐阅读
相关标签
  

闽ICP备14008679号