关闭,如果没有活动线程,或者如果有任何活动,然后等待,直到它的完成并关闭 [英] Close if no active threads, or if any active, then wait till it's complete and close

查看:202
本文介绍了关闭,如果没有活动线程,或者如果有任何活动,然后等待,直到它的完成并关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序概述

ASP.Net Web服务招待来自通过客户端数字签名和验证各种应用程序的请求。 WebService的然后将这些路由请求到智能卡

当系统日期的变化,我想下面的情况发生。


  1. 从客户新的要求都是为了等待


  2. Web服务和智能卡之间目前的工作应该得到完成


  3. 如果有任何前挂起的请求,那么他们应该得到完成。


为什么我需要上面的事情发生的原因是,我需要关闭智能卡和web服务之间的现有会话。只有当文件没有签名/验证这应该发生。我不能只是关闭所有的对话,因为它可能会影响由线程中的任何一个被处理的文件。所以,我需要确保有互联网服务和智能卡之间没有当前活动的线程。

我写了一张code的这使得web服务和智能卡之间的活动线程的总数。

  INT vWorkerThreads,vWorkerThreadsMax;
诠释vPortThreads,vPortThreadsMax;
系统:线程::线程池^ vThreadPool;
vThreadPool-> GetAvailableThreads(vWorkerThreads,vPortThreads);
vThreadPool-> GetMaxThreads(vWorkerThreadsMax,vPortThreadsMax);
ActiveThreadCount = vWorkerThreadsMax - vWorkerThreads;

这意味着,我还需要在客户端请求的等待?

清理机制:关闭PKCS#11使用API​​ C_CloseAllSessions 使用C_Finalize 通话将释放该库,以便它清除所有的会话对象。这应该是每天做一次。

我如何能完成这样的任务?任何想法

更新:

我可以在我的查询已经更加清晰。我想清楚地表明,我的目的不是要关闭ASP.NET web服务。 我的目的是要重置智能卡。由于我通过访问Web服务ASP.NET智能卡,我需要一个机制来进行重置智能卡的这个任务。

我给下面

当前进程

  

      
  1. 客户端检测日期的变化,在午夜

  2.   
  3. 客户端调用函数 WebService_Close_SmartCard

  4.   
  5. Web服务接收到请求 WebService_Close_SmartCard 并依次
      电话 PKCS11_Close_SmartCard 。这个
      呼叫将通过之一送达
      从线程可用线程
      池。 PKCS11_Close_SmartCard会
      关闭所有当前存在的
      会议与智能卡。

  6.   
  7. 在这一点上,我要确保没有使用线程
      函数调用,如
       PKCS11_DigitalSign_SmartCard /
       PKCS11_DigitalVerify_SmartCard 交谈
      以智能卡,如
       PKCS11_Close_SmartCard 将突然
      结束其他正在进行的会话。

  8.   

PS:我是新来ASP.NET和多线程


解决方案

现在的问题是在一个大的方式进行更新,如此忍受我...

由于没有线程被你的Web方法code直接创建\\间接的:


  

Quesiton 所以你没有明确创建任何新的线程或使用线程池线程直接\\间接的,你只是接收到您的Web方法调用和执行你的code同步?


  
  

是,你是正确的。有一个客户端API调用该web服务。然后,Web服务将自动管理线程(作者:丛松/中分配等)inresponse通过加密/解密打开多个会话客户端的web服务demands.The会谈智能卡。


这是更有益的沿着请求,而不是线程,例如。

的线重新表述了原来的问题

  

当系统日期修改我要重新开始我的ASP.NET应用程序,并确保当前正在执行的所有的要求完成,任何优秀的\\排队的请求完成为好。


,因为这里提供请求队列和活动请求的概念这被自动处理。当你的ASP.NET应用程序重新启动,所有当前和排队的完成的请求(除非他们没有及时完成),以及新的请求进行排队,然后提供服务,当一个新的工作进程回来了。当你回收你的ASP.NET应用程序所属的应用程序池此过程之后。

您可以配置应用程序池时,透过为相关应用程序池的回收的设置在IIS管理器中设定的时间回收。 presumably你想在00:00要做到这一点。

更新

我想我可以从你的意见,你需要运行一些清理code时,所有的请求已被提供服务,然后应用程序即将关闭收集。你应该把这个code,在全球 Application_End 事件处理程序。

更新2

在回答你的问题的更新。您的要求是:

当重新启动应用程序:


  1. 从客户的新要求都是为了等待

  2. Web服务和智能卡之间目前的工作应该得到完成

  3. 如果有任何前挂起的请求,那么他们应该得到完成。

这是我所描述的标准回收模式的支持。你并不需要处理的请求线程自己 - 这是ASP.NET框架的支柱之一,它与此交易为您服务。它是面向要求和摘要如何请求的处理,即服务于多个线程。它管理把请求到线程并且当该应用程序被回收管理这些请求的lifeclyle。

更新3

OK,我想我们有场景的最后一块在这里。您正在试图通过发出封闭Web服务调用从客户端关闭ASP.NET。基本上
你想实现自己的ASP.NET通过确保所有当前和排队的请求被处理过,然后你执行你清理code关机的行为。

您正试图重新发明轮子。

ASP.NET已经具有这种行为,它是由支持:

一个。 应用回收它将干净出色的服务请求,并启动了一个新的流程来服务新的请求。它甚至会排队,虽然这个过程是怎么回事收到任何新的请求。

乙。 Application_End 全局应用程序事件处理程序,在那里你可以把你清理code。它将执行的之后的回收利用已处理干净你未完成的请求。

您不需要你的关闭命令。

您应该考虑让IIS回收您的应用程序,因为它具有在每天指定时间(s)回收支持。如果您无法配置IIS由于部署原因,那么你可以使用的web.config接触强制回收超出范围的IIS:

一个。已在服务器上运行的定时器,可检查日期的改变condtion,然后触摸web.config文件。

乙。还有在客户端调用一个封闭网络的方法,但有封闭的方法只需触摸web.config文件。

IIS,那么一是最不可取的。

老实说微软已经想过这个问题。 :)

更新4

@Raj OK,让我尝试,并再次重新表述这一点。

您的条件是:


  1. 您有一个要求,一旦重置您的智能卡的日子。

  2. 重置智能卡,目前所有的前后排队Web服务请求必须完成即完成的请求。

  3. 优秀的请求完成后,重置您的智能卡。

  4. 的进来,而这个过程的任何新的要求正在发生的事情应该排队,然后智能卡服务后,已经复位。

这些条件,您可以完成现有请求,排队任何新的请求,重置您的智能卡,然后将卡已被复位后开始处理新的请求。

我的建议是:


  1. 将在Application_End你的智能卡复位code。

  2. 配置IIS在00:00回收您的应用程序。确保在相关的应用程序池所配置的禁用重叠循环=真高级设置。

  3. 在00:00申请回收,确保所有当前和排队的请求将完成。

  4. 00:00的应用程序后循环利用,确保所有新的请求都将同时请求3完成在排队,并在应用程序执行关机步骤。

  5. 在3的请求完成后,Applicaton_End将被自动调用。这保证了所有当前请求完成后您的智能卡复位。

  6. 申请回收,确保您的应用程序在新的进程重新启动,并有新的申请步骤排队4开始进行处理。这里最重要的是,你的复位code已被称为5。

除非有一些细节从你的问题缺少的,上面显示,以满足您的条件。你愿意做X,Y,Z和ASP.NET内置了可用于实现X,Y,Z和给你的成熟,保证良好的文档实现支持。

我仍然在努力理解为什么你所谈论的主题。我做的多线程开发,而是想着ASP.NET在谈到线程,而不是要求这个讨论增加了不必要的复杂性。除非你的问题,目前尚不得而知。

也许你错过了,我在这里做点。我画你所需要的行为之间的平行当您从您的客户端应用程序调用封闭,当你回收应用程序时会发生什么。可以使用回收和Application_End来实现所需的结果。

我想帮助你在这里,因为想实现此行为自己是不必要的,不平凡的。

My application overview is

ASP.Net webservice entertains requests from various applications for digital signing and verification via a client. The webservice will then route these requests to a smart card

When the system date changes, I want the following to happen.

  1. New request from the clients are made to wait

  2. Current work between webservice and smart card should get completed

  3. If there is any prior pending requests then they should get completed.

The reason why I need the above things to happen is, I need to close the existing sessions between the smartcard and webservice. This should happen only when there is no signing/verification of files. I cannot just close all the sessions as it might affect a file being processed by any one of the threads. So I need to make sure that there are no current active threads between webservice and smart card.

I wrote a piece of code which gives the total number of active threads between webservice and smartcard.

int vWorkerThreads,vWorkerThreadsMax;
int vPortThreads,vPortThreadsMax;
System::Threading::ThreadPool ^ vThreadPool;
vThreadPool->GetAvailableThreads(vWorkerThreads, vPortThreads);
vThreadPool->GetMaxThreads(vWorkerThreadsMax, vPortThreadsMax);
ActiveThreadCount = vWorkerThreadsMax - vWorkerThreads;

This means, I also need to make the client requests wait?

CLEANUP MECHANISM: Close the PKCS#11 API using C_CloseAllSessions and C_Finalize call which will free up the library so that it cleans all the session objects. This should be done once everyday.

Any ideas on how I can perform such a task?

UPDATE:

I could have been much more clearer in my query. I want to make it clear that my aim is not to shutdown the ASP.NET webservice. My aim is to reset the smartcard. As I am accessing the smartcard via ASP.NET webservice, I need a mechanism to perform this task of resetting the smart card.

I am giving the current process below

  1. Client detects Date change, At midnight
  2. Client calls the function WebService_Close_SmartCard
  3. Web Service receives the request WebService_Close_SmartCard and in turn calls PKCS11_Close_SmartCard. This Call will be served via one of the available threads from the Thread Pool. PKCS11_Close_SmartCard will close all the existing current sessions with the smartcard.
  4. At this point, I want to make sure that there are no threads with function calls such as PKCS11_DigitalSign_SmartCard/ PKCS11_DigitalVerify_SmartCard talking to smartcard, as PKCS11_Close_SmartCard will abruptly end the other ongoing sessions.

PS: I am new to ASP.NET and Multithreading.

解决方案

The question was updated in a big way, so bear with me...

Given that no threads are being created directly\indirectly by your web method code:

Quesiton So you are not explicitly creating any new threads or using ThreadPool threads directly\indirectly, you are simply receiving calls to your web method and executing your code synchronously?

Answer Yes, you are correct. There is a client API which calls the webservice. Then the webservice manages the threads automatically(creats/allocates etc) inresponse to the client's demands.The webservice talks to a smart card by opening multiple sessions for encryption/decryption.

It is more helpful to rephrase the original question along the lines of "requests" rather than threads, e.g.

When the system date changes I want to re-start my ASP.NET application and ensure that all requests that are currently executing are completed, and that any outstanding\queued requests are completed as well.

This is handled automatically as there is a concept of a request queue and active requests. When your ASP.NET application is restarted, all current and queued requests are completed (unless they do not complete in a timely fashion), and new requests are queued and then serviced when a new worker process comes back up. This process is followed when you recycle the Application Pool that your ASP.NET application belongs to.

You can configure your application pool to recycle at a set time in IIS Manager via the "Recycle" settings for the associated Application Pool. Presumably you want to do this at "00:00".

Update

I think I can glean from your comments that you need to run some cleanup code when all requests have been serviced and then the application is about to shut down. You should place this code in the global "Application_End" event handler.

Update 2

In answer to your updated question. Your requirements are:

When the application is restarted:

  1. New request from the clients are made to wait
  2. Current work between webservice and smart card should get completed
  3. If there is any prior pending requests then they should get completed.

This is supported by the standard recycling pattern that I have described. You do not need to deal with request threads yourself - this is one of the pillars of the ASP.NET framework, it deals with this for you. It is request orientated and abstracts how requests are handled i.e. serviced on multiple threads. It manages putting requests onto threads and manages the lifeclyle of those requests when the application is recycled.

Update 3

OK, I think we have the final piece of the scenario here. You are trying to shut down ASP.NET from your client by issuing a "CLOSED" web service call. Basically you want to implement your own ASP.NET shut down behaviour by making sure that all current and queued request are dealt with before you then execute your clean-up code.

You are trying to re-invent the wheel.

ASP.NET already has this behaviour and it is supported by:

a. Application Recycling It will service outstanding requests cleanly and start-up a new process to serve new requests. It will even queue any new requests that are received whilst this process is going on.

b. Application_End A global application event handler where you can put your clean-up code. It will execute after recycling has cleanly dealt with your outstanding requests.

You do not need your "CLOSED" command.

You should consider letting IIS recycle your application as it has support for recycling at a specified daily time(s). If you cannot configure IIS due to deployment reasons then you can you use web.config "touching" to force a recycle out-of-bounds of IIS:

a. Have a timer running in the server which can check for the date change condtion and then touch the web.config file.

b. Still have the client call a "CLOSED" web method, but have the "CLOSED" method just touch the web.config file.

IIS, then "a" are the most desirable.

Honestly Microsoft have already thought about it. :)

Update 4

@Raj OK, let me try and rephrase this again.

Your conditions are:

  1. You have a requirement to reset your smartcard once a day.
  2. Before resetting your smartcard, all current and queued web service requests must be completed i.e. the outstanding requests.
  3. After outstanding requests are completed, you reset your smartcard.
  4. Any new requests that come in whilst this process is happening should be queued and then serviced after the smartcard has been reset.

These conditions allow you to complete existing requests, queue any new requests, reset your smartcard, and then start processing new requests after the card has been reset.

What I am suggesting is:

  1. Place your smartcard reset code in "Application_End".
  2. Configure IIS to recycle your application at "00:00". Ensure that in advanced settings for the associated Application Pool that you configure "Disable Overlapped Recycle = True".
  3. At "00:00" application recycling ensures that all current and queued requests will be completed.
  4. After "00:00" application recycling ensures that all new requests will be queued whilst requests in "3" are completed and the application performs shutdown steps.
  5. After requests in "3" are completed, "Applicaton_End" will be called automatically. This ensures that your smartcard is reset after all current requests are completed.
  6. Application recycling ensures that your application is re-started in a new process, and that new requests queued in step "4" start to be processed. The important thing here is that your reset code has been called in "5".

Unless there is some detail missing from your question, the above appears to meet your conditions. You wish to do "x,y,z" and ASP.NET has built-in support which can be used to achieve "x,y,z" and gives you mature, guaranteed and well-documented implementations.

I am still struggling to understand why you are talking about threads. I do multi-threaded development, but talking about threads instead of requests when thinking about ASP.NET adds unnecessary complexity to this discussion. Unless your question is still unclear.

Perhaps you are missing the point I'm making here. I am drawing a parallel between the behaviour you require when you call "CLOSED" from your client application, and what happens when you recycle an application. You can use recycling and "Application_End" to achieve the required results.

I am trying to help you out here, as trying to implement this behaviour yourself is unnecessary and non-trivial.

这篇关于关闭,如果没有活动线程,或者如果有任何活动,然后等待,直到它的完成并关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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