我正在审查 API 示例应用程序中的一些代码,需要一些帮助来更好地理解Action<T>整个示例应用程序中的委托。我在整个代码中提出了几个问题。任何帮助表示赞赏
API 在Client.cs类中实现,当我从应用程序发出请求时,API 会将响应发送到Client.cs已实现的函数。
/***** Client.cs *****/
public event Action<int, int, int> TickSize;
void tickSize(int tickerId, int field, int size)
{
var tmp = TickSize;
//What is the point of tmp, and why do we check if it is not null?
if (tmp != null)
//This invokes the Action? ie - fires the TickSize Action event?
tmp(tickerId, field, size);
}
然后UI.cs该类处理 UI 交互并将信息反馈回 UI,以便用户可以查看返回的数据
/***** UI.cs *****/
//Delegate to handle reading messages
delegate void MessageHandlerDelegate(Message message);
protected Client client;
public appDialog(){
InitializeComponent();
client = new Client();
.
.
//Subscribes the client_TickSize method to the client.TickSize action?
client.TickSize += client_TickSize;
}
void client_TickSize(int tickerId, int field, int size){
HandleMessage(new Message(ticketID, field, size));
}
public void HandleMessage(Message message){
//So, this.InvokeRequired checks if there is another thread accessing the method?
//Unclear as to what this does and its purpose
//What is the purpose of the MessageHandlerDelegate callback
// - some clarification on what is going on here would be helpful
if (this.InvokeRequired)
{
MessageHandlerDelegate callback = new MessageHandlerDelegate(HandleMessage);
this.Invoke(callback, new object[] { message });
}
else
{
UpdateUI(message);
}
}
private void UpdateUI(Message message) { handle messages }
波斯汪
相关分类