C# Windows 服务与桌面应用程序之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18535958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Communication between Windows Service and Desktop Application
提问by Kyle G.
I know that similar questions have been asked before, but even after all my Googling I'm still completely lost.
我知道以前也有人问过类似的问题,但即使在我所有的谷歌搜索之后,我仍然完全迷失了。
I've written a small Windows Service that does what my main application usedto do in a background thread (it made sense to move it to a separate service for several reasons).
我编写了一个小型 Windows 服务,它执行我的主应用程序过去在后台线程中执行的操作(出于多种原因将其移至单独的服务是有意义的)。
When the background thread was running as a part of my application it would fire an event every time it finished working (updating a database, in this case) and I would use a timestamp from that event to grab the appropriate information to update the window. I don't think the specifics are relevant, but please let me know if you need more details.
当后台线程作为我的应用程序的一部分运行时,它会在每次完成工作时触发一个事件(在这种情况下更新数据库),我将使用该事件的时间戳来获取适当的信息来更新窗口。我认为这些细节无关紧要,但如果您需要更多详细信息,请告诉我。
Now that I've got the database-filling stuff running as a service, the question becomes: How do I replace what used to be done by the event?
既然我已经将填充数据库的东西作为服务运行,问题就变成了:如何替换过去由事件完成的工作?
In other words, what's the simplest and/or most effective way to communicate a basic DateTime from my service to my app?
换句话说,将基本 DateTime 从我的服务传达到我的应用程序的最简单和/或最有效的方法是什么?
I've never done any IPC before but started reading up on it and got thoroughly confused. Named Pipes
seem to be my best bet so far, but I've been unable to find anything that helps me understand their functionality... All I've found are poorly-commented chunks of code or "tutorials" that go waydeeper than I need, without clearly covering the basics.
我以前从未做过任何 IPC,但开始阅读它并感到非常困惑。Named Pipes
似乎是我最好的选择,到目前为止,但我一直无法找到任何可以帮助我了解它们的功能......所有我发现有不良的注释的代码块,或者去“教程”的方式更深比我更需要,没有清楚地涵盖基础知识。
So, without being able to establish a good base, I'm a little stuck. What's the best method to implement simple communication that does what my event used to do and where can I learn the basics of that method?
因此,由于无法建立良好的基础,我有点卡住了。实现简单通信的最佳方法是什么,以完成我的事件过去所做的事情,我在哪里可以学习该方法的基础知识?
EDIT:
编辑:
As always, everyone here rocks, thanks for all the quick responses. Gerrie's link turned out to be exactly what I needed (though I didn't know that's what I needed at the time).
和往常一样,这里的每个人都很震撼,感谢所有快速回复。事实证明,Gerrie 的链接正是我所需要的(尽管当时我不知道这就是我所需要的)。
采纳答案by Gerrie Schenck
Don't get scared away by the named pipes concept, WCF will take care of the nasty bits for you. You'll only have to do the configuration, it's simply another communication channel, instead of using HTTP for example. So you'll need to look into WCF.
不要被命名管道的概念吓跑,WCF 会为你处理那些令人讨厌的部分。您只需要进行配置,它只是另一个通信通道,而不是例如使用 HTTP。所以你需要研究WCF。
Check out this tutorial.
查看本教程。
回答by Ehsan
In other words, what's the simplest and/or most effective way to communicate a basic DateTime from my service to my app?
换句话说,将基本 DateTime 从我的服务传达到我的应用程序的最简单和/或最有效的方法是什么?
You should use named pipein this scenario, though the other options that you have involve MSMQ, webservices.