Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / VB / VB.net - How do you code a Mutex?

Author
Message
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2007 13:11
The scenario I have is a very simple one: 2 programs, each "owning" a mutex. I want to check that each program is still running from the other one. One is VB.net, the other is DBP.

I have my Mutexes, with the VB one "owned" at startup:



The VB app then starts the DBP app. So firtsly, I need to wait until the DBP Mutex becomes locked, to know we are up and running. After that, I check it periodically. The question is, how do I check the status mutex, knowing that in different circumstances it may be "locked" or "unlocked"?

John Y
Synergy Editor Developer
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: UK
Posted: 14th Nov 2007 13:29
Create a thread that sleeps for the time period of 'periodically'. When it wakes up have it try and take control of the mutex, with a timeout that allows it to fail.

Something like,

if(!mutDBP.Lock(1))
{
// Can't lock mutex, so DBP app is still running
}

Synergy Editor - Available in the WIP forum
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2007 14:59
Quote: "if(!mutDBP.Lock(1))"


This is the issue...there is no method that looks like I can try to lock the mutex.

John Y
Synergy Editor Developer
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: UK
Posted: 14th Nov 2007 15:23
It's not Lock in .Net, it's WaitOne

if(!mutDBP.WaitOne(1, false))
{
// Can't lock mutex, so DBP app is still running
}

Synergy Editor - Available in the WIP forum
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2007 15:52
Ok, that looks good! Thanks, John.

BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2007 17:56
It almost works, but it throws an exception, and I'm not sure why.
When the program starts, and I load the DBP app from VB, it sits nicely in a loop, waiting for the Mutex to be locked by DBP.

Then, 5 seconds later, the first check kicks in, and in my test I have already killed the DBP app...



The message is:

Quote: "
The wait completed to an abandoned mutex
at System.Threading.WaitHandle.WaitOne()"


John Y
Synergy Editor Developer
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: UK
Posted: 14th Nov 2007 18:05 Edited at: 14th Nov 2007 18:06
http://msdn2.microsoft.com/en-us/library/system.threading.abandonedmutexexception.aspx

Looks like you can just handle it.



Synergy Editor - Available in the WIP forum
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 14th Nov 2007 18:19
or, in my VB.net terms...

On Error Resume Next

Thanks again

John Y
Synergy Editor Developer
22
Years of Service
User Offline
Joined: 4th Sep 2002
Location: UK
Posted: 14th Nov 2007 20:04 Edited at: 14th Nov 2007 20:04
I've done a lot of work with MSIL in the past, and I don't recommend that you take language specific shortcuts. When you do, the compiler spits out a load of support code to make it work correctly. Take a look at the MSIL produced with C# (no on error resume next) and VB.Net

C# (24 lines)


VB (63 lines!)


Synergy Editor - Available in the WIP forum
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 15th Nov 2007 00:52
Quote: "On Error Resume Next"


GAH!!! NO!

Handle the error, dont just bypass it


Sub/Function...
Dim... objects global to the try catch block, but local in sub/func scope

Try
'code you would like to run in your sub or function
Catch ex As Exception
'what to do if an exception occurs. The object is ex (ie ex.tostring)
Finally
'code to run whether an exception occurs OR not
End Try
End Sub/Function

Finally's are optional in the Try - Catch - End Try block

My DBP plugins page is now hosted [href]here[/href]
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Nov 2007 10:17
Nice, I learnt something new from this. I suppose On Error shows when I last updated my VB knowledge, eh?

CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 15th Nov 2007 13:28
yeah a bit dated, but alas! you have the power of .net now

My DBP plugins page is now hosted [href]here[/href]

Login to post a reply

Server time is: 2025-05-06 04:29:00
Your offset time is: 2025-05-06 04:29:00