这可能是一个非常愚蠢的问题,特别是来自在.Net工作了几年的人。这个问题很简单:在单一方法调用中包装方法和事件处理程序
有没有办法来包装方法调用+的事件处理程序中,根据我的理解一个方法调用
创建从方法 ,它不是。这里有一个例子:
// Starts the long running transaction, when this method completes
// we have no idea whether the transaction actually succeeded/failed
// /finished/etc
public bool BeginLongRunningTransaction(/*args*/)
{
myService.LongRunningTransactionComplete += TransactionComplete;
// Runs the actual transaction - this can take some time
myService.ExecuteLongRunningTransaction();
return true;
}
// Handles the long running transaction complete event
// which allows us to see if the transaction suceeded/failed/etc
private void TransactionComplete(/*args*/)
{
/* Stuff complete! */
}
会发生什么事,是一个呼叫者将调用BeginLongRunningTransaction()方法,将开始长期运行的事务,但将无法返回该交易的结果,因为这结果将在TransactionComplete()事件处理程序中返回。什么我想看到的是是否有一种方式来开始和返回BeginLongRunningTransaction()方法中的长期运行的事务的结果,所以对主叫,
我知道同步等待模式并且还知道内联事件处理程序。根据我的理解,这些都不能达到我想要的。
这个问题的主要原因是从订阅客户的角度简化了通信。
非常感谢!
什么是为myService?它是一个Web服务?你使用的是什么版本的Visual Studio? – NineBerry
_“对DoStuff()的请求和实际的DoStuff()功能将在不同的线程上完成”_ - 说谁?在你的问题中根本没有任何建议涉及多个线程。在C#中使用'event'肯定不是隐含的,也不是通常的方法调用。你的问题太模糊不清。修正你的问题,使其包含一个很好的[mcve],清楚地显示你已经尝试了什么,并且解释_precisely_代码的作用以及你希望它做什么。 –
道歉不清楚。我尽量保持示例尽可能简单。我已经用更好的命名+评论更新了这个问题,希望更清楚地说明问题。 – Ross