局域网上的多个exe共享一个全局变量 [英] multiple exe on a lan shared a global variable

查看:123
本文介绍了局域网上的多个exe共享一个全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我用C#编写了一个应用程序,并在局域网中的每台客户端计算机上安装了exe,这实际上是在服务器上共享一个公共数据库,现在我要检查是否有人正在访问challan窗体,那么没有用户可以打开该窗体表格直到关闭,请帮助我

thanx


i made an application in c#,and installed the exe on each client computer within a lan, that is actually sharing a common database on server , and now i want to check for that if some one is accessing challan form then no user can open that form until closed, plz help me out

thanx

推荐答案

此方法没有内置的解决方案.但是您可以使用数据库(逻辑上涉及此锁的专用表或共享表).假设您使用SQL Server,则可以使用适当的锁提示和事务隔离来实现相同的目标(在此处查看详细信息 [ ^ ])-请参见可序列化.您可能会在以下文章中找到更多详细信息:在ADO.NET中使用事务 [ ^ ], MS SQL Server中的锁定和持续时间 [ ^ ].
想法:如果对象被锁定,则只有一个应用程序可以访问该对象,并且您可以使用此行为来控制表单是否可以显示的天气.
如果您采用此路径,请注意要进行测试,以防客户端上的锁被锁住但被服务器切断(被杀死),您可能需要进行一些微调.但是,即使这样,这种方法也比其他分布式IPC更容易编写代码.

更新:
这是您可以用来获取和测试对任何表(生产表或专用表)的独占访问的方案-这是T-SQL版本,但是在C#中也很简单. > 好的,首先,要显示该编辑器表单,请发出此脚本(或等效于C#的脚本):
There is no built-in solution for this approach. But you can use the database (a dedicated table, or a shared table that-s involved in this lock logically). Assuming you use SQL Server proper lock hint and transaction isolation can be used to achieve the same goal (see details here[^]) - see SERIALIZABLE. You might find more details in these articles: Using Transactions in ADO.NET[^], Locks and Duration of Transactions in MS SQL Server[^].
The idea: if an object is locked, only one application can gain access to it, and you can use this behavior to control weather a form can or can not be displayed.
If you take this path, be aware to test it in cases when on client gathered the lock, but was cut from the server (killed), you might need some fine-tuning. But even so, this approach is easier to code than other distributed IPC.

Update:
Here is the scenario you can use to gain and test exclusive access to any table (a production one, or a dedicated one) - this is the T-SQL version, but it is as simple to do it in C# also.
Ok, first of all, before you want to show that editor form, issue this script (or it''s c# equivalent):
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
GO
SET LOCK_TIMEOUT 0;
GO
BEGIN TRANSACTION;
GO
UPDATE T SET X=X WHERE X<>X;
GO


请标记伪DML语句:它什么都不做,但会锁定表T.

用户完成编辑后,发出实际的更新DML语句,然后简单地提交事务:


Please remark the dummy DML statement: it does nothing but locks the table T.

After the user finished editing, issue the actual update DML statements, and simply commit the transaction:

COMMIT TRANSACTION;
GO



发生什么了?通过可序列化的事务,以及发布的更新,客户端将获得整个事务对T的独占访问权-直到由于连接丢失服务器本身提交,回滚或取消该事务为止.而且由于这是零锁定等待超时,因此第二个尝试获得相同锁定的应用程序实例将立即失败.您将在客户端看到一个例外,您可以使用它来警告用户以后再来,因为他要编辑的表是由其他人编辑的.提交(或回滚)后,将释放该锁,以便其他实例可以获取该锁,依此类推.

对于C#版本,请参见 SqlConnection.BeginTransaction [



What happens? With serializable transaction, and the update issued the client will gain exclusive access to the T for the whole transaction - until it is committed, rolled back or canceled by the server itself due to lost connection. And because it is a zero lock wait timeout, the second application instance trying to get the same lock, will fail immediately. You will get an exception on client side, that you can use to warn the user to come back later, since the table he wants to edit is edited by somebody else. After commit (or rollback) the lock is released, so an other instance can get the lock and so on.

For C# version, see SqlConnection.BeginTransaction[^], as I know SET LOCK_TIMEOUT has to be issued as a nonquery command.

You can also fine-tune the timeout value if you can allow waiting. By default it is -1 (infinite) - thus any lock could hang your application.


这篇关于局域网上的多个exe共享一个全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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