Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / August 2003

Tip: Looking for answers? Try searching our database.

Mutex

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Leonardo Lanni - 28 Aug 2003 13:56 GMT
I've still my 2 processes, the first installs shared memory and a mutex, the
second opens both and has the purpose to create and manage a collection of
messages written into the shared memory. So by the second process I'm able
to add,remove  a message or view the all into the shared memory, into a LIFO
structure.
I need a mutex in order to avoid data inconsistence in the shared memory if
2 or more instances of the program are working, so that every function (add,
remove, view) makes WaitForSingleObject on the mutex immediately before
operating on shared memory,and makes a ReleaseMutex when it finishes to work
on it.
In installing Mutex (1st program) I used : sem1 =
CreateMutex(0,FALSE,"leo");
In opening Mutex (2nd program) I used : sem =
OpenMutex(MUTEX_ALL_ACCESS,FALSE,"leo");
The problem is this : Sometimes in using Add/Remove/View function, the
process blocks as if the mutex was 0 (but of course it is initially set on
1), and there is no way to unblock it, but closing both the processes (1st
and 2nd) and restarting both..
I thought the problem was in choosing the desidered access mode:
MSDN suggested to use MUTEX_MODIFY_STATE if I need to use ReleaseMutex (as
my case),
but using it the mutex does not work at all..(opening 2 instances there is
no block on function is mutex is on 0)
While using MUTEX_ALL_ACCESS the mutex works correctly but I obtain the
problem shown before.
I've also thought it depends on the BOOL regulates the inherit of to process
created by this one, that I've set on FALSE.
Please help me in this and I'm ready with my programm!
Thank you again,
Leonardo
Dino Chiesa [MSFT] - 28 Aug 2003 17:42 GMT
It's not clear what you are doing.  Can you show us a complete, simple test
case? I don't want your entire app, just a small test case that shows the
behavior you don't like or do not understand.

For example, below is a test that illustrates a mutex.  Compile the code,
then open 2 command windows and run the exe in both.  You will see the
instances of the app obtaining and releasing the mutex.   Does this do what
you want?  If not, why not?

-Dino

// ---------------------------------------------------------------------
namespace Ionic.Demo {

 public class MutexTest1 {
   static string MutexName= "MutexTest";
   static int SleepTime= 4000;
   System.Threading.Mutex  MyMutex= null;

   public void TryAgain() {
     bool stat= false;
     int i;
     for (i=0; i < 11 & !stat; i++) {
       stat= MyMutex.WaitOne(SleepTime/10+100,true);
     }
     if (stat) {
       System.Console.WriteLine("Obtained the mutex on cycle {0}", i);
       System.Console.WriteLine("Sleeping for {0}", SleepTime);
       System.Threading.Thread.Sleep(SleepTime);  // in ms
       MyMutex.ReleaseMutex();
     }
   }

   public void InitialTry() {
     //create a mutex with a unique name
     MyMutex= new System.Threading.Mutex(false,MutexName);

     // initial attempt to grab:
     if (MyMutex.WaitOne(10,true)) {
       System.Console.WriteLine("Got the mutex. Sleeping for {0}",
SleepTime);
       System.Threading.Thread.Sleep(SleepTime);  // in ms
       // release
       System.Console.WriteLine("Releasing the mutex....");
       MyMutex.ReleaseMutex();
       System.Threading.Thread.Sleep(120);  // in ms
       System.Console.WriteLine("Trying to re-acquire the mutex....");
       TryAgain();
     }
     else {
       System.Console.WriteLine("Could not get the mutex at startup.
Polling...");
       TryAgain();
     }
   }

   public static void Main(string[] args) {

     MutexTest1 me= new MutexTest1();
     me.InitialTry();
     System.Console.WriteLine("exiting...");
   }
 }

}

> I've still my 2 processes, the first installs shared memory and a mutex, the
> second opens both and has the purpose to create and manage a collection of
[quoted text clipped - 26 lines]
> Thank you again,
> Leonardo

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.