0

我正在开发一个小工具来每天在特定时间安排 p4 同步。在这个工具中,我想在 P4API 运行命令时显示它的输出。

我可以看到 P4API.net 有一个 P4Callbacks 类,有几个委托:InfoResultsDelegate、TaggedOutputDelegate、LogMessageDelegate、ErrorDelegate。

我的问题是:我如何使用这些,我在网上找不到一个例子。一个简短的示例代码会很棒!

注意:我是一个初学者,以前从未使用过委托。

4

1 回答 1

0

通过一个例子回答我自己的问题。我最终自己弄清楚了,这是一个简单的事件。

请注意,这只适用于 P4Server。我上次从 P4.Connection 获取 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);
}

这两种方法将在同步命令执行时触发。

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"]);
}
于 2020-04-10T17:31:27.843 回答