如何在程序关闭后自动重启程序

时间:2014-12-05 22:01:38

标签: vb.net

我正在为我的孩子开发一个计时器,一旦时间到了就会自动关闭计算机,我试图找出一种方法,如果程序通过任务管理器关闭,程序将自动重启。 如果有任何帮助,我已经为我的程序发布了我的代码。

Imports System
Imports System.IO
Imports System.Text
Imports System.Collections.Generic
Public Class Digparent
'add to startupp:
' My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).SetValue(Application.ProductName, Application.ExecutablePath)

'remove from startup
'My.Computer.Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True).DeleteValue(Application.ProductName)

'use application setting boolean to not add same application to startup more than once
'charge for this feature



'to do
'
'wrongn height when make timer unstopable
'above all
Dim X, Y As Integer
Dim NewPoint As New System.Drawing.Point
Public second As Integer
Public checkdone As Boolean
Public checkoff As Boolean
Public unstop As Boolean
Dim Mondayt As String
Dim Tuesdayt As String
Dim Wendsdayt As String
Dim Thursdayt As String
Dim Fridayt As String
Dim Saturdayt As String
Dim Sundayt As String
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' //
    '    //
    '   //
    '  //Start reader
    ' //
    Dim timeinfo As String = "C:\Users\DigiParent\Desktop\Project data\good.txt"
    ' IO.File.SetAttributes("C:\Users\DigiParent\Desktop\Project data\Digitimeinfo.txt", IO.FileAttributes.Hidden)

    Dim timeChecker As New System.IO.StreamWriter(timeinfo, True)
    timeChecker.Close()
    Dim readertime As New System.IO.StreamReader(timeinfo, Encoding.Default)
    Dim texttime As String = readertime.ReadToEnd
    readertime.Close()
    If texttime = "" Then
        Dim timeobjWriter As New System.IO.StreamWriter(timeinfo, True)
        timeobjWriter.Write(",,,,")
        timeobjWriter.Close()
    End If
    Dim startup As String = "C:\Users\DigiParent\Desktop\Project data\good.txt"
    Dim reader As New System.IO.StreamReader(startup, Encoding.Default)
    Dim data As String = reader.ReadToEnd
    Dim aryTextFile(6) As String
    aryTextFile = data.Split(",")
    Mondayt = aryTextFile(0)
    Tuesdayt = aryTextFile(1)
    Wendsdayt = aryTextFile(2)
    Thursdayt = aryTextFile(3)
    Fridayt = aryTextFile(4)
    '
    'enable this for saturday and sunday
    '
    'Saturdayt = aryTextFile(5)
    'Sundayt = aryTextFile(6)
    reader.Close()

    '    //
    '   //
    '  //Finish reader
    ' //

End Sub

Private Sub Panel2_MouseMove(sender As Object, e As MouseEventArgs) Handles Panel2.MouseMove, time.MouseMove, timeup.MouseMove
    If unstop = True Then
        If e.Button = Windows.Forms.MouseButtons.Left Then
            NewPoint = Control.MousePosition
            NewPoint.X -= (X)
            NewPoint.Y -= (Y)
            Me.Location = NewPoint
        End If
    End If
End Sub



Private Sub Panel2_MouseDown(sender As Object, e As MouseEventArgs) Handles Panel2.MouseDown, time.MouseDown, timeup.MouseDown
    If unstop = True Then
        X = Control.MousePosition.X - Me.Location.X
        Y = Control.MousePosition.Y - Me.Location.Y
    End If
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    NumericUpDownhrs.Left -= 40
    NumericUpDownmin.Left -= 40
    NumericUpDownsec.Left -= 29 '25
    Hourstxt.Left -= 40
    Minutestxt.Left -= 30
    secondstxt.Left -= 30
    Panel1.Left -= 30
    RadioButton2.Left -= 30
    RadioButton1.Left -= 30
    Label4.Left -= 30
    Label5.Left -= 30
    Button4.Left -= 30
    time.Left -= 30
    timeup.Left -= 30
    If RadioButton1.Location = RadioButton5.Location Then
        Timer1.Stop()

    Else
    End If
    If Me.Height < 265 Then
        Me.Height = Me.Height + 1
    End If


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    more.Visible = False
    updateb.Visible = False
    feedbackb.Visible = False
    Timer1.Start()
    Button1.Visible = False
End Sub

Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged

End Sub

Private Sub Button4_Click(snder As Object, e As EventArgs) Handles Button4.Click
    My.Settings.Data = True
    If RadioButton6.Checked = True Then
        My.Settings.unstopable = True
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.ShowInTaskbar = False
        Me.ControlBox = False
        Me.Text = ""
        Me.ShowIcon = False
        Me.ShowInTaskbar = False
        Me.ControlBox = False
        unstop = True
        Me.Height = 149
    Else
        My.Settings.unstopable = False
    End If
    If RadioButton1.Checked = True Then
        My.Settings.Shutdown = True
        checkoff = True
        ' System.Diagnostics.Process.Start("ShutDown", "/s")
    Else
        My.Settings.Shutdown = False
    End If
    vhrs = NumericUpDownhrs.Value
    vmin = NumericUpDownmin.Value
    vsec = NumericUpDownsec.Value
    My.Settings.hours = vhrs
    My.Settings.min = vmin
    My.Settings.second = vsec
    PictureBox1.Dock = DockStyle.None
    PictureBox1.Visible = False
    starttime.Start()
    realTimer.Start()
End Sub
Public Hrs As Integer  'number of hours   '
Public Min As Integer  'number of Minutes '
Public Sec As Integer  'number of Sec     '
Public Function GetTime(Time As Integer) As String
    'Seconds'
    Sec = Time Mod 60

    'Minutes'
    Min = ((Time - Sec) / 60) Mod 60

    'Hours'
    Hrs = ((Time - (Sec + (Min * 60))) / 3600) Mod 60
    Return Format(Hrs, "00") & ":" & Format(Min, "00") & ":" & Format(Sec, "00")
End Function
Private Sub realTimer_Tick(sender As Object, e As EventArgs) Handles realTimer.Tick
    second = second + 1
    time.Text = GetTime(second)
    'now
    If Min >= vmin And Hrs >= vhrs And Sec >= vsec Then
        checkdone = True
        Me.TopMost = True
        'Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
        endtime.Start()
        If unstop = True Then
            closeb.Visible = True
        End If
        realTimer.Stop()
    End If
    If checkdone = True And checkoff = True Then

        endtime.Start()
        System.Diagnostics.Process.Start("ShutDown", "/s")
    End If
End Sub

Private Sub starttime_Tick(sender As Object, e As EventArgs) Handles starttime.Tick
    time.Left -= 30
    Panel1.Left -= 30
    RadioButton2.Left -= 30
    RadioButton1.Left -= 30
    Label4.Left -= 30
    Label5.Left -= 30
    Button4.Left -= 30
    timeup.Left -= 30
    If time.Location = Label2.Location Then
        starttime.Stop()
    End If
    If Me.Height > 189 Then
        Me.Height = Me.Height - 5
    End If
End Sub


Private Sub endtime_Tick(sender As Object, e As EventArgs) Handles endtime.Tick
    time.Left -= 30
    timeup.Left -= 30
    If timeup.Location = labeltimeup.Location Then

        endtime.Stop()
    End If
End Sub

Private Sub more_Click(sender As Object, e As EventArgs) Handles more.Click
    Form3.Show()
    'more.Visible = False
    'moretimer.Start()
End Sub

Private Sub moretimer_Tick(sender As Object, e As EventArgs) Handles moretimer.Tick
    If updateb.Location = Updatebutton.Location Then
        moretimer.Stop()
    End If
    feedbackb.Left += 15
    updateb.Left -= 15
End Sub

Private Sub updateb_Click(sender As Object, e As EventArgs) Handles updateb.Click
    System.Diagnostics.Process.Start("http://digiparent.weebly.com/beta-20-update.html")
End Sub

Private Sub feedbackb_Click(sender As Object, e As EventArgs) Handles feedbackb.Click
    System.Diagnostics.Process.Start("http://digiparent.weebly.com/feedback.html")
End Sub


Private Sub closeb_Click(sender As Object, e As EventArgs) Handles closeb.Click
    Me.Close()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    NumericUpDownsec.Value = My.Settings.second
    NumericUpDownmin.Value = My.Settings.min
    NumericUpDownhrs.Value = My.Settings.hours
    If My.Settings.Shutdown = True Then
        RadioButton1.Checked = True
    End If
    If My.Settings.unstopable = True Then
        RadioButton6.Checked = True
    End If
    Button2.Visible = False
End Sub

Private Sub Numericchanged(sender As Object, e As EventArgs) Handles NumericUpDownsec.ValueChanged, NumericUpDownmin.ValueChanged, NumericUpDownhrs.ValueChanged
    If NumericUpDownsec.Value = 0 Then ' NumericUpDownhrs.Value = 0  NumericUpDownmin.Value = 0 Then

        If NumericUpDownhrs.Value = 0 Then
            If NumericUpDownmin.Value = 0 Then

                Button2.Visible = True
            Else
                Button2.Visible = False
            End If

        Else
            Button2.Visible = False
        End If
    Else
        Button2.Visible = False
    End If




End Sub

Private Sub Label4_Click(sender As Object, e As EventArgs) Handles Label4.Click

End Sub



End Class

2 个答案:

答案 0 :(得分:2)

这是一款非常基本的无窗口看门狗应用程序。

从标准的WinForms项目开始。 添加模块。 添加Public Sub Main到它。 转到项目 - &gt;属性 - &gt; “应用程序”选项卡,以及取消选中“启用应用程序框架”框。 在此之上,将“启动对象:”下拉列表从“Form1”更改为“Sub Main”。

代码......

Module Module1

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

End Module

Public Class Watchdog
    Inherits ApplicationContext

    Private AppToWatch As String
    Private FullPath As String = "C:\WINDOWS\system32\calc.exe"

    Private WithEvents P As Process

    Public Sub New()
        AppToWatch = System.IO.Path.GetFileNameWithoutExtension(FullPath)
        Dim PS() As Process = Process.GetProcessesByName(AppToWatch)
        If PS.Length = 0 Then
            StartIt()
        Else
            P = PS(0)
            P.EnableRaisingEvents = True
        End If
    End Sub

    Private Sub P_Exited(sender As Object, e As EventArgs) Handles P.Exited
        StartIt()
    End Sub

    Private Sub StartIt()
        P = Process.Start(FullPath)
        P.EnableRaisingEvents = True
    End Sub

End Class

答案 1 :(得分:1)

将程序编译为服务,并将其配置为自动启动。