来自网站和'n'个用户的长期Windows服务电话 [英] long runing windows service call from website and 'n' number of users

查看:85
本文介绍了来自网站和'n'个用户的长期Windows服务电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下要求。

一个网站,任意数量的用户都可以登录并点击按钮。该按钮单击内的任务将是一个长时间运行的过程(可能需要数小时或数天才能完成任务)。所以我想在Windows服务中编写任务。因此用户可以从网站注销,但任务将在后台运行。但我怀疑在这种情况下我们如何处理'n'个用户?

我目前的解决方案: -

我创建了4-5个服务实例,它将会分配给登录该网站的前4位用户。其余用户将排队。一旦任何服务免费,它将分配给队列中的用户。但我不确定这是一个正确的方法。因为我使用了windows服务的多个实例,并在Windows服务的'Onstart'和'Onstop'事件中编写了我的所有代码。 (后台进程和管理队列任务)

我已经读过在Windows服务的'onstart'事件中保持长时间运行进程不好的做法,因为服务可能会在特定时间后停止。所以解决方案是使用线程来处理请求。我的下一个疑问如下所示。



1)如果我创建一个新线程来处理每个用户请求是否可以[仅表示一个服务实例,但创建了多个线程在该服务中处理每个用户请求]?如果是这样,我如何再次从网站为下一位用户拨打该服务?因为服务已经处于运行阶段,但是为了向用户分配新线程,我应该调用该服务并执行其onstart事件。请注意,我还将一些参数传递给Windows服务以识别任务并登录用户。



2)如果我在线程中处理此问题,则每当服务停止时,它会停止我的线程(在服务内创建)?因为我不想这样做。我需要线程运行直到它完成任务。

I have the following requirement.
A website in which any number of user can login and click on button. The task inside that button click will be a long running process (May take hours or days to finish the task). So I thought of writing the task in a windows service. So the users can log out from the website, but the task will run in background. But my doubt is how we can handle ‘n’ number of users in this case?
My current solution:-
I have created 4-5 service instances and it will allocate to the first 4 users logged into the website. Rest of the users will be in queue. Once any service is free, then it will allocate to the users who are in queue. But I am not sure this is a correct approach. Because I have used multiple instance of the windows service and written all my code in ‘Onstart’ and ‘Onstop’ event of windows service. (The background process and managing queue task)
I have read like it is not good practice to keep long running process in ‘onstart’ event of windows service as the service may stop after a particular amount of time. So the solution is to use threads to handle the request. My next doubt is shown below.

1) Is it okay if I create a new thread for handling each user request [Means only one service instance, but multiple threads created in that service to handle each user request]? If so how can I call the service again for next user from the website? Because the service is already in running stage, but to allocate a new thread to the user, I should call the service and execute its onstart event. Note that I am also passing some argument to the windows service to identify the task and logged in user.

2) If I handle this in thread, whenever the service stops, will it stop my thread as well (created inside service)? Because I don’t want to do that. I need the thread to be running until it finishes the task.

推荐答案

1)我不建议每个用户使用一个线程。它可能会创建不受控制的线程数。当线程数量固定时(例如,取决于某些配置),服务的效果最佳。您始终可以在队列中为用户请求提供服务。一个重要的提示:你总是需要一个单独的线程来监听新的连接,所以线程写入/从网络流中读取的内容将与之分开。



2)不是一个问题。停止服务意味着终止进程。重新启动服务时,这是该进程的新实例。让它一直运行。



-SA
1) I would not recommend a thread per user. It may create uncontrolled number of thread. Services work the best when number of threads is fixed (say, depends on some configuration). You can always server user requests in a queue. An important hint: you always need a separate thread listening for new connections, so the thread writing/reading to/from network stream would be separate from that.

2) Not really a question. Stopping the service means end of the process lifetime. When you restart the service, this is a new instance of the process. Keep it running at all times.

—SA


这篇关于来自网站和'n'个用户的长期Windows服务电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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