我正在开发一个小工具,每天在特定的时间安排p4同步。在这个工具中,我希望在P4API运行命令时显示它的输出。
我可以看到P4API.net有一个P4Callbacks类,它有几个委托: InfoResultsDelegate、TaggedOutputDelegate、LogMessageDelegate、ErrorDelegate。
我的问题是:我如何使用这些,我在网上找不到一个这样的例子。一个简短的示例代码将是惊人的!
注意:我是一个初学者,以前从未使用过委托。
发布于 2020-04-11 01:31:28
通过一个例子回答我自己的问题。我最终自己弄明白了,这是一件简单的事情。
请注意,这只适用于P4Server。我最后一次尝试从P4Connection获取TaggedOutput是不成功的,它们在运行命令时从未被触发。
因此,下面是一个代码示例:
P4Server p4Server = new P4Server(syncPath);
p4Server.TaggedOutputReceived += P4ServerTaggedOutputEvent;
p4Server.ErrorReceived += P4ServerErrorReceived;
bool syncSuccess = false;
try
{
P4Command syncCommand = new P4Command(p4Server, "sync", true, syncPath + "\\...");
P4CommandResult rslt = syncCommand.Run();
syncSuccess=true;
//Here you can read the content of the P4CommandResult
//But it will only be accessible when the command is finished.
}
catch (P4Exception ex) //Will be caught only when the command has completely failed
{
Console.WriteLine("P4Command failed: " + ex.Message);
}和两个方法,它们将在执行sync命令时触发。
private void P4ServerErrorReceived(uint cmdId, int severity, int errorNumber, string data)
{
Console.WriteLine("P4ServerErrorReceived:" + data);
}
private void P4ServerTaggedOutputEvent(uint cmdId, int ObjId, TaggedObject Obj)
{
Console.WriteLine("P4ServerTaggedOutputEvent:" + Obj["clientFile"]);
}https://stackoverflow.com/questions/60667597
复制相似问题