【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;
        }
相关推荐
HealthScience6 分钟前
【Bib 2026】基因最新综述(有什么任务、benchmark、代表性模型)
android·开发语言·kotlin
wjs20247 分钟前
CSS 网格元素
开发语言
Java小白笔记26 分钟前
OpenClaw 实战方法论
java·开发语言·人工智能·ai·全文检索·ai编程·ai写作
CoderCodingNo39 分钟前
【信奥业余科普】C++ 的奇妙之旅 | 12:程序的交互与加工——数据的输入与算术运算
开发语言·c++
S1998_1997111609•X2 小时前
MacOS/ˉsh(so.))os.apkair/AI
开发语言·网络·人工智能
SimpleLearingAI2 小时前
C++虚函数详解
开发语言·c++
Dxy12393102162 小时前
Python使用XPath定位元素:动态计算与函数调用
开发语言·python
Evand J2 小时前
【MATLAB代码介绍】三种CT模型的IMM(交互式多模型)对目标高精度定位
开发语言·matlab·ct·imm·交互式多模型·多模型·转弯
AC赳赳老秦2 小时前
OpenClaw权限管理实操:团队共享Agent,设置操作权限,保障数据安全
服务器·开发语言·前端·javascript·excel·deepseek·openclaw
geovindu2 小时前
go: Proxy Pattern
开发语言·后端·设计模式·golang·代理模式