private async Task TakeImage() { var cameraOpts = new CameraMediaStorageOptions(); cameraOpts.PercentQuality = 50; cameraOpts.MaxPixelDimension = 1200; var result = await UIUtils.ShowSelectList("Take a photo", "Choose from library", this); System.Threading.Tasks.Task <MediaFile> taskMedia = null; if (result == 1) { taskMedia = DependencyService.Get <IMediaPicker>().TakePhotoAsync(cameraOpts); } else if (result == 2) { taskMedia = DependencyService.Get <IMediaPicker>().SelectPhotoAsync(cameraOpts); } else { return; } await taskMedia.ContinueWith((t, o) => { if (t.IsCanceled || t.Result == null) { return; } photoFile = t.Result; }, null); }
private void btnTaskSub_Click(object sender, EventArgs e) {//前面提到了,当你等待一个任务的时候,同时需要等待它的子任务完成。 //下面代码演示了带子任务的Task: System.Threading.Tasks.Task <int[]> parentTask = System.Threading.Tasks.Task.Factory.StartNew(() => { int[] results = new int[3]; System.Threading.Tasks.Task t1 = new System.Threading.Tasks.Task(() => { System.Threading.Thread.Sleep(3000); results[0] = 0; }, TaskCreationOptions.AttachedToParent); System.Threading.Tasks.Task t2 = new System.Threading.Tasks.Task(() => { System.Threading.Thread.Sleep(3000); results[1] = 1; }, TaskCreationOptions.AttachedToParent); System.Threading.Tasks.Task t3 = new System.Threading.Tasks.Task(() => { System.Threading.Thread.Sleep(3000); results[2] = 2; }, TaskCreationOptions.AttachedToParent); t1.Start(); t2.Start(); t3.Start(); return(results); }); System.Threading.Tasks.Task finalTask = parentTask.ContinueWith(parent => { foreach (int result in parent.Result) { Console.WriteLine(result); } }); finalTask.Wait(); Console.ReadLine(); //这段代码的输出结果是: 1,2,3 //FinalTask会等待所有子Task结束后再执行。 }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path">Excel文件路径</param> /// <param name="objectProps">转换规则</param> /// <param name="worksheetIndex">获取工作表Index 默认获取第一张工作表内容( Index = 0 )</param> /// <param name="isContainLieDingYi">是否含有列定义行, 默认true</param> /// <param name="lieDingYi_RowIndex">首位列定义 RowIndex</param> /// <param name="lieDingYi_ColumnIndex">首位列定义 ColumnIndex</param> /// <returns></returns> public void WorkSheet2ListAsync <T>(Action <Task <List <T> > > actionHandler, string path, List <PropertyColumn> objectProps, int worksheetIndex = 0, bool isContainLieDingYi = true, int lieDingYi_RowIndex = 0, int lieDingYi_ColumnIndex = 0, bool ignoreRepeatColumnName = false ) where T : class, new() { Task <List <T> > mTask = new System.Threading.Tasks.Task <List <T> >(() => WorkSheet2List <T> ( path: path, objectProps: objectProps, worksheetIndex: worksheetIndex, isContainLieDingYi: isContainLieDingYi, lieDingYi_RowIndex: lieDingYi_RowIndex, lieDingYi_ColumnIndex: lieDingYi_ColumnIndex, ignoreRepeatColumnName: ignoreRepeatColumnName ) ); mTask.ContinueWith((task) => actionHandler(task)); mTask.Start(); #region (UI代码参考)下载完毕 Handler 可以参考以下代码 //public void DownloadFileByHttpRequestAynsc_Handler(System.Threading.Tasks.Task task) //{ // string msg = "IsCanceled={0}\nIsCompleted={1}\nIsFaulted={2};" // .FormatWith // ( // task.IsCanceled, // 因被取消而完成 // task.IsCompleted, // 成功完成 // task.IsFaulted // 因发生异常而完成 // ); // Console.WriteLine(msg); // if (task.IsFaulted == true) // { // if (task.Exception != null) // { // Console.WriteLine(task.IsFaulted); // MessageBox.Show(task.Exception.GetFullInfo()); // } // else // { // MessageBox.Show("下载失败。"); // } // } // else // { // MessageBox.Show("下载完成。"); // } //} #endregion }
public void SaveAsyncOrShowError(ProjectGroup projectGroup) { System.Threading.Tasks.Task <bool> task = SaveAsync(projectGroup); task.ContinueWith((t) => { if (!t.Result) { MessageBox.Show("Failed to save project changes on disk.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }); }
private void _Read() { System.Threading.Tasks.Task <int> task = _Peer.Receive(_ReadedByte, 0, 1); task.ContinueWith(t => _Readed(t)); }
static public void ContinueTask(System.Threading.Tasks.Task <object> task, object state) { // Will complete asynchronously. Schedule continuation to finish processing. task.ContinueWith(new Action <System.Threading.Tasks.Task <object>, object>(edgeAppCompletedOnCLRThread), state); }