几种异步方法编程举例
此文主要是通过具体例子简述AsyncCallback ,BackgroundWorker ,web service的异步方法编程
//1. Handheld Program 使用AsyncCallback
//2. winform Program 使用AsyncCallback
//3. 使用BackgroundWorker
//4. 使用web service的异步方法
以下是具体的例子
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Xml;
usingSystem.Xml.Linq;
namespaceTestAPP
{
publicclassEntity
{
publicintOperatorID {get; set; }
publicstringOperatorName {get; set; }
publicstringBlock {get; set; }
publicstringFloor {get; set; }
}
public delegate Object Asyncdelegate();
public class CallBack
{
//1. Handheld Program 使用AsyncCallback
public delegate void taskstatusDel(objectStatusList);
private void BindcboStatus(objectStatusList)
{
List<ManualTask.TaskStatus> _taskstatus = newList<ManualTask.TaskStatus>();
_taskstatus = StatusList asList<ManualTask.TaskStatus>;
ManualTask.TaskStatus_statusObj = newManualTask.TaskStatus();
_statusObj.StatusCode = "";
_statusObj.StatusDesc = "";
_taskstatus.Insert(0, _statusObj);
ComboBoxcboStatus = newComboBox();
cboStatus.DataSource = _taskstatus;
cboStatus.DisplayMember = "StatusDesc";
cboStatus.ValueMember = "StatusCode";
cboStatus.SelectedIndex = -1;
}
private void CallbackHandheld(IAsyncResultar)
{
List<ManualTask.TaskStatus> _taskstatus = newList<ManualTask.TaskStatus>();
_taskstatus = _taskService.EndGetTaskStatus(ar).ToList();
this.BeginInvoke(newtaskstatusDel(BindcboStatus), _taskstatus);
}
private void BindTaskStatus(stringstatusCode)
{
ManualTask.RLDMSManualSendTaskServicetaskService = newManualTask.RLDMSManualSendTaskService();
taskService.BeginGetTaskStatus(statusCode, new AsyncCallback(CallbackHandheld), this);
}
//2. winform Program 使用AsyncCallback
Object GetDriverList()
{
ManualTask.RLDMSManualSendTaskServicetaskService = newManualTask.RLDMSManualSendTaskService();
List<ManualTask.DriverEntity> driver = newList<ManualTask.DriverEntity>();
Entity_entity = newEntity();
_entity.OperatorID = 0;
_entity.OperatorName = "";
_entity.Block = "4´Á ";
_entity.Floor = "1¼Ó ";
driver = taskService.GetDriver(_entity.OperatorID, _entity.OperatorName, _entity.Block, _entity.Floor).ToList();
return driver;
}
private void CallbackMethod(IAsyncResultar)
{
// Retrieve the delegate.
Asyncdelegate dlgt = (Asyncdelegate)ar.AsyncState;
// Call EndInvoke to retrieve the results.
List<ManualTask.DriverEntity> driver = newList<ManualTask.DriverEntity>();
driver=dlgt.EndInvoke(ar) asList<ManualTask.DriverEntity> ;
MessageBox.Show("End");~~
}
public void MainCallBack()
{
Asyncdelegateisgt = newAsyncdelegate(GetDriverList);
AsyncCallbackacb = new AsyncCallback(CallbackMethod);
IAsyncResultar = isgt.BeginInvoke(acb, isgt);
MessageBox.Show("Start");~~
}
//3. 使用BackgroundWorker
publicvoidMainMethod()
{
BackgroundWorkerbackworker = newBackgroundWorker();
backworker.DoWork += newDoWorkEventHandler(Method1);
backworker.RunWorkerCompleted += newRunWorkerCompletedEventHandler(back_RunWorkerCompleted);
Entity_entity = newEntity();
_entity.OperatorID = 0;
_entity.OperatorName = "";
_entity.Block = "4´Á ";
_entity.Floor = "1¼Ó ";
&
补充:综合编程 , 其他综合 ,