geoffrey54
Messages postés3Date d'inscriptionsamedi 26 février 2005StatutMembreDernière intervention30 septembre 2007
-
28 sept. 2007 à 15:38
geoffrey54
Messages postés3Date d'inscriptionsamedi 26 février 2005StatutMembreDernière intervention30 septembre 2007
-
30 sept. 2007 à 14:26
Salut a tous,
voila j'essaie de faire un programme permettant de savoir si des fichiers ont ete modifiés dans un dossier. Ca marche seulement mes boutons sont bloqués aprés. Voici mon code:
Private Sub Command1_Click()
Dim Ret As Long
'Set the notification hook
Ret = FindFirstChangeNotification("C:\temp2", &HFFFFFFFF, FILE_NOTIFY_CHANGE_ALL)
flag = False
'Wait until the event is triggered
WaitForSingleObject Ret, &HFFFFFFFF
MsgBox "Event Triggered for the first time"
'Reactivate our hook
Do While flag = False
FindNextChangeNotification Ret
'Wait until the event is triggered
DoEvents
WaitForSingleObject Ret, &HFFFFFFFF
DoEvents
MsgBox "Event Triggered for the second time"
'Remove our hook
Loop
FindCloseChangeNotification Ret
End Sub
Private Sub Command2_Click()
flag = True
End Sub
En faite j'aimerais que lorsque je click sur le bouton 2 il arrete de surveiller le dossier mais impossible de clicker sur le 2eme bouton. Je pensait que l'instruction Doevent suffirait mais non.
cs_casy
Messages postés7741Date d'inscriptionmercredi 1 septembre 2004StatutMembreDernière intervention24 septembre 201440 28 sept. 2007 à 18:11
La solution serait aussi de ne pas mettre un délai d'attente infini pour WaitForSingleObject.
En mettant &HFFFFFFFF, tu indique un temps d'attente infini. Ce qui fait que ton programme reste bloqué sur l'instruction WaitForSingleObject tant que l'évènement correspondant à Ret ne sera pas déclenché. Et il peut retser ainsi bloqué des siècles.
xav0
Messages postés20Date d'inscriptionsamedi 23 septembre 2006StatutMembreDernière intervention10 octobre 2007 30 sept. 2007 à 10:04
ça doit être le premier WaitForSingleObject qui bloque encore. Il faut le remplacer aussi, et du coup, je pense que c'est plus simple de tout mettre dans la même boucle :
Dim Ret As Long, Code As Long, i As Long
'Set the notification hook
Ret = FindFirstChangeNotification("C:\temp2", INFINITE, FILE_NOTIFY_CHANGE_ALL)
flag = False
'Cette boucle se termine quand l'utilisateur a annulé la surveillance
DoEvents 'Il faut faire DoEvents avant MsgWaitForMultipleObjects
Do Until flag = True 'Test du flag entre DoEvents et MsgWaitForMultipleObjects
Code = MsgWaitForMultipleObjects(1, Ret, 0, INFINITE, QS_ALLINPUT)
If Code = WAIT_OBJECT_0 Then
'Il y a eu une modification
i = i + 1
MsgBox "Modification détectée (n° " & i & ")"
'Fin du traitement, attend la modification suivante...
FindNextChangeNotification Ret
ElseIf Code = -1 Then
MsgBox "Une erreur s'est produite"
Exit Do
End If
DoEvents
Loop
xav0
Messages postés20Date d'inscriptionsamedi 23 septembre 2006StatutMembreDernière intervention10 octobre 2007 28 sept. 2007 à 17:28
Bonjour,
la solution est de remplacer WaitForSingleObject (bloquante) par MsgWaitForMultipleObjects qui s'arête lorsque la modification du fichier est faite OU lorsqu'un événement arrive dans l'application.
Do
DoEvents
If flag = true then Exit Do
Loop While MsgWaitForMultipleObjects(1, Ret, 0, INFINITE, QS_ALLINPUT) = WAIT_OBJECT_0 + 1
If flag = true then ... 'L'utilisateur a annulé
(Const WAIT_OBJECT_0 0, QS_ALLINPUT 255)
Vous n’avez pas trouvé la réponse que vous recherchez ?
geoffrey54
Messages postés3Date d'inscriptionsamedi 26 février 2005StatutMembreDernière intervention30 septembre 2007 29 sept. 2007 à 14:19
voila ce que j'ai mis:
Dim Ret As Long
'Set the notification hook
Ret = FindFirstChangeNotification("C:\temp2", &HFFFFFFFF, FILE_NOTIFY_CHANGE_ALL)
flag = False
WaitForSingleObject Ret, &HFFFF
MsgBox "Event Triggered for the first time"
FindNextChangeNotification Ret
Do
DoEvents
If flag = True Then Exit Do
Loop While MsgWaitForMultipleObjects(1, Ret, 0, INFINITE, QS_ALLINPUT) = WAIT_OBJECT_0 + 1
If flag = True Then MsgBox "L'utilisateur a annulé"
End Sub
Private Sub Command2_Click()
flag = True
End Sub
voila ce que j'ai mis mais il y a une erreur qui fait tout planter...
les deux autres solutions ne marchent pas puisque DoEvents je l'avais deja mis et le fait de raccourcir le temps ne change me permet d'appuyer mais le programme ne fait pas ce que je veux...
geoffrey54
Messages postés3Date d'inscriptionsamedi 26 février 2005StatutMembreDernière intervention30 septembre 2007 30 sept. 2007 à 14:26
C bon j'ai fait un mix des deux reponses:
DoEvents 'Il faut faire DoEvents avant MsgWaitForMultipleObjects
Do Until flag = True 'Test du flag entre DoEvents et MsgWaitForMultipleObjects
Code = WaitForSingleObject(Ret, &HFF)
If Code = WAIT_OBJECT_0 Then
'Il y a eu une modification
i = i + 1
MsgBox "Modification détectée (n° " & i & ")"
'Fin du traitement, attend la modification suivante...
FindNextChangeNotification Ret
ElseIf Code = -1 Then
MsgBox "Une erreur s'est produite"
Exit Do
End If
DoEvents
Loop
J'ai remplacé MSGwaitblabla par WaitForSingleObject et j'ai reuduit la duree.