我有一个方法,如下所示,它调用服务.
如何通过线程运行此方法?
public List<AccessDetails> GetAccessListOfMirror(string mirrorId,string server) { List<AccessDetails> accessOfMirror = new List<AccessDetails>(); string loginUserId = SessionManager.Session.Current.LoggedInUserName; string userPassword = SessionManager.Session.Current.Password; using (Service1Client client = new Service1Client()) { client.Open(); accessOfMirror = client.GetMirrorList1(mirrorId,server,null); } return accessOfMirror; }
解决方法
在C#3.5或4.0中,您可以执行此操作.
var task = Task.Factory.StartNew<List<AccessDetails>>(() => GetAccessListOfMirror(mirrorId,server)) .ContinueWith(tsk => ProcessResult(tsk)); private void ProcessResult(Task task) { var result = task.Result; }
在C#4.5中有await / async关键字,这是上面的一些糖
public async Task<List<AccessDetails>> GetAccessListOfMirror(string mirrorId,string server) var myResult = await GetAccessListOfMirror(mirrorId,server)