Results 1 to 13 of 13

Thread: I guess no one knows...

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Location
    Seattle, WA, USA
    Posts
    46
    Hi,

    I guess no one knows how to make a module that is running in the background close when windows shuts down. My program will not close when windows shuts down. It is a module, with no forms that runs in the background. All of my code is in the Sub Main(). I started with a form set to be invisible but then my code would not execute. So, I put it in a module, and it ran but would not close when windows shuts down (told my program was not respnding, have been told this is because vb does not understand what do when windows sends its shut down message). I have a loop in my program and would like it to end when windows shuts down. I have been told that I have to subclass, but I do not know how. I have posted many posts on this topic, with little success. So I thought I would give it one last try.


    Thanks for all you help and understanding,

    Kevin

  2. #2
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I assume you use a loop in the sub main. I had a quick test, and it worked fine after I included a DoEvents in the loop.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2000
    Location
    Seattle, WA, USA
    Posts
    46
    Thanks,

    I do have a Doevents in my loop and is still will not work. Here is my loop code:

    Code:
    Dim result As Integer
    Dim tdFile As Integer
    
    tdFile = FreeFile
    Open "C:\Log.txt" For Append As tdFile
    Do While Format(Now(), "h:mm am/pm") < Format("8:00 pm", "h:mm am/pm")
        Call CheckKey
          
        If Len(sString) = 50 Then
            Print #tdFile, sString
            sString = ""
            DoEvents
        End If
    Loop

    Thanks,

    Kevin

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Was you told not to do that? I have posted this maany times already: Do NOT use DOEVENTS with continuous loops. The simplest way to solve this problem is to add a form that will automatically on attemt to close, run the code in its Form_Unload event. There you should specify a condition that your loop in your Sub Main will end. Then set cancel=1 You also need to Unload the form once more and set a condition in Form_Unload that will skip the cancel and end.

    Sounds too complicated? I hate this too

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2000
    Location
    Seattle, WA, USA
    Posts
    46
    Ok, let me get this right.

    my code would look some thing like this

    Code:
    Dim result As Integer
    Dim tdFile As Integer
    Dim unloadnow As Byte 
    
    tdFile = FreeFile
    Open "C:\Log.txt" For Append As tdFile
    Loop Until unloadnow 
    unloadnow = 1 
    Unload Me 
    
    Call CheckKey
          
        If Len(sString) = 50 Then
            Print #tdFile, sString
            sString = ""
            DoEvents
        End If
    Loop
    
    Private Sub Form_Unload(Cancel As Integer) 
    If unloadnow < 2 Then 
    unloadnow = 1 
    Cancel = 1 
    End If 
    End 
    End Sub
    Can I make my form invisible and still have the code execute??

    Thanks,

    Kevin


  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Easy thing: form.visible=false

  7. #7
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    Kedaman:
    "Was you told not to do that? I have posted this maany times already: Do NOT use DOEVENTS with continuous loops."

    You have any special reason for stating that?
    IMHO, loops that run longer then 10 times, and don't have doevents, are crappy code... Hangs up windooz (at least it looks like it does for a user..) and say you have a cancel button to cancel the operation, you will never be able to exit that loop.. Heck, you will never be able to end it anyway since a close msg that windooz might send to your program will never be responded to while the loop is running....

  8. #8
    Junior Member
    Join Date
    Mar 2000
    Posts
    29
    Put the DoEvents outside the if statement
    just before the 'loop'
    Maybe your program loop is not getting to the DoEvents

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Also, get rid of the Form. And use a module. Create a Sub routine called Main and change the Startup Object to be Sub Main. This way your application won't have any forms, but will run on a background.

    If you meant something else, please, rephrase.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    no! no! NO! You people don't get it!

    Crazy D: "are crappy code... Hangs up windooz "
    ' I know, I know!!!!! That's why it is complicated.

    Crazy D: "You have any special reason for stating that? "

    Attemt to Close an app while in loop with doevents will cause program not to close. When you try to restart windows with that, it will wait until a End task window will show up. Thats annoying.

    And the solution: When a form (wich have a Unload event that modules don't have) closes, you can end the loop. The code in Kevdogs last reply, will work.

    browner: "Put the DoEvents outside the if statement"
    That will cause the hangup that Crazy D was talking about

    Serge: "Also, get rid of the Form. And use a module. Create a Sub routine called Main and change the Startup Object to be Sub Main. This way your application won't have any forms, but will run on a background."

    I just don't want to explain this one more time but the form is really neccesary.

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2000
    Location
    Seattle, WA, USA
    Posts
    46
    Thanks,

    Do I need to keep my module? Or can I just put it all in a form?? When I put all my code, in a form before, and turned it invisible it would not execute the code, if I do it this way in a form will it work?


    Thanks,

    Kevin

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Sure thing, Kevdog.

    And for that thing i posted "Do NOT use DOEVENTS with continuous loops" I would add: ", if you don't have the unloading procedure"

  13. #13

    Thread Starter
    Member
    Join Date
    Feb 2000
    Location
    Seattle, WA, USA
    Posts
    46
    Thanks,

    But do I need a module and a form or just a form?? Also, I tried putting all my code in a form, with a the code you gave me and it would not work??


    Thanks,


    Kevin

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