4

I have the same BackgroundWorker code piece in two simultaneously running applications. Will this code avoid the problem of same resource getting access by two processes and run smoothly?

void bw_DoWork(object sender, DoWorkEventArgs e)
    {
       bool flag = false;
       System.Threading.Thread.Sleep(1000);

       while (flag.Equals(false))
       {

           string dir = @"C:\ProgramData\Msgs";
           try
           {
               if (Directory.GetFiles(smsdir).Length > 0)
               {                      
                   flag = true;
               }

           }
           catch (Exception exc)
           {
               Logger.Log("Dir Access Exception: " + exc.Message);
               System.Threading.Thread.Sleep(10);
           }
       }        
Aakar
  • 87
  • 1
  • 6
  • `flag.Equals(false)`? damn this reminds me some tdwtf horros. `!flag` is way better – Daniel Aug 08 '11 at 23:19
  • I think I have miscommunicated here. I have two applications having the same code piece (in different files) and both of them are trying to access (read only) the same directory. – Aakar Aug 08 '11 at 23:39
  • 1
    as long as you open the files as read only it will be fine. reading the directory structure can't even be locked so I don't see what can go wrong. – Daniel Aug 08 '11 at 23:44

6 Answers6

2

No, it won't solve the issue because setting the boolean's value and checking it is not an atomic function and is thus not thread safe. You have to use either a Mutex or a Monitor object.

Check this link for more info: Monitor vs Mutex in c#

Community
  • 1
  • 1
Skorpioh
  • 1,355
  • 1
  • 11
  • 30
2

On one level, depending on what you're doing, there's nothing wrong with having multiple applications accessing the same directory or file. If it's just read access, then by all means, both can access it at once.

If you've got identical code in multiple applications, then a Boolean isn't going to cut it for synchronization, no matter what you do: Each application has its own copy of the Boolean, and cannot modify the other.

For cross application synhronization, I'd use the Mutex class. There's a constructor that takes a string parameter, specifying the name of the Mutex. Mutex names are unique across all of Windows, not just your application. You can do Mutex m = new Mutex(false, "MySpecialMutex"); in two different applications, and each object will be referring to the same thing.

David Yaw
  • 27,383
  • 4
  • 60
  • 93
0

No, it will not -- at least, the code you have pasted will not accomplish any sort of meaningful process synchronization.

If you want a more detailed and helpful answer, you are going to need to be more specific about what you are doing.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
0

no, 'flag' is local to the scope of the method, which is local to the scope of the thread. In other words, it will also equal false.

This is what the lock function is for. Use it like this In your class, declare a private object called gothread.

in your method write it like this

lock(gothread)
{

// put your code in here, one thread will not be able to enter when another thread is already
// in here

}
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
edepperson
  • 1,035
  • 1
  • 14
  • 33
0

You must come up with some kind of cross-process synchronization scheme - any locking mechanism you use in that code is irrelevant if you're trying to prevent collisions between two processes as opposed to two threads running on the same process.

kprobst
  • 16,165
  • 5
  • 32
  • 53
0

A good way to do locking across processes like this is to use a file. First process in creates a file and opens it with exclusive access, and then deletes it when its done. The second process in will either see that the file exists and have to wait till it doesn't or it will fail when attempting to open the file exclusively.

Kratz
  • 4,280
  • 3
  • 32
  • 55