【发布时间】:2021-07-12 09:00:33
【问题描述】:
所以我有一个程序,我想在 Windows 的通知托盘中最小化它。如果我从开始菜单手动启动它,我有程序可以做到这一点,但是我遇到了一个问题,如果我尝试使用任务计划程序启动它,它将不会启动。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists("enabled") Then
Me.Hide()
Me.WindowState = FormWindowState.Minimized
End If
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
NotifyIcon1.Icon = SystemIcons.Application
NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
NotifyIcon1.BalloonTipTitle = "Title"
NotifyIcon1.BalloonTipText = "Text"
NotifyIcon1.ShowBalloonTip(50000)
'Me.Hide()
ShowInTaskbar = False
End If
Private Sub enable_Click(sender As Object, e As EventArgs) Handles enable.Click
Using tService As New TaskService()
Dim tDefinition As TaskDefinition = tService.NewTask
tDefinition.RegistrationInfo.Description =
"Test description"
'LogonTrigger'
Dim tLogon As New LogonTrigger()
tLogon.UserId = SystemInformation.UserName
tDefinition.Triggers.Add(tLogon)
tDefinition.Actions.Add(New ExecAction(exePath))
tService.RootFolder.RegisterTaskDefinition("Test",
tDefinition)
End Using
Dim fs As FileStream = File.Create("enabled")
fs.Close()
End Sub
这些是程序启动时使用的代码行,用于安排程序在用户登录时启动。对于任务调度,我使用了 dahall 的 Task Scheduler .NET 包装器,您可以在此处找到:https://github.com/dahall/taskscheduler
【问题讨论】:
标签: .net windows vb.net scheduled-tasks