天天看點

C#async await Task使用例子

private async Task<bool> 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<bool>.Run(() =>
            {
                Console.WriteLine("加載作業開始");
                bool result = FtpHelper.Download(robot.RCIpAddr, "", "", Constants.JobsRemotePath, dialog.SelectedPath + "\\Job.xml");
                Console.WriteLine("加載作業結束");
                return result;
            });
            return res;
        }
           

以上代碼是異步使用FTP下載下傳檔案并傳回結果。