Vb.net 定期执行其他表单

Vb.net 定期执行其他表单,vb.net,winforms,.net-4.5,Vb.net,Winforms,.net 4.5,在Vb.net中,我希望定期显示我的主窗体和其他窗体。 最后一个窗体执行检查操作并自动关闭。 应该实现线程,但我不确定是否可能 我的代码是空的 Imports System Imports System.Windows.Forms Friend NotInheritable Class Program Private Sub New() End Sub <STAThread() _ Shared Sub Main() Applicati

在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
端接头

其他表单(或FormCheckOperations()):当在此表单中编码的例程完成时,我将建立一个布尔值_successfulChecking,因此如果此布尔值为true,则主表单将继续显示,在另一种情况下,应用程序将结束

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中打开了两个线程,第一个线程使用主窗体,第二个线程使用另一个线程,该线程每60秒打开一次CheckOperations窗体。但是当在VisualStudio中执行时,表单会“落后于”SDK,而且它们不能正常工作,但我认为最终的方法应该更接近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
导入系统
导入System.Windows.Forms
友元不可继承类程序
私人分新
端接头

在FormMain_关闭事件中,设置Visible=False,实例化并显示另一个表单,并设置e.cancel=True,然后将代码放入另一个表单的关闭事件中,以结束应用程序

例如:

最后我解决了这个问题。 在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
导入系统
导入System.Windows.Forms
友元不可继承类程序
私人分新
端接头

executemyform
是否意味着显示它?…因此从主窗体定期显示另一个窗体(可能使用计时器)?在这里你到底需要什么帮助?但是当我显示demain表单时,主线程保持锁定…我想知道如何才能做到这一点。谢谢关于编辑2,除了FormMain的关闭事件外,定期显示FormCheckOperation的其他条件是什么?真的,我不明白你的意思。现在,我正在从Sub Main(在我的程序.vb中)搜索解决方案。这不是一个好的编程,我描述的方法更适合Orhadox。我没有投你的反对票,但有了这个答案,我无法证明我对你的问题投了赞成票。
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
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