简体   繁体   English

如何通过C#在所有应用程序域中定义一个公共变量

[英]How to define a common variable in all application domains by c#

Consider I want to run APP.exe 2 times, respectively. 考虑我要分别运行2次APP.exe。 I need to define a variable to be shared in all application domains. 我需要定义一个在所有应用程序域中共享的变量。 for example I have a integer X in my code, I changed it in app.exe(1)(means first run of app) and then want to use it in app.exe(2)(second run of same app). 例如,我的代码中有一个整数X,我在app.exe(1)中更改了它(意味着第一次运行应用程序),然后又想在app.exe(2)(同一应用程序的第二次运行)中使用它。 But each time the variables initialize and each run app have IT'S X. I know this is not happen in web application if I set Static for X, but for WPF and Winform, the application domains are different and the X is different. 但是每次变量初始化并且每个运行的应用程序都具有IT X时。我知道如果为X设置为Static,则在Web应用程序中不会发生这种情况,但是对于WPF和Winform,应用程序域是不同的,并且X是不同的。 Actually my real problem is locking on entity model. 实际上,我真正的问题是锁定实体模型。 I want to prevent accessing to model by each instance of application. 我想防止每个应用程序实例访问模型。 I know it is possible by SQL but I want to Lock a Common Shared variable in C#. 我知道SQL可以实现,但是我想锁定C#中的Common Shared变量。

EDITED 已编辑

I used Mutex but the problem still remains. 我使用了Mutex,但问题仍然存在。 See my example please 请看我的例子

private static Mutex mut = new Mutex();

public void Dopessimistic(int id , string name)
{       
    mut.WaitOne();
    {
        MessageBox.Show("nm1");
    }
}

I Run my app.exe 2 times. 我运行我的app.exe 2次。 I expect that The message will be shown just one time, because I do not release the mutex, but for every run of app.exe it works and shows the message. 我希望该消息仅显示一次,因为我没有释放互斥锁,但是对于app.exe的每次运行,它都可以工作并显示该消息。 Actually the Mutex seems not to be shared among All applications and each run has its own mutex separately!! 实际上,互斥体似乎并没有在所有应用程序之间共享,并且每次运行都单独拥有自己的互斥体!

You should use: 您应该使用:

private static Mutex mut = new Mutex(false,"MyAppMutex"); 私有静态Mutex mut = new Mutex(false,“ MyAppMutex”);

public void Dopessimistic(int id , string name) { 公共无效Dopessimistic(int id,字符串名称){

        mut.WaitOne();
        {
            MessageBox.Show("nm1");

        }

    }

In that way you will create a systemwide Mutex . 这样,您将创建系统范围的Mutex

If you create an unnamed one (like you did in your code), it will be local to your app. 如果您创建一个未命名的文件(如您在代码中所做的一样),则该文件将位于您的应用程序本地。

Note : if one app takes ownership of the Mutex and is closed without releasing it, the Mutex becomes 'abandoned' and the next thread waiting for the Mutex will get ownership. 注意 :如果一个应用程序拥有Mutex所有权并在没有释放的情况下关闭,则Mutex将被“放弃”,并且等待Mutex的下一个线程将获得所有权。 In your test this would mean that the second instance of App.exe gets to show its text as well, but only after the first instance has been closed. 在您的测试中,这意味着App.exe的第二个实例也将显示其文本,但是仅在第一个实例关闭之后。

You can do it in many ways. 您可以通过多种方式进行操作。 you may pass your variable as form parameter- 您可以将变量作为表单参数传递-

private void button1_Click(object sender, EventArgs e)
{
        int X=5;
        Form2 frm2 = new Form2(X);
        frm2.Show();    
}

In second form just get the value - 以第二种形式获取值-

 public Form2(int X)
    {
        InitializeComponent();
        textBox1.Text = X.ToString();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM