C#async await Task使用例子

private async Task GetJobsFromRC()
        {
            RobotInfo robot = OutdoorUIPlatform.GetInstance().CurrentSelectedRobot;
            if (robot == null) return false;    //未选择机器人
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择所在文件夹";
            if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return false;  //直接点取消
            if (string.IsNullOrEmpty(dialog.SelectedPath)) return false;                    //未选择路径
            bool res = await Task.Run(() =>
            {
                Console.WriteLine("加载作业开始");
                bool result = FtpHelper.Download(robot.RCIpAddr, "", "", Constants.JobsRemotePath, dialog.SelectedPath + "\\Job.xml");
                Console.WriteLine("加载作业结束");
                return result;
            });
            return res;
        }

以上代码是异步使用FTP下载文件并返回结果。

你可能感兴趣的:(C#async await Task使用例子)