我正在修改一个用C#编写的旧的、大的(并且没有文档记录的)程序,它使用API在串行总线上通信。
有没有什么方法可以让OnIndication触发SendRepeatRequest继续?我希望避免使用等待Xms轮询标志,因为响应时间差异很大,我需要快速响应。
//Pseudocode
public void SendRepeatRequest(int X)
{
SendToAPI();
// Wait until API responds, usually a few ms but can take 1-2min
// loop X times
}
//this is activated by the API response
public void OnIndication()
{
// Handle request from API...
// Tell SendRepeatRequest to continue
}
你对如何做到这一点有什么建议吗?谢谢!
发布于 2014-02-11 23:47:39
您可能需要查看任务库(在System.Threading.Tasks名称空间下的.NET 4.0中引入)。它有各种各样的线程操作来很容易地做到这一点。
我相信下面的部分可能会对你有所帮助(或者让你入门)。
public void OnIndication()
{
Task doWork = new Task(() =>
{
// Handle request
});
Action<Task> onComplete = (task) =>
{
SendRepeatRequest(X, args)
};
doWork.Start();
doWork.ContinueWith(onComplete, TaskScheduler.FromCurrentSynchronizationContext());
}
https://stackoverflow.com/questions/21706304
复制相似问题