CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33
  1. #1
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Still my program is running :(

    Hai pals,

    Even after executing the code this code, my application exe is still running in the task manager?

    Code:
    Option Explicit
    
    Private Sub cmdExit_Click()
     Dim frm As Form
     For Each frm In Forms
       If frm.Name <> Me.Name Then ' Unload this form LAST
         Unload frm
         Set frm = Nothing
       End If
     Next
     Unload Me
    End Sub

    what can be the problem?
    "Don't Bring A Knife To A Gun Fight"

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Still my program is running :(

    Leaving OBJECTS open (not set to NOTHING explicitly)

    Calling ANY form property after that code.

    We'd have to see more...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: Still my program is running :(

    Quote Originally Posted by dglienna View Post
    Leaving OBJECTS open (not set to NOTHING explicitly)

    Calling ANY form property after that code.

    We'd have to see more...
    ahaaa, let me chk.
    "Don't Bring A Knife To A Gun Fight"

  4. #4
    Join Date
    Dec 2008
    Posts
    19

    Re: Still my program is running :(

    Try moving the code to the Unload Event instead of from a command button.

  5. #5
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: Still my program is running :(

    no tom,
    it just the system tray program exit menu item. btw, i am keep chcking for any objects not destroyed.
    "Don't Bring A Knife To A Gun Fight"

  6. #6
    Join Date
    Nov 2002
    Posts
    278

    Re: Still my program is running :(

    If you don't mind being a little messy . . . Put an "End" Statement after your loop. That oughta shut her down.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Still my program is running :(

    That's WRONG. END should NEVER be used. It is old, from the DOS days, when full-screen apps wouldn't shut down. END still leaves open files open.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Still my program is running :(

    I would not say that end should never be used. There is no problem using it but one should clean up the proper way before hand even if the End statement is used. End will terminate the app and in most cases there are no adverse side effects. Now of course if you leave files open and dlls initialized there could possibly be an issue but even then in most cases they are terminated by the end statement pending data will likely be lost but that is about it.

    As for why the app is not shutting down my guess would be due to code located in a module or class that is part of your project. The end statement should terminate the process but would be better to look for the real problem.

    I woudl check modules,classes, the unload event in each form, check the form area declarations for vars defined as public which may be accessed in a module and things such as this.

  9. #9
    Join Date
    Jul 2007
    Location
    in your pocket
    Posts
    339

    Re: Still my program is running :(

    very useful information i got !
    "Don't Bring A Knife To A Gun Fight"

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Still my program is running :(

    Code:
    Option Explicit
    
    Public xlAppTemp As Excel.Application
    Public xlWorkBook As Excel.Workbook
    Public xlSheet As Excel.Worksheet
    Dim strDate$
    
    Public Sub GenerateReport()
      On Error GoTo ErrHandler
      
      ' Creating Object for Excel File.....
      Set xlAppTemp = New Excel.Application
      
      ' Making it Invisible and non-Interactive.....
      xlAppTemp.Visible = False
      xlAppTemp.DisplayAlerts = False
        ' Opening Template Excel File.....
      Set xlWorkBook = xlAppTemp.Workbooks.Open(App.Path & "\Book1.xls", , False)
      Set xlSheet = xlWorkBook.Sheets(1)
    
      ' Making Active to Worksheet 1.....
      xlSheet.Activate
    '          END  '  Let's put END here.  Think EXCEL will end also?
      ' I am doing lot of things in it, but to provide you with example
      xlSheet.Cells(15, 1) = "This is my report 1"
    
      ' Formating Date to attach with new file name.....
      strDate = Format(Date, "yyyy-mm-dd")
      
      ' Saving excel file with new name on different folder.....
      xlWorkBook.SaveAs App.Path & "\Output" & strDate & ".xls"
    
    Cleanup:
      ' Destroying Objects.....
      Set xlSheet = Nothing
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
      Exit Sub
    ErrHandler:
    'I presume this section comes after ErrHandler, in which case you will want to close the workbook without changes.
    '(save happens just above if no error occurs)
      xlWorkBook.Close SaveChanges:=False
      Set xlWorkBook = Nothing
    
    'The Visible and DisplayAlerts settings should be reset, as they can affect 'manual' use of Excel too.
      xlAppTemp.Visible = True
      xlAppTemp.DisplayAlerts = True
    
      xlAppTemp.Quit
      Set xlAppTemp = Nothing
    End Sub
    
    Private Sub Command1_Click()
      Call GenerateReport
      Beep
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Still my program is running :(

    xlSheet.Activate
    ' END ' Let's put END here. Think EXCEL will end also?
    No. I would say that it will not. but then here you are activating another program and should not expect the end statement to terminate the external executable(s).

    That is not to say that end does not have its uses. As with all tools they are good for some things and not for others. One must no where and when to use them and what to expect when they are used.

    In this case the program that you have written should terminate completely but will leave Excel running as should be expected and in rare cases maybe even the desired effect.

    That said I rarely use such coding practice. I do not like the idea of forcing my users to purchase excel or other third party programs to use my software. It has its place but you also must realize there are special requirements when you use these techniques that are outside of the VB environment.

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Still my program is running :(

    But, called enough times in a row is a guaranteed way to run out of memory.

    An instance of Excel is created, and it's under program control to terminate it properly. That is the ONLY way to release the memory it's using.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Dec 2008
    Posts
    19

    Re: Still my program is running :(

    You say "it just the system tray program exit menu item. btw, i am keep chcking for any objects not destroyed."

    Do you mean you are exiting the program from a pop-up menu from the system tray? If that is the case you still should put the unload forms code in the Unload Event. Try it.

    I apologize if I misunderstood.

  14. #14
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Still my program is running :(

    Quote Originally Posted by dglienna View Post
    But, called enough times in a row is a guaranteed way to run out of memory.

    An instance of Excel is created, and it's under program control to terminate it properly. That is the ONLY way to release the memory it's using.
    Yes of course, but this is not the purpose of the end statement. There is a specific statement to terminate the external instance of the other program you have started in this case. In other cases there may be no direct method to shut down the other program. Shell to a dos program for example.

    BTW one can always simply close excel manually at any point.

    The point is that just because the End statement does not terminate external programs launched by your code does not mean that it should never be used. That's kinda like saying one should never use the unload function because it does not unload the entire app or not use the print function because it does not cause a word document to print on a different pc. Or you could say that one should never use the excel functions or word or access because they don't terminate when your application is closed.

    The fact is the End does exactly what it should. It ends your program execution. It is up to the programmer to make sure that the program is ready to be ended at this point but the VB environment does handle many oversights by the programmer as well as many bad programming techniques to result in a clean exit in most cases. It was never intended to end other programs.

  15. #15
    Join Date
    Dec 2001
    Posts
    6,332

    Re: Still my program is running :(

    I had one app which would terminate just fine on most machines, but not all. It was a simple app, with only one form. After exhausting all options, I used the End statement after Unload Me, and that solved it. I think that was with winME though, but it just shows that it is possible to have an app not fully unload even when you're code is perfectly fine.

    So if you're properly unloading everything, I see nothing wrong with using the End statement. It's just one of those things which we normally wouldn't need, sorta like the Call, Let, and GoTo statements.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Page 1 of 3 123 LastLast

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

Featured