Mutex class

Mutex class

Postby Marco Turco » Fri Dec 24, 2010 5:47 pm

Hi all.
I need to know if my app is already running to avoid more cuncurrent executions.
I always used to check this that the Luis Krause's tMutex class but it seems doen't runs with last FWH/xHarbour version.
Do you know if there is an updater for this class or any alternative ?

Thanks in advance
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Mutex class

Postby Bayron » Fri Dec 24, 2010 6:14 pm

Marco Turco,
You can try with this code:
Code: Select all  Expand view
     IF IsExeRunning(cFileName(GetModuleFileName(GetInstance())))
            msgalert("Program is already running."+CRLF+CRLF+;
                     "You may:"+CRLF+;
                     "  1) Have it minimized in Windows tool bar."+CRLF+;
                     "  2) Have it permanently loaded as a Process,"+CRLF+;
                     "     in this case, you should Restart Windows.","Warning")
            quit
      endif
 
=====>

Bayron Landaverry
(215)2226600 Philadelphia,PA, USA
+(502)46727275 Guatemala
MayaBuilders@gMail.com

FWH12.04||Harbour 3.2.0 (18754)||BCC6.5||UEstudio 10.10||
Windows 7 Ultimate

FiveWin, One line of code and it's done...
User avatar
Bayron
 
Posts: 815
Joined: Thu Dec 24, 2009 12:46 am
Location: Philadelphia, PA

Re: Mutex class

Postby carlos vargas » Fri Dec 24, 2010 10:19 pm

Esto esta probado con fwh 10.06
y xharbour la mas reciente.

salu2
carlos vargas

Code: Select all  Expand view

GLOBAL oMutex

procedure main()
....
return

/*-------------------------------------------------------------------------------------------------*/
/*crea un mutex para evitar abrir aplicacion mas de una ocación*/
INIT PROCEDURE Main_Ini()

   /*crea objeto tmutex*/
   oMutex := TMutex():Open( NIL, FALSE, "KDSoft" )

   /*valida mutex*/
   IF oMutex:hMutex==0
      oMutex := TMutex():Create( NIL, FALSE, "KDSoft" )
   ELSE
      MsgAlert("La aplicación esta en ejecución actualmente!")
      oMutex:Close()
      QUIT
   ENDIF

RETURN

/*-------------------------------------------------------------------------------------------------*/
/*Elimina mutex creado*/
EXIT PROCEDURE Main_End()

   IF oMutex != NIL
      oMutex:Close()
   ENDIF

RETURN



 


Code: Select all  Expand view

/*-------------------------------------------------------------------------------------------------*/
/*archivo cabezera del proyecto*/
#include "fivewin.ch"

/*-------------------------------------------------------------------------------------------------*/

#define iKERNEL         "Kernel32.dll"
#define iASPASCAL       .T.

#define iTRUE           1
#define iFALSE          0

#define MUTANT_QUERY_STATE            1
#define STANDARD_RIGHTS_REQUIRED      983040
#define SYNCHRONIZE                   1048576
#define MUTEX_ALL_ACCESS              nOr( STANDARD_RIGHTS_REQUIRED, SYNCHRONIZE, MUTANT_QUERY_STATE )

#xtranslate Bool2Int( <lVar> )  =>    If( <lVar>, iTRUE, iFALSE )

/*-------------------------------------------------------------------------------------------------*/

CREATE CLASS TMutex
   DATA cName
   DATA hMutex

   METHOD Create( sMutexAttr, lInitialOwner, cName ) CONSTRUCTOR
   METHOD New( sMutexAttr, lInitialOwner, cName ) INLINE ::Create( sMutexAttr, lInitialOwner, cName )
   METHOD Open( nAccess, lInitialOwner, cName ) Constructor
   METHOD Close()
   METHOD Release()
   METHOD End() INLINE ::Close()
   METHOD Failed() HIDDEN

ENDCLASS

/*-------------------------------------------------------------------------------------------------*/

METHOD Create( sMutexAttr, lInitialOwner, cName ) CLASS TMutex
   LOCAL hDLL    := LoadLibrary( iKERNEL )
   LOCAL cFunc   := "CreateMutexA"
   LOCAL cBuffer := NIL
   LOCAL cFarProc

   DEFAULT lInitialOwner := .F., cName := "FiveWin App"

   ::hMutex := 0
   ::cName  := ""

   IF ValType( sMutexAttr ) == "O" .and. Upper( sMutexAttr:className() ) == "TSTRUCT"
      cBuffer := sMutexAttr:cBuffer
   Endif

   ::cName := cName

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG, LONG, STRING )
      ::hMutex := FWCallDLL( cFarProc,  iif( cBuffer != NIL, @cBuffer, cBuffer ), Bool2Int( lInitialOwner ), cName + Chr(0) )
      FreeLibrary( hDLL )
      IF cBuffer != NIL
         sMutexAttr:cBuffer := cBuffer
      ENDIF
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN Self

/*-------------------------------------------------------------------------------------------------*/

METHOD Open( nAccess, lInitialOwner, cName ) CLASS TMutex
   LOCAL hDLL    := LoadLibrary( iKERNEL )
   LOCAL cFunc   := "OpenMutexA"
   LOCAL cFarProc

   DEFAULT nAccess := MUTEX_ALL_ACCESS, lInitialOwner := .F., cName := "FiveWin App"

   ::cName := cName

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG, LONG, STRING )
      ::hMutex := FWCallDLL( cFarProc, nAccess, Bool2Int( lInitialOwner ), cName + Chr(0) )
      FreeLibrary( hDLL )
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN Self

/*-------------------------------------------------------------------------------------------------*/

METHOD Close() CLASS TMutex
   LOCAL hDLL  := LoadLibrary( iKERNEL )
   LOCAL cFunc := "CloseHandle"
   LOCAL nResult
   LOCAL cFarProc

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG )
      nResult  := FWCallDLL( cFarProc, ::hMutex )
      FreeLibrary( hDLL )
      ::hMutex := 0
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN nResult

/*-------------------------------------------------------------------------------------------------*/

METHOD Release() CLASS TMutex
   LOCAL hDLL  := LoadLibrary( iKERNEL )
   LOCAL cFunc := "ReleaseMutex"
   LOCAL cFarProc
   LOCAL nResult

   IF Abs( hDLL ) > 32
      cFarProc := GetProcAdd( hDLL, cFunc, iASPASCAL, LONG, LONG )
      nResult := FWCallDLL( cFarProc, ::hMutex )
      FreeLibrary( hDLL )
   ELSE
      ::Failed( hDLL, cFunc )
   ENDIF

RETURN nResult

/*-------------------------------------------------------------------------------------------------*/

METHOD Failed( nError, cFunc ) CLASS TMutex
   MsgAlert( "Error: " + LTrim( Str( nError ) ) + " al cargar " + iKERNEL + CRLF + "Función: " + cFunc, "Clase " + ::className() )
RETURN Self


/*-------------------------------------------------------------------------------------------------*/
//EOF
/*-------------------------------------------------------------------------------------------------*/
 
Salu2
Carlos Vargas
Desde Managua, Nicaragua (CA)
User avatar
carlos vargas
 
Posts: 1720
Joined: Tue Oct 11, 2005 5:01 pm
Location: Nicaragua

Re: Mutex class

Postby Horizon » Sat Dec 25, 2010 9:50 am

Hi,

Which one should be choose? It seem both does the same thing? Is there any advantage using Tmutex class according to IsExeRunning function?

Thanks,
Regards,

Hakan ONEMLI

Harbour & MSVC 2022 & FWH 23.04
Horizon
 
Posts: 1322
Joined: Fri May 23, 2008 1:33 pm

Re: Mutex class

Postby Bayron » Sat Dec 25, 2010 8:28 pm

Horizon,
I don't know the use of the class, but for my use, the function I placed above is enough.

Maybe the class has a function other than just detecting the App Running, I hope users of the class can lighten us up...
=====>

Bayron Landaverry
(215)2226600 Philadelphia,PA, USA
+(502)46727275 Guatemala
MayaBuilders@gMail.com

FWH12.04||Harbour 3.2.0 (18754)||BCC6.5||UEstudio 10.10||
Windows 7 Ultimate

FiveWin, One line of code and it's done...
User avatar
Bayron
 
Posts: 815
Joined: Thu Dec 24, 2009 12:46 am
Location: Philadelphia, PA

Re: Mutex class

Postby Marco Turco » Sun Dec 26, 2010 11:34 am

Hi, thanks everybody for the support.

Using a Mutex instead of a call to IsExeRunning has advantages because the mutex can be easly checked from other windows applications,
just a sample: if you use InnoSetup to install your application or an upgrade for your application then you can check if it is already running simply adding AppMutex=<your app name> on the InnoSetup install code.

I would like - if possible - that class could be included in FWH as standard class in order to have it regulary updated - Antonio ?
Best Regards,

Marco Turco
SOFTWARE XP LLP
User avatar
Marco Turco
 
Posts: 858
Joined: Fri Oct 07, 2005 12:00 pm
Location: London

Re: Mutex class

Postby Bayron » Sun Dec 26, 2010 4:30 pm

Marco,

Thanks for the info....
=====>

Bayron Landaverry
(215)2226600 Philadelphia,PA, USA
+(502)46727275 Guatemala
MayaBuilders@gMail.com

FWH12.04||Harbour 3.2.0 (18754)||BCC6.5||UEstudio 10.10||
Windows 7 Ultimate

FiveWin, One line of code and it's done...
User avatar
Bayron
 
Posts: 815
Joined: Thu Dec 24, 2009 12:46 am
Location: Philadelphia, PA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 92 guests