1# 自定义函数,添加到注册表
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Public Sub StartRunRegHKLM() 2 3 REM HKEY_LOCAL_MACHINE \ SOFTWARE \ WOW6432Node \ Microsoft \ Windows \ CurrentVersion \ Run 4 5 Dim strName As String = Application.StartupPath + "\" + Application.ProductName + ".exe" 6 7 Dim strNewName As String = System.IO.Path.GetFileNameWithoutExtension(strName) 8 If Not System.IO.File.Exists(strName) Then Return 9 Try 10 Dim Rkey As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", True) 11 If IsNothing(Rkey) Then 12 Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Run") 13 Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注册表,使程序开机时自动执行. 14 Else 15 Rkey.SetValue(strNewName, Chr(34) & strName & Chr(34)) '修改注册表,使程序开机时自动执行。 16 End If 17 Catch ex As Exception 18 MessageBox.Show(ex.Message, "StartRun") ' //MessageBox.Show(ex.Message); 19 End Try 20 End Sub
2# timer 事件中写入处理机制,不笔者限制ACAD程序最多运行2个
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 2 Dim listacad = CheckAppProcess("acad"), proAcad As Process 3 If listacad.Count > 2 Then 4 Me.Timer1.Stop() 5 If MessageBox.Show("进程数量超过服务器的载荷!请排队等候", "AutoCAD 报告", MessageBoxButtons.OK, MessageBoxIcon.Warning) = DialogResult.OK Then 6 For index = listacad.Count - 1 To 2 Step -1 7 proAcad = listacad(index) 8 If Not proAcad.HasExited Then proAcad.Kill() 9 Next 10 Me.Timer1.Start() 11 End If 12 End If 13 End Sub
3# 读取进程的数量
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Public Function CheckAppProcess(appName As String) As List(Of Process) 2 Dim pros As Process() = Process.GetProcesses() 3 Dim processAcad As List(Of Process) = New List(Of Process)() 4 For Each p As Process In pros 5 If p.ProcessName = "acad" Then processAcad.Add(p) 6 Next 7 Return processAcad 8 End Function
4# 最后将相关的启动timer空间的语句写入到程序开始
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 2 Me.Timer1.Interval = 3000 3 Me.Timer1.Start() 4 End Sub
运行开机启动需要提权到管理员权限