Results 1 to 16 of 16

Thread: making your programs wait

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25

    Red face

    can anybody plz tell me how to make my program execute slower.
    eg :
    I have to kill a process and then rename the file of the which process i just killed.

    problem is that the process takes time to kill and the renaming code tries to execute while the process is still closing.

    so in between the two pieces of code i have to , like put a wait statement or something like that.

    Any ideas?
    Go Luke , and may the source be with you.

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    try putting DoEvents between the two processes.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Halifax,UK
    Posts
    274
    Try this

    kill "myfile.txt"
    do until dir("myfile.txt")=""

    loop
    'next process

    regards

    Chris a
    VB6 VS2005

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25
    i shall try so , kind ppl's

    how would i use "DoEvents" ?
    Go Luke , and may the source be with you.

  5. #5
    Guest
    'code
    'code
    DoEvents
    'finish stuff above the DoEvents before continuing the blah blah's
    'blah
    'blah

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25
    i think my code that i am using to kill the process(from taskman) is not working properly .
    i have to use it like this

    Code:
    If AppKill("c:\windows\explorer.exe") = True then
          End If
    i think that it is sloppy coding to have to use the Appkill function in an if statement.

    Does anybody know of easier code that i could borrow/use ?

    thnx

    Go Luke , and may the source be with you.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25
    thanx Gates
    i will try to use that
    Go Luke , and may the source be with you.

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Worse case:
    You could add a timer to check for the handle of the process you are trying to kill.
    If not found then rename else keep checking till gone.

    Post your code if you have problems.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25
    naw
    i think it is primitive for the timer to check , anyway , i tried that already and it takes too long.

    I fixed the appkill function and i put in DoEvents after the renaming of each file but it still kills the process too slowly or goes to the renaming code too fast (ie :while the process is being killed)

    here is the code that i use :

    Code:
    KillApp ("c:\windows\explorer.exe") ' here i kill the app (using a function i got from somewhere else)
        
        DoEvents  ' supposed to wait here but doesnt
        
        'here i save some settings
        cfeir = GetSetting("Desklok", "Security", "CFEIR")
        
        a1ir = Val(cfeir) + 1
        
        SaveSetting "Desklok", "Security", "CFEIR", a1ir
        SaveSetting "Desklok", "Security", "CFEIRD", Date
        SaveSetting "Desklok", "Security", "CFEIRt", Time
            
        regtr = GetSetting("Desklok", "Preferences", "tColor1")
        regtg = GetSetting("Desklok", "Preferences", "tColor2")
        regtb = GetSetting("Desklok", "Preferences", "tColor3")
        regbr = GetSetting("Desklok", "Preferences", "bColor1")
        regbg = GetSetting("Desklok", "Preferences", "bColor2")
        regbb = GetSetting("Desklok", "Preferences", "bColor3")
        
        
        get2 = GetSetting("Desklok", "Preferences", "Disableclock")
        agree = GetSetting("Desklok", "Legal", "Agree")         'assign vals to vars
        stacheck = GetSetting("Desklok", "Security", "Startup")
        
        If agree = "" Then     'checks to see if it is first prog run
            frmabout.Show vbModal     ' if it is 1st run then show about box with legal agreement
            frmoptions.Show vbModal      ' also set first pass if agree at aboutbox
        End If
                   
    'and here is the code that renames the files , which gets executed too quickly    
    If stacheck = "no" Or stacheck = "" Then
            SaveSetting "Desklok", "Security", "Startup", "yes"
            Name "c:\windows\explorer.exe" As "c:\program files\desklok\real\explorer.exe"
            DoEvents
            Name "c:\program files\desklok\fake\explorer.exe" As "c:\windows\explorer.exe"
            DoEvents
        End If
    Go Luke , and may the source be with you.

  10. #10
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Instead of KillApp, use SyncKillApp.
    Code:
    Call SyncKillApp("C:\Windows\Explorer.exe") ' Here you kill the app (using a function you got from me)
    ' DoEvents is not needed, SyncKillApp takes care of it
    ' The rest of your code here!
    And here's the function... Put it in a module to make it look pretty.
    It returns True on success, False on failure.
    Code:
    Option Explicit
    
    Private Const MAX_PATH = 260
    Private Const TH32CS_SNAPPROCESS = &H2
    Private Const PROCESS_TERMINATE = &H1
    Private Const WAIT_TIMEOUT = &H102&
    
    Private Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile(1 To MAX_PATH) As Byte
    End Type
    
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Function StripNulls(ByVal sString As String) As String
        Dim lPos As Long
        
        lPos = InStr(sString, vbNullChar)
        If lPos Then StripNulls = Left(sString, lPos - 1) Else StripNulls = sString
    End Function
    
    Public Function SyncKillApp(ByVal sProcessExeName As String) As Boolean
        Dim hSnapshot As Long, hProcess As Long
        Dim tEntry As PROCESSENTRY32
        
        hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
        If hSnapshot = -1 Then Exit Function
        
        tEntry.dwSize = Len(tEntry)
        
        If Process32First(hSnapshot, tEntry) = 0 Then
            Call CloseHandle(hSnapshot)
            Exit Function
        End If
        
        Do Until StripNulls(UCase(StrConv(tEntry.szExeFile, vbUnicode))) = UCase(sProcessExeName)
            tEntry.dwSize = Len(tEntry)
            
            If Process32Next(hSnapshot, tEntry) = 0 Then
                Call CloseHandle(hSnapshot)
                Exit Function
            End If
        Loop
        
        Call CloseHandle(hSnapshot)
        hProcess = OpenProcess(PROCESS_TERMINATE, 0, tEntry.th32ProcessID)
        If hProcess = 0 Then Exit Function
        
        If TerminateProcess(hProcess, 0) = 0 Then
            Call CloseHandle(hProcess)
            Exit Function
        End If
        
        Do
            DoEvents
        Loop Until Not WaitForSingleObject(hProcess, 0) = WAIT_TIMEOUT
        
        Call CloseHandle(hProcess)
        SyncKillApp = True
    End Function
    Enjoy!

  11. #11
    Lively Member
    Join Date
    Aug 2000
    Posts
    114
    Put this wait function I have listed below. To execute it just put something like... Wait(5) ..... 5 being the number of seconds you want to wait:



    Function Wait(numseconds As Long)

    Dim start As Variant, rightnow As Variant
    Dim HourDiff As Variant, MinuteDiff As Variant, SecondDiff As Variant
    Dim TotalMinDiff As Variant, TotalSecDiff As Variant
    start = Now

    While True
    rightnow = Now
    HourDiff = Hour(rightnow) - Hour(start)
    MinuteDiff = Minute(rightnow) - Minute(start)
    SecondDiff = Second(rightnow) - Second(start) + 1

    If SecondDiff = 60 Then
    MinuteDiff = MinuteDiff + 1 ' Add 1 to minute.
    SecondDiff = 0 ' Zero seconds.
    End If

    If MinuteDiff = 60 Then
    HourDiff = HourDiff + 1 ' Add 1 to hour.
    MinuteDiff = 0 ' Zero minutes.
    End If

    TotalMinDiff = (HourDiff * 60) + MinuteDiff ' Get totals.
    TotalSecDiff = (TotalMinDiff * 60) + SecondDiff

    If TotalSecDiff >= numseconds Then
    Exit Function
    End If

    DoEvents

    Wend

    End Function

  12. #12
    Lively Member
    Join Date
    Aug 2000
    Posts
    114
    The DoEvents statement is not going to make your program wait in that spot. DoEvents yields execution so that the operating system can process other events. That way your program will not hang when it is in a loop or something and you have a DoEvents in there.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25

    Talking

    gee , what a helpful lot you are

    Yonatan , i will try your code even though i got my app working now.

    but now that i have it working , there is a tiny glitch.
    see below

    here is the code :
    Code:
    SaveSetting "Desklok", "Security", "Startup", "no"
        
        Name "c:\windows\explorer.exe" As "c:\windows\explorer.fak"
        Sleep 500
        Name "c:\windows\Explorer.rea" As "c:\windows\Explorer.exe"
        Sleep 500
        
        GetSetting "Desklok", "Security", "CFEIR"
        
        If Val(cfeir) > 1 Then
            SaveSetting "Desklok", "Security", "CFEIR", "0"
            Unload Me
            Shell ("c:\windows\explorer.exe")
            DoEvents
        Else
            Shell ("c:\windows\explorer.exe")
            DoEvents
            SaveSetting "Desklok", "Security", "CFEIR", "0"
            Unload Me
            Set frmback = Nothing
            End
        End If
    End Sub
    this code is at the end of my from load event , all goes well except that it opens a instance of Windows explorer as well as loading the explorer shell.

    maybe a command line parameter for explorer will fix this , but i dont know any of them

    Plz hlp 1 more time

    Thanks for all your help
    Go Luke , and may the source be with you.

  14. #14
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    Waiting for a specified amount of time (with Sleep or with anything else) is no good for this matter.
    Slower computers may take a long time to finish terminating the process with KillApp.
    However, SyncKillApp also waits until the process is done terminating, before returning...
    Which makes it perfect here.

    And, you can try something like this:
    Code:
    Call Shell("C:\Windows\Explorer.exe", vbHide)

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    South Africa
    Posts
    25

    Talking

    Yonatan , dude
    you are a frikkin genius , it is working now , flawlessly.

    I want to compile now and then i will send u the source and setup as a thank you for your help.

    it makes me think how true that "Teenage Programmer" part in your signature is.

    thanks a lot
    Go Luke , and may the source be with you.

  16. #16
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    I don't really need the source.
    If anyone who I ever helped with a VB problem sent me his source code, I would have needed a bigger hard drive!
    Thanks anyway, and naaaaa I really am 14.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width