在Vb.net中,我想定期显示我的主表单和其他表单。 最后一个表单检查操作并自动关闭。 应该实现线程,但我不确定它是否可能......
我的代码是空的..
Imports System
Imports System.Windows.Forms
Friend NotInheritable Class Program
Private Sub New()
End Sub
<STAThread() _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Application.Run (New formMain()) '--> Main Form
'All code here is not execute until Main Form is closed
End Sub
End Class
提前致谢。
编辑:对不起,我是vb.net的新手,我想说显示我的主要表单。
编辑2:我想我没解释得很好,但在@JeremyThompson的帮助下,我可以完成我的代码。
FormMain:现在,当我尝试关闭此表单时,我会显示另一个表单来执行检查操作,但我认为(FormMain)始终可见。
Private Sub FormMain_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles FormMain.FormClosing
Me.Visible = True
Dim anotherForm as New CheckOperationsForm()
anotherForm.Show()
e.Cancel = True
End Sub
其他形式(或FormCheckOperations()):当以这种形式编码的例程完成时,我建立一个布尔值_successfulChecking的值,所以如果这个布尔值为真,则主窗体继续显示,在另一种情况下,应用程序结束
Private Sub FormCheckOperations_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles FormCheckOperations.FormClosing
If Not _succesfulChecking Then
End 'Close application
End If
End Sub
我的疑问是,我如何定期从MainForm显示FormCheckOperation(我可以调用FormMain.Close()来执行此操作)或者如何从Main Sub中显示?
编辑3:现在,在我当前的方法中,我在Main Sub中打开2个线程,第一个使用Main Form,第二个使用另一个线程,每60秒打开一次CheckOperations Forms。但是当在Visual Studio中执行时,表单仍然位于SDK的“后面”,而且它们不能正常工作,但我认为最后的方法应该更接近。
Imports System
Imports System.Windows.Forms
Friend NotInheritable Class Program
Private Sub New()
End Sub
<STAThread() _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Dim threadFormMain As New Thread (AddressOf launchFrmMain)
Dim threadFormCheckOperations As New Thread (AddressOf launchThreadFrmCheckOperations)
threadFormMain.Start()
threadFormCheckOperations.Start()
End Sub
Public Shared Sub launchFrmMain()
Application.Run(New FormMain()
End Sub
Public Shared Sub launchThreadFrmCheckOperations()
While(True)
Dim threadForm As New Thread(AddressOf launchFrmCheckOperations)
threadForm.Start()
'Here I should stop while loop during 60 secs…
'Thread.Sleep(60000)
End While
End Sub
Public Shared Sub launchFrmCheckOperations()
Application.Run(New FormCheckOperations()
End Sub
End Class
答案 0 :(得分:1)
在FormMain_Closing事件中,设置Visible = False,实例化并显示另一个表单并设置e.cancel = True,然后将另一个表格的结束事件中的代码放到应用程序的End
。
例如:
Private Sub FormMain_FormClosing(sender as Object, e as FormClosingEventArgs) _
Handles FormMain.FormClosing
Me.Visible = False
Dim anotherForm as New AnotherForm()
anotherForm.Show()
e.Cancel = True
End Sub
答案 1 :(得分:0)
最后我解决了这个问题。 在Main Sub中首先启动一个带有检查操作表单的线程(全局声明),然后在主线程中启动mainForm。 带有检查操作的线程包含while循环,它在每次迭代中创建一个新线程,但Thread.Join()保持等待前一个线程的结束,所以它避免在打开线程时创建其他线程。
代码如下:
Imports System
Imports System.Windows.Forms
Friend NotInheritable Class Program
Private Sub New()
End Sub
<STAThread() _
Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
threadFormCheckOperations.Start()
Application.Run(New FormMain())
End Sub
Public Shared threadFormCheckOperations As New Thread(AddressOf launchThreadFrmCheckOperations)
Public Shared Sub launchThreadFrmCheckOperations()
While(True)
Dim threadForm As New Thread(AddressOf launchFrmCheckOperations)
threadForm.Start()
threadForm.Join() '---> Wait until thread is closed
Thread.Sleep(60000)
End While
End Sub
Public Shared Sub launchFrmCheckOperations()
Application.Run(New FormCheckOperations()
End Sub
End Class