此文主要是通过具体例子简述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¼Ó ";
backworker.RunWorkerAsync(_entity);
MessageBox.Show("Start");~~
}
public void Method1(objectsender,DoWorkEventArgse)
{
ManualTask.RLDMSManualSendTaskServicetaskService = newManualTask.RLDMSManualSendTaskService();
List<ManualTask.DriverEntity> driver = newList<ManualTask.DriverEntity>();
Entity_entity = newEntity();
_entity = e.Argument asEntity;
driver = taskService.GetDriver(_entity .OperatorID ,_entity .OperatorName ,_entity .Block ,_entity .Floor ).ToList();
e.Result = driver;
}
//§¹¦¨¨Æ¥ó~
void back_RunWorkerCompleted(objectsender, RunWorkerCompletedEventArgs e)
{
if(e.Error != null)
throwe.Error;
List<ManualTask.DriverEntity> driver = newList<ManualTask.DriverEntity>();
driver = e.Result as List<ManualTask.DriverEntity>;
MessageBox.Show("OK");~~
}
//4. 使用web service的异步方法
public void CallWebService()
{
ManualTask.RLDMSManualSendTaskServicetaskService = newManualTask.RLDMSManualSendTaskService();
Entity_entity = newEntity();
_entity.OperatorID = 0;
_entity.OperatorName = "";
_entity.Block = "4t ";
_entity.Floor = "1f";
taskService.GetDriverAsync(_entity.OperatorID, _entity.OperatorName, _entity.Block, _entity.Floor);
taskService.GetDriverCompleted += newTestAPP.ManualTask.GetDriverCompletedEventHandler(GetDriverCompleted);
//driver = taskService.GetDriver(_entity.OperatorID, _entity.OperatorName, _entity.Block, _entity.Floor).ToList();
MessageBox.Show("Start");~
}
//§¹¦¨¨Æ¥ó‰]²z¤èªk~
void GetDriverCompleted(objectsender, ManualTask .GetDriverCompletedEventArgse)
{
if(e.Error != null)
throwe.Error;
List<ManualTask.DriverEntity> driver = newList<ManualTask.DriverEntity>();
driver = e.Result.ToList();
MessageBox.Show("Finish");~~~//显¥ÜŸÏªA‰U¾¹获¨úªº结ªG È~
}~
}
}