Windows Service与Windows Form应用程序 [英] Windows Service vs Windows Form application

查看:104
本文介绍了Windows Service与Windows Form应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个C#Windows窗体应用程序,它似乎消耗了更多的内存.我试图使其效率更高,但我想出了一个更好的主意是在Windows服务应用程序中实现该功能,这样我就可以使该应用程序自动暂停和恢复,从而节省一些处理内存的过程. Windows Service将成为此解决方案的有效方法吗?

另一个问题:
是否可以在服务内部创建一个线程,即使该服务被暂停或停止,该线程也将处于活动状态?我的要求是,我希望线程在计算机打开时始终检查Internet连接,如果建立了连接,则必须启动或继续该服务,以及在其他
位置 条件应该暂停或停止服务.

是否有可能在背景中运行一个简单的线程来检查Internet连接,如果建立了连接,我希望启动该服务.如何在Visual Studio中创建线程过程?

请帮我.在此先感谢.

I have developed an C# windows form application and it seems to consume more memory. I tried making it more efficient and I got an idea if it is a better idea to implement the functionality in a windows service application rather, so that I can make the application to pause and resume automatically so that I can save some processessing memory. Will windows Service be an efficient approach to this solution?

Another question:
Is it possible to create a thread inside the service which will be active even if the service is paused or stopped? My requirement is that I wanted the thread to check for the internet connectivity at all times when the computer is ON and if the connection is established the service must be started or continued, and at other
conditions the service should be paused or stopped.

Is it possible to just have a simple thread running in the backgroud which will check for internet connectivity and if the connection is established I want the service to be started. How to create a thread process in visual studio?

Pls help me out guys. Thanks in advance.

推荐答案

第一个问题:性能不会有实际差异.业务逻辑将是相同的.仅当GUI正在消耗您的资源时,您才会注意到显着的差异,但是您会遇到其他类型的问题,而且我无法想象GUI密集型应用程序将作为服务运行.

第二个问题:暂停服务本身无法执行任何操作.您对服务中的线程负责.您必须暂停或停止线程.如果您不必一直运行线程,则可以使用 Thread.Sleep [ ^ ],创建您的线程睡一会儿.因此,暂停/停止并重新启动服务不是一个好主意,这是对资源的浪费.您甚至可以根据需要使用IPC方法或线程同步方法来使解决方案的各个部分协同工作.

第三个问题:您不能在某个地方使用弯针线.线程属于进程.不要将您的服务视为单线程应用程序.服务中可以有很多线程:例如连接监视程序和工作线程.您可以使用观察者线程中的线程控制和同步方法来暂停/恢复或重新启动工作线程,而不是整个服务.
First question: There will be no actual difference in performance. The business logic will be the same. You will be noticing considerable difference only if the GUI is consuming your resources, but than you have an other kind of problem, and I can not imagine a gui intensive application running as service.

Second question: pausing a service does nothing by itself. You are responsible of your threads in the service. You have to pause or stop your threads. Buy you don''t have to run your thread all the time, you can use Thread.Sleep[^], to make your thread sleep a while. So pausing/stoping and restarting a service is not a good idea, it is a waste of resources. You can even use IPC methods or thread synchronization methods to have separate parts of your solution work together as needed.

Third question: You can not have a limbo thread somewhere. A thread belongs to a process. Think of your service not as a single threaded application. You can have many threads in a service: like a connection watcher and working thread. You can use thread control and synchronization methods from the watcher thread to pause/resume or restart you worker thread(s) rather than your whole service.


这篇关于Windows Service与Windows Form应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆