FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Mutex class
Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Mutex class
Posted: Fri Dec 24, 2010 05: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
Posts: 883
Joined: Thu Dec 24, 2009 12:46 AM
Re: Mutex class
Posted: Fri Dec 24, 2010 06:14 PM
Marco Turco,
You can try with this code:
Code (fw): Select all Collapse
 聽 聽 聽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
xBasePHP.com
(215)2226600 Philadelphia,PA, USA
MayaBuilders@gMail.com
Guatemala

FWH25.06--Harbour 3.0.0--BCC7.7--UEstudio 10.10
Windows 10

FiveWin, One line of code and it's done...

Posts: 1789
Joined: Tue Oct 11, 2005 05:01 PM
Re: Mutex class
Posted: Fri Dec 24, 2010 10:19 PM
Esto esta probado con fwh 10.06
y xharbour la mas reciente.

salu2
carlos vargas

Code (fw): Select all Collapse
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 (fw): Select all Collapse
/*-------------------------------------------------------------------------------------------------*/
/*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)
Posts: 1387
Joined: Fri May 23, 2008 01:33 PM
Re: Mutex class
Posted: Sat Dec 25, 2010 09: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.06
Posts: 883
Joined: Thu Dec 24, 2009 12:46 AM
Re: Mutex class
Posted: Sat Dec 25, 2010 08: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
xBasePHP.com
(215)2226600 Philadelphia,PA, USA
MayaBuilders@gMail.com
Guatemala

FWH25.06--Harbour 3.0.0--BCC7.7--UEstudio 10.10
Windows 10

FiveWin, One line of code and it's done...

Posts: 858
Joined: Fri Oct 07, 2005 12:00 PM
Re: Mutex class
Posted: 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
Posts: 883
Joined: Thu Dec 24, 2009 12:46 AM
Re: Mutex class
Posted: Sun Dec 26, 2010 04:30 PM

Marco,

Thanks for the info....

=====>

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

FWH25.06--Harbour 3.0.0--BCC7.7--UEstudio 10.10
Windows 10

FiveWin, One line of code and it's done...

Continue the discussion