基本信息
源码名称:C# 仿 android asynctask 类 实例源码下载
源码大小:0.02M
文件格式:.zip
开发语言:Java
更新时间:2015-02-10
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System; using System.Collections.Generic; using System.Text; using System.ComponentModel.Custom.Generic; namespace AsyncTaskCallback { /* Quote From Android API : " AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. " */ /// <summary> /// /// </summary> /// <typeparam name="Tin"></typeparam> /// <typeparam name="Tout"></typeparam> public class AsyncTask<Tin, Tout> { private BackgroundWorker<Tin, int, Tout> bw;//cannot pass void :( public delegate bool PreFunc(); public delegate Tout DoFunc(Tin input); public delegate void PostFunc(Tout output, Exception err); public PreFunc Pre { get; set; } public DoFunc Do { get; set; } public PostFunc Post { get; set; } public AsyncTask() { this.Pre = delegate { return true; }; this.bw = new BackgroundWorker<Tin, int, Tout>(); this.bw.DoWork = new EventHandler<DoWorkEventArgs<Tin, Tout>>(OnDoWork); this.bw.RunWorkerCompleted = new EventHandler<RunWorkerCompletedEventArgs<Tout>>(OnRunWorkerCompleted); } public void Execute(Tin input) { if (Pre()) { this.bw.RunWorkerAsync(input); } } private void OnDoWork(object sender, DoWorkEventArgs<Tin, Tout> e) { Tout r = Do(e.Argument); e.Result = r; } private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs<Tout> e) { Post(e.Result, e.Error); } } /// <summary> /// /// </summary> /// <typeparam name="Tin"></typeparam> /// <typeparam name="Tgo"></typeparam> /// <typeparam name="Tout"></typeparam> public class AsyncTask<Tin, Tgo, Tout> { public class Progress { private AsyncTask<Tin, Tgo, Tout> asyncTask; public Progress(AsyncTask<Tin, Tgo, Tout> at) { this.asyncTask = at; } public void Report(int percent, Tgo progress) { this.asyncTask.bw.ReportProgress(percent, progress); } } private BackgroundWorker<Tin, Tgo, Tout> bw; public delegate bool PreFunc(); public delegate Tout DoFunc(Tin input, Progress p); public delegate void GoFunc(Tgo progress); public delegate void PostFunc(Tout output, Exception e); public PreFunc Pre { get; set; } public DoFunc Do { get; set; } public GoFunc Go { get; set; } public PostFunc Post { get; set; } public AsyncTask() { this.Pre = delegate { return true; }; this.bw = new BackgroundWorker<Tin, Tgo, Tout>(); this.bw.DoWork = new EventHandler<DoWorkEventArgs<Tin, Tout>>(OnDoWork); this.bw.ProgressChanged = new EventHandler<ProgressChangedEventArgs<Tgo>>(OnProgressChanged); this.bw.RunWorkerCompleted = new EventHandler<RunWorkerCompletedEventArgs<Tout>>(OnRunWorkerCompleted); } public void Execute(Tin input) { if (Pre()) { this.bw.RunWorkerAsync(input); } } private void OnDoWork(object sender, DoWorkEventArgs<Tin, Tout> e) { e.Result = Do(e.Argument, new Progress(this)); } private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs<Tout> e) { Post(e.Result, e.Error); } private void OnProgressChanged(object sender, ProgressChangedEventArgs<Tgo> e) { Go(e.UserState); } } }