0

Consider I want to run APP.exe 2 times, respectively. 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). 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. 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#.

EDITED

I used Mutex but the problem still remains. 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. 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. Actually the Mutex seems not to be shared among All applications and each run has its own mutex separately!!

rene
  • 41,474
  • 78
  • 114
  • 152
  • 1
    see https://stackoverflow.com/questions/1795143/synchronization-between-two-processes-in-c – Johan Donne Sep 10 '17 at 09:05
  • please see my edited, not solved yet! thanks –  Sep 10 '17 at 09:20
  • `but for every run of app.exe it works and shows the message.` When you say this are you talking about two instances of app.exe running at the **same time**? Why do you not want to do this in SQL? – mjwills Sep 10 '17 at 09:23
  • exactly! TWO instance, I just Curious is it possible or not, and learn new ways. Actually I want to Lock an entity model on entity framework to do Pessimistic Concurrency!!! –  Sep 10 '17 at 09:47
  • the problem solved by below answer greatly,. NO I do not want open and close application, consider you have two client application and both of them want to access an entity model, I wanted to lock the entity model but each lock object belongs to each instance , as you mentioned, the messagebox was just a simple example. when two instance already runned Just one messagebox can show and thas what I wanted, Thanks you ALL guys. –  Sep 10 '17 at 09:56

2 Answers2

0

You should use:

private static Mutex mut = new Mutex(false,"MyAppMutex");

public void Dopessimistic(int id , string name) {

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

        }

    }

In that way you will create a systemwide 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. 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.

Johan Donne
  • 3,104
  • 14
  • 21
  • great anwer, but would you explain a little more. how it happens actually? isn't the mutex created locally?? So why is it shared? and where is located actually? –  Sep 10 '17 at 10:00
  • another problem is is it work just on a SAME PC or across the network? if I want to use this approach across the network, what is the solution? I see some solutions about using WCF synchronization, is it right? –  Sep 10 '17 at 10:14
  • 1
    A named mutex is systemwide, and maintained by the operating system. Waiting and realasing it, is (underlying) implemented by calls to the OS. If you want a solution that works across a network, I would suggest a client-server approach where the EF model and database access is exposed via an API. If you really want distributed access to the database, you need to implement some communication between the app instances to synchronize the access, but that would not be trivial. Maybe there is something like a 'network shared mutex' but I am not aware of that. – Johan Donne Sep 10 '17 at 10:17
-1

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();

    }
sakib rahman
  • 172
  • 1
  • 14
  • the OP is not referring to different forms, but to different instances of the same application running side by side. – Johan Donne Sep 10 '17 at 09:52
  • we need two instance not two forms, the problem solved , see the answer –  Sep 10 '17 at 09:58