我有一个outlook插件和一个桌面应用程序。我在两个应用程序中都实现了相同的同步过程。但是,我不想让用户同时从两个应用程序运行同步过程。所以,当一个同步正在运行,用户尝试从另一个应用程序启动同步。他/她会看到一条消息,说明同步已经在运行并且同步请求已中止。为了实现这一点,我正在考虑创建一个文件以及每当应用程序启动同步时。它会在该文件中创建一个条目。所以如果是然后,用户尝试从第二个应用程序开始同步,然后首先检查文件是否有条目,如果条目存在,则请求被中止。还有其他方法可以执行此操作吗?
答案 0 :(得分:2)
如果您可以控制这两个应用程序,那么您可以使用命名管道来启动它们之间的通信。 命名管道是Windows中与服务器客户端体系结构一起工作的进程间通信的最佳选择。named pipe周围有.net Wrapper,它将大大简化整个过程。
来自那里的代码。
服务器代码
var server = new NamedPipeServer<SomeClass>("MyServerPipe");
server.ClientConnected += delegate(NamedPipeConnection<SomeClass> conn)
{
Console.WriteLine("Client {0} is now connected!", conn.Id);
conn.PushMessage(new SomeClass { Text: "Welcome!" });
};
server.ClientMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)
{
Console.WriteLine("Client {0} says: {1}", conn.Id, message.Text);
};
// Start up the server asynchronously and begin listening for connections.
// This method will return immediately while the server runs in a separate background thread.
server.Start();
和客户代码
var client = new NamedPipeClient<SomeClass>("MyServerPipe");
client.ServerMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)
{
Console.WriteLine("Server says: {0}", message.Text);
};
// Start up the client asynchronously and connect to the specified server pipe.
// This method will return immediately while the client runs in a separate background thread.
client.Start();
希望它会对你有所帮助。
答案 1 :(得分:2)
你不想要IPC。 IPC将这个问题减少到两个将军的问题,因为即使使用IPC,你也会遇到竞争条件。
更有意义的是创建第三个流程,一个负责存储和同步数据的服务。我将这个服务称为数据库。
然后,outlook插件和桌面应用程序只能根据需要连接并从该数据库中获取数据。
他们还可以随时请求同步,因为他们知道数据库服务在任何时候都只会运行一次同步。
最后,有许多免费产品可以为您提供此功能,而无需您明确地编写它,例如,您可以使用CouchDB服务进行数据同步/存储。