asp.net中的锁定机制 [英] locking mechanism in asp.net

查看:122
本文介绍了asp.net中的锁定机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是开发新手。我已经实现了一个Web应用程序。在这里,客户可以通过单击一个按钮来运行.exe。

我们拥有大量用户,因此我们需要避免并行访问或运行exe。请帮我实现一个锁定机制,在同步过程中运行我的.exe文件(保留Q用户请求)

Hi everyone,

I am new to development. I have implemented one web application. Here customer can run the .exe from clicking the one button.
We have huge number of user so we need to avoid the parallel accessing or running of exe . Please help me to implemented a locking mechanism to running the my .exe file in synchronize process (keeping in Q user requests)

推荐答案

设置一个数据库表行和2个字段,ID(tinyint会这样做)作为主键,Lock作为可以为空的varchar。

插入ID为1的行并保留Lock null



在您的代码中,在try / catch块中,在循环中,编写更新查询,使用该请求的一条唯一信息更新Lock(注意,如果是,则可以锁定一个数字唯一信息是数字)其中ID = 1且锁定为空

现在选择行并且如果Lock匹配您唯一信息退出循环

在循环中,在测试之后,睡眠一小段时间(比如10毫秒)以避免阻塞

循环后执行你的EXE

在try / catch块的最后发出更新查询将lock设置为NULL,其中ID = 1且Lock =唯一的信息离开



这种方法唯一的危险就是某个过程会在没有清除锁定的情况下退出。要解决此问题,您可以在数据库上添加时间戳并运行预定作业,以便在该时间戳超时后清除锁定。如果您没有使用DBMS服务器,请测试循环中的时间戳并清除它是否已过期,但您仍需要进行测试以确保此过程是持有锁的过程。
Set up a database table with a single row and 2 fields, ID (tinyint will do) as primary key and Lock as, say, a nullable varchar.
Insert one row with ID of 1 and leave Lock null

In your code, within a try/catch block, in a loop, write an update query, updating Lock with a piece of unique information for that request (note you can make Lock a number if the unique information is numeric) where ID = 1 and Lock Is NULL
Now select the row and if Lock matches you unique information exit the loop
In the loop, after the test, sleep for a short period (say 10 milliseconds) to avoid blocking
After the loop execute your EXE
In the Finally of the try/catch block issue an update query to set lock to NULL where ID = 1 and Lock = unique information

The only danger with this approach is that somehow a process exits without ever clearing the lock. To solve this you could add a timestamp and run a scheduled job on your database to clear Lock after a timeout on that timestamp. If you are not using a DBMS server, test the timestamp in your loop and clear it if it has expired, but you still need to test to ensure that this process is the one that holds the lock.


这篇关于asp.net中的锁定机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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