[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Leonardo Lanni

8/28/2003 12:56:00 PM

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


1 Answer

Dino Chiesa [MSFT]

8/28/2003 4:42:00 PM

0

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...");
}
}

}


"Leonardo Lanni" <djleonardo@lanni.albaweb.it> wrote in message
news:biku59$3id$1@newsread.albacom.net...
> 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
>
>