【WPF 按钮点击后异步上传多文件code示例】

前言: WPF中按钮点击事件如何执行时间太长会导致整个UI线程卡顿,现象就是页面刷新卡住,点击其他按钮无反馈。如下是进行异步执行命令,并远程上传文件的代码。
c 复制代码
// 这里对于长时间执行的任务,必须使用异步方法进行处理
        private async void SaveCameraConfig(object obj)
        {
            SystemModel device = SystemModel.GetGlobalInstance();
            int robotindex = device.SelectedRobotIndex;
            string selectRobotIp;
            string temp;
            if (!Directory.Exists(hostFilePath))
            {
                temp = "相机本地配置文件夹不存在,请检查!";
                Growl.Error(temp, _token);
                return;
            }
            if (!device.robotIdToIpDict.TryGetValue(robotindex, out selectRobotIp))
            {
                temp = "Robot " + (robotindex + 1) + " not connected yet, load faliure!";
                Growl.Error(temp, _token);
                return;
            }
            Task<bool> task = Task.Run(() => ExecuteLongProcedure(this, selectRobotIp, remoteFilePath, hostFilePath, userName, passWord));
            await Task.WhenAll(task);
            bool result = task.Result;
            if (result)
            {
                device.LoadCameraConfigFlag = true;
                temp = "相机配置文件加载成功!";
                Growl.Success(temp, _token);
            } else {
                temp = "相机配置文件传输失败!";
                Growl.Error(temp, _token);
            }
        }

        private bool ExecuteLongProcedure(object context, string selectRobotIp, string remoteFolderPath, string hostFolderPath, string userName, string passWord)
        {
            bool transferFlag = true;
            string temp;
            string[] filePaths = Directory.GetFiles(hostFolderPath);
            foreach (string filepath in filePaths)
            {
                string filename = Path.GetFileName(filepath);
                // 网络摄像头分组A的配置文件均进行发送
                if (!filename.EndsWith("Group0.config"))
                {
                    continue;
                }
                string remoteFilePath = $"{remoteFolderPath}/{filename}";
                using (var client = new SftpClient(selectRobotIp, userName, passWord))
                {
                    try
                    {
                        client.Connect();
                        using (var fileStream = new FileStream(filepath, FileMode.Open))
                        {
                            client.UploadFile(fileStream, remoteFilePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        transferFlag = false;
                        temp = filename + $"文件传输失败:{ex.Message}";
                        Growl.Error(temp, _token);
                        return false;
                    }
                    finally
                    {
                        if (client.IsConnected)
                        {
                            client.Disconnect();
                        }
                    }
                }
            }
            return transferFlag;
        }
相关推荐
xh didida29 分钟前
算法 -- 位运算
开发语言·c++·算法
谙弆悕博士36 分钟前
快速学C语言——第2章:编程规范与代码风格
服务器·c语言·开发语言·经验分享·程序人生·学习方法·业界资讯
byzh_rc1 小时前
[AI编程从入门到入土] 装饰器decorator
开发语言·python·ai编程
贫民窟的勇敢爷们1 小时前
Java 与 Python 如何选型与融合
java·开发语言·python
流氓也是种气质 _Cookie1 小时前
Chrome Performance常见名词解释(FP, FCP, LCP, DCL, FMP, TTI, TBT, FID, CLS)
开发语言·javascript·ecmascript
gihigo19981 小时前
基于MATLAB的LTE物理层仿真系统
开发语言·matlab
刚子编程1 小时前
C# Join 实战:左连接写法、字符串拼接与 EF Core 性能调优
开发语言·c#·solr·join
fie88892 小时前
基于粒子群优化(PSO)算法的带STATCOM的IEEE 30节点系统最优潮流MATLAB实现
开发语言·算法·matlab
Stream_Silver2 小时前
【JNA实战:Java无缝调用Windows API模拟键盘输入】
java·开发语言·windows
焦糖玛奇朵婷2 小时前
回收小程序开发案例分享
java·开发语言