ATOUTFOX
COMMUNAUTÉ FRANCOPHONE DES PROFESSIONNELS FOXPRO
Visual FoxPro : le développement durable

Multi-tâche avec vfp   



L'auteur

eddymaue
Canada Canada
Membre Simple
# 0000000075
enregistré le 26/10/2004
Maue Eddy
j8j 8j8 Gatineau
de la société Formatek
Fiche personnelle


Note des membres
pas de note

Contributions > 05 - API et appels systèmes

Multi-tâche avec vfp
# 0000000126
ajouté le 24/12/2004 01:44:26 et modifié le 31/03/2006
consulté 8934 fois
Niveau initié

Version(s) Foxpro :
VFP 9.0
VFP 8.0
VFP 7.0
VFP 6.0
VFP 5.0


Télécharger le ZIP (21.89 Ko)
Description

Durant l'exécution d'une application certaines taches prennent du temps à s'exécuter. Comme la recherche de fichiers sur un disque, les appels à un webservice ou attendre qu'une page web soit chargée en mémoire.

Vous avez une tâche à accomplir qui prend du temps à s'exécuter. Cela peut être désagréable pour vous et où votre clientèle. Pourquoi ne pas l'exécuter sur processus séparé de votre application? Windows est un système multitâche et la fonction Createprocess de kernel32.Dll permet d'accomplir l'exécution d'une tache sur un processus séparé de votre application.

Créer un nouveau projet. Donnez-lui le nom de votre tache. Faites en un exécutable.

Dans votre projet, remplacez l'appel de votre tache par :

ExecProcess("x:\MaTache.exe",p1,p2,...,pn)

De cette façon la tache va s'exécuter sur processus séparé de votre application.

Code source :
#if .f.
 Adapté par Eddy Maue
 Decembre le 23,2004
 = execprocess("c:\MonExecutable.exe","1","2")
 possibilité d'envoyé 17 paramêtres

retourne -1 si la tâche est exécutée
retourne 0 si la tache n'
est pas  exécutée

 Important : Le passage des paramêtres de l'apiWin32 CreateProcess à votre exécutable se
fait par la séparation avec un espace et non par une virgule

si il y a un espace dans dans l'
un(les) paramêtre(s), il y aura une transformation
          STRTRAN(STRTRAN(&cTransform,"%","%%")," ","%32")

          Ex : si p1 = "Bonjours les foxeurs. 1% des membres utilisent cette api"

          les % deviennent %% et les espaces deviennent %32

          Ce que votre exécutable recevevra
          "Bonjours%32les%32foxeurs.%321%%%32des%32membre%32utilis%32cette%32api"

          Pour retrouver l'original

Le programme pricipal doit retransformer les paramêtres comme suivant

          * Main.prg
LPARAMETERS p1
p1 = EMConvertir(p1)


  * à ajouter dans le main prog ou dans votre fichier de procedure
  * ******************************************************
  * * EMConvertir
  * ******************************************************
  **  Créé par            : Eddy Maue
  * * Créé le             : 03/31/06
  * * Modifier le         :
  * ******************************************************
PROCEDURE EMConvertir(p)
return STRTRAN(STRTRAN(p,"%32"," "),"%%","%")
ENDPROC && EMConvertir




#endif


= execprocess(ADDBS(JUSTPATH(SYS(16)))+"EXECPROCESSEXAMPLE.EXE","paramêtre p1",1,"paramêtre p3")
Function ExecProcess
Lparameters cFile,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p17
Local ;
i As Integer ,;
cParam as Character

For i = 2 To Parameters()
cTransform = "transform(p"+Transform(i)+")"
m.cFile = m.cFile + " '
"+STRTRAN(STRTRAN(&cTransform,"%","%%")," ","%32")+"' "

ENDFOR


#Define NORMAL_PRIORITY_CLASS 32
#Define IDLE_PRIORITY_CLASS 64
#Define HIGH_PRIORITY_CLASS 128
#Define REALTIME_PRIORITY_CLASS 1600

   * Return code from WaitForSingleObject() if
   * it timed out.
#Define WAIT_TIMEOUT 0x00000102

   * This controls how long, in milli secconds, WaitForSingleObject()
   * waits before it times out. Change this to suit your preferences.
#Define WAIT_INTERVAL 200

Declare Integer CreateProcess In kernel32.Dll ;
INTEGER lpApplicationName, ;
STRING lpCommandLine, ;
INTEGER lpProcessAttributes, ;
INTEGER lpThreadAttributes, ;
INTEGER bInheritHandles, ;
INTEGER dwCreationFlags, ;
INTEGER lpEnvironment, ;
INTEGER lpCurrentDirectory, ;
STRING @lpStartupInfo, ;
STRING @lpProcessInformation

Declare Integer WaitForSingleObject In kernel32.Dll ;
INTEGER hHandle, Integer dwMilliseconds

Declare Integer CloseHandle In kernel32.Dll ;
INTEGER hObject

Declare Integer GetLastError In kernel32.Dll


   * STARTUPINFO is 68 bytes, of which we need to
   * initially populate the 'cb' or Count of Bytes member
   * with the overall length of the structure.
   * The remainder should be 0-filled
Start = long2str(68) + Replicate(Chr(0), 64)

   * PROCESS_INFORMATION structure is 4 longs,
   * or 4*4 bytes = 16 bytes, which we'll fill with nulls.
process_info = Replicate(Chr(0), 16)


   * Call CreateProcess, obtain a process handle. Treat the
   * application to run as the 'command line' argument, accept
   * all other defaults. Important to pass the start and
   * process_info by reference.
RetCode = CreateProcess(0, m.cFile+Chr(0), 0, 0, 1, ;
NORMAL_PRIORITY_CLASS, 0, 0, @Start, @process_info)

   * Unable to run, exit now.
If RetCode = 0
=Messagebox("Error occurred. Error code: ", GetLastError())
Return 0
Endif

Local lTerminer
Do While !lTerminer
      * Use timeout of TIMEOUT_INTERVAL msec so the display
      * will be updated. Otherwise, the VFP window never repaints until
      * the loop is exited.
If !WaitForSingleObject(RetCode, WAIT_INTERVAL) != WAIT_TIMEOUT
DoEvents
Else
lTerminer = .T.
         * Show a message box when we're done.
         * Close the process handle afterwards.
RetCode = CloseHandle(RetCode)
return -1
Endif
Enddo



   ********************
Function long2str
   ********************
   * Passed : 32-bit non-negative numeric value (m.longval)
   * Returns : ASCII character representation of passed
   *           value in low-high format (m.retstr)
   * Example :
   *    m.long = 999999
   *    m.longstr = long2str(m.long)

Parameters m.longval

Private i, m.retstr

m.retstr = ""
For i = 24 To 0 Step -8
m.retstr = Chr(Int(m.longval/(2^i))) + m.retstr
m.longval = Mod(m.longval, (2^i))
Next
Return m.retstr


   *******************
Function str2long
   *******************
   * Passed:  4-byte character string (m.longstr)
   *   in low-high ASCII format
   * returns:  long integer value
   * example:
   *   m.longstr = "1111"
   *   m.longval = str2long(m.longstr)

Parameters m.longstr
Private i, m.retval

m.retval = 0
For i = 0 To 24 Step 8
m.retval = m.retval + (Asc(m.longstr) * (2^i))
m.longstr = Right(m.longstr, Len(m.longstr) - 1)
Next
Return m.retval

Commentaires
Aucun commentaire enregistré ...

Publicité

Les pubs en cours :


www.atoutfox.org - Site de la Communauté Francophone des Professionnels FoxPro - v3.4.0 - © 2004-2024.
Cette page est générée par un composant COM+ développé en Visual FoxPro 9.0-SP2-HF3