[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 581
  • Last Modified:

Need Help Shutting Down A .NET Windows Application Using A Timer

Hello,

I am working on a VB.net program using Visual Studio 2008 and the .NET 3.5 Framework.

I have declared a timer object at the module level in the module containing my startup subroutine.  The declaration is: Private WithEvents runmax As New Timer

In the startup module, I have the line: runmax.Interval = 300000
This is so I get a tick every 5 minutes.

I have code in the tick handler for runmax as follows:

Dim currentTime As TimeSpan = DateTime.Now.TimeOfDay
Dim shutdownTime As TimeSpan = New TimeSpan(7, 0, 0)
If currentTime > shutdownTime Then
'Need terminate code here
End If

The system is form-less, so me.close() cannot be used to end the program after five hours of runtime.  So, the question is: what do I put where it says "Need terminate code here"?  The idea is that the run should end at 7:00 AM.

Windows Scheduler on Win 2008 R2 does have a maximum runtime setting, but it doesn't actually stop the run (i.e., there is a bug in Windows Scheduler on Win 2008).  So I need to terminate the run after five hours of runtime.  The scheduler properly starts the run at 2:00 AM as specified, but as stated the scheduler's maximum runtime setting does nothing.

Thank you,
Stewart Engelman
0
stuengelman
Asked:
stuengelman
  • 9
  • 4
  • 2
  • +1
2 Solutions
 
käµfm³d 👽Commented:
Well since it's VB, you could use the End keyword (provided you haven't spawned any threads). Probably not very .NET-centric, but should work.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"The system is form-less"

What type of project did you start with?

How are you keeping the app "alive"?
0
 
stuengelmanAuthor Commented:
Hi,

I put the "end" statement in the startup subroutine, and it did stop the program.

To test the timer's click handler, I set the Interval to 60000 (one minute), and the system did not stop after one minute.  I then added a msgbox to the tick handler for the timer, and got no msgbox after more than one minute.  It appears the tick handler isn't being thrown.  Here's the code:

Private Sub runmax_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles runmax.Tick
    Dim currentTime As TimeSpan = DateTime.Now.TimeOfDay
    Dim shutdownTime As TimeSpan = New TimeSpan(7, 0, 0)
    MsgBox(currentTime.ToString & " - " & shutdownTime.ToString)
    If currentTime > shutdownTime Then
        End
    End If
End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Éric MoreauSenior .Net ConsultantCommented:
Application.Exit can also be used
0
 
stuengelmanAuthor Commented:
Hi Idle,

The system is a Windows PC application.  The startup object is Sub Main() within a standard module.

Stu
0
 
stuengelmanAuthor Commented:
Hi Everyone,

I made a few code changes and now have the timer ticks being handled.

I also checked to make certain that (currentTime > shutdownTime) = true.

Application.Exit is not stopping the program though.

I'll try "end" again.
0
 
stuengelmanAuthor Commented:
OK, "end" stops the program.

One strange thing: Task Manager does not rid itself of the pgmname.vshost.exe process.

I can't even remove it using "End Process" within Task Manager.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sub Main().  Cool.   But how are you keeping the app from exiting?
0
 
stuengelmanAuthor Commented:
Hi Idle,

The application exits (the startup entity Sub Main() ends) after it finishes reading several data files and writes their records to a MySQL DB.

I put the timer logic in to terminate it midstream if the runtime goes over 5 hours.

Stu
0
 
stuengelmanAuthor Commented:
I found a way to remove the pgmname.vshost.exe process from RAM.  It appears that if pgmname is the startup program, it gets thrown in core without actually starting it.  If I switch to an ASP.NET program within the Solution as the startup program, the PC application gets released from the Task Manager process list.  Is this to be expected for .NET Windows PC applications?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could also kill it with:

    Process.GetCurrentProcess.Kill()
0
 
käµfm³d 👽Commented:
One strange thing: Task Manager does not rid itself of the pgmname.vshost.exe process.
You're running it from the debugger (i.e. from within Visual Studio). This behavior is expected. When you build your project for release, there will be no *.vshost.exe program.
0
 
stuengelmanAuthor Commented:
Idle - thanks, but at this point "End" appears to be working.

Kaufmed - I'll test again in release mode to see that the process is released.
0
 
stuengelmanAuthor Commented:
Beautiful - everything works perfectly now.

Thanks so much to all of you.
0
 
stuengelmanAuthor Commented:
Expert provided an important part of the solution, and also diagnosed some process release issues for me which were very helpful.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For future apps like this, you might want to consider using an ApplicationContext() instead:
Module Module1

    Public Sub Main()
        Application.Run(New MyApp)
    End Sub

End Module

Public Class MyApp
    Inherits ApplicationContext

    Private WorkingThread As New System.Threading.Thread(AddressOf Worker)
    Private SleepingThread As New System.Threading.Thread(AddressOf Sleeper)

    Public Sub New()
        SleepingThread.IsBackground = True ' Allow it to be killed automatically when app shuts down "normally"
        SleepingThread.Start()

        WorkingThread.IsBackground = True ' Allow it to be killed prematurely from the "sleeper thread"
        WorkingThread.Start()
    End Sub

    Private Sub Worker()
        ' let's do 30 seconds worth of work (the sleeper below will kill after 15 seconds though)
        For i As Integer = 1 To 30
            System.Threading.Thread.Sleep(1000)
            Debug.Print(i)
        Next

        ' When you're done, call Application.Exit()
        Application.Exit() ' sleeper thread below will be terminated automatically
    End Sub

    Private Sub Sleeper()
        ' Kill the app in 5 hours if it hasn't already shut down:
        ' System.Threading.Thread.Sleep(TimeSpan.FromHours(5).TotalMilliseconds)

        ' for demonstration, kill app after 15 seconds
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(15).TotalMilliseconds)
        Application.Exit()
    End Sub

End Class

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now