Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
SyncLock equivalent in VFP
Message
De
21/05/2014 01:09:03
 
 
À
20/05/2014 16:19:33
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
OS:
Windows 8.1
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01600343
Message ID:
01600365
Vues:
94
This message has been marked as a message which has helped to the initial question of the thread.
>>a bit unclear, as vfp is single threaded. Are you talking semaphores or MTA/STA apartment ?
>
>Semaphore approach but not related to data. SyncLock allows to lock at the code level until the lock is released, thus in a MTA apartment. The goal is to make sure that two instance of the same EXE cannot collide on processing the same file.

Apart from locking the file, a mutex perhaps

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682411(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx
	local mutexObj, mutexName, tryDuringMilliseconds
	mutexName = 'TheNameIWant'  && the name of the file.  In TSE you may want to prefix the name with Global\
	tryDuringMilliseconds = 200
	
	if( Mutex_Object(@m.mutexObj, m.mutexName, m.tryDuringMilliseconds) )
		?'succeeded'
		
		&& do work
		
		mutexObj = null && release mutex
	else
		?'Fail'
	endif
#include	"FoxPro.h"

#define true	.t.
#define false	.f.

#define ERROR_ALREADY_EXISTS  183

#define MUTEX_CLASS	[_Mutex_Class_]

*_______________________________________________________________________________
function Mutex_Object(obj, mutexName, tryDuringMilliseconds)

	obj = null
	
	try
		obj = createObject(MUTEX_CLASS, m.mutexName, m.tryDuringMilliseconds)
	
	catch
	
	endtry
	
	return IsObject(m.obj)

endfunc
*_______________________________________________________________________________
define class MUTEX_CLASS as relation
	
	protected Handle
	Handle = 0
*_______________________________________________________________________________
protected function Init(mutexName, tryDuringMilliseconds)
	
	local success
	success = true
	
	local handle, lasterror, i
	
	do case
	case !m.success
	
	case !isChar(m.mutexName, true)
		assert false
		success = false
	
	case !IsNonNegativeInteger(m.tryDuringMilliseconds)
		assert false
		success = false
		
	otherwise
		for i = m.tryDuringMilliseconds to 0 step -100

			handle = CreateMutex(null, 1, @m.mutexName)
			lasterror = GetLastError()
			
			do case
			case empty(m.handle)
			
			case inlist(m.lasterror, ERROR_ALREADY_EXISTS)
				=CloseHandle(m.handle)
				handle = 0
				
			otherwise
				exit
				
			endcase
			=Inkey(.100)
		endfor
		

	endcase
	
	do case
	case !m.success
	
	case empty(m.handle)
		assert false
		success = false
	
	otherwise
		this.Handle = m.handle
	
	endcase
	
	return m.success
	
endfunc
*_______________________________________________________________________________
*_______________________________________________________________________________
protected function Destroy()
	
	=empty(m.this.Handle) or !empty(CloseHandle(m.this.Handle))
	
	return DoDefault()
endfunc
*_______________________________________________________________________________

*_______________________________________________________________________________
enddefine
*===============================================================================
*===============================================================================
*===============================================================================
#define T_HANDLE	long
#define	LPSECURITY_ATTRIBUTES	string @
#define BOOL		long
#define LPCTSTR	String @

function CreateMutex(lpMutexAttributes, bInitialOwner, lpName)
	

	local returnValue
	
	declare T_HANDLE CreateMutex IN kernel32.dll ;
		LPSECURITY_ATTRIBUTES  lpMutexAttributes, ;
		BOOL bInitialOwner, ;
		LPCTSTR lpName
		
	returnValue = CreateMutex(m.lpMutexAttributes, m.bInitialOwner, @m.lpName)
	
	return m.returnValue
endfunc
*_______________________________________________________________________________
function CloseHandle(handle)
	
	local returnValue
	
	declare BOOL CloseHandle in Kernel32.dll ;
			T_HANDLE	handle
		
	returnValue = CloseHandle(m.handle)

	
	return m.returnValue

endfunc
*_______________________________________________________________________________
function GetLastError()
	
	local returnValue

	declare integer GetLastError IN Win32API
	returnValue= GetLastError()
	

	return m.returnValue

endfunc
*_______________________________________________________________________________
function IsChar(s, needFilled)
	
	return	inlist(vartype(m.s), T_CHARACTER, T_MEMO) ;
		and	( !m.needFilled or !empty(nvl(m.s, '')) )
	
endfunc
*_______________________________________________________________________________
function IsNonNegativeInteger(x)
	return inlist(vartype(m.x), T_NUMERIC) and empty(mod(m.x, 1)) and (m.x >= 0)
endfunc
*_______________________________________________________________________________
function IsObject(obj)
	
	return inlist(vartype(m.obj), T_OBJECT)

endfunc
*_______________________________________________________________________________
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform