question

kavehrahimi-5744 avatar image
0 Votes"
kavehrahimi-5744 asked XingyuZhao-MSFT commented

Problem with form execution

Hi ,when I have a form class in my code the other lines except 'public class form1' don't execute.
I don't know why it's so.for example my code is:
Imports System.Management
Imports Microsoft.Win32
Imports System.Windows.Forms

Public Class mainclass
Public Shared Sub dosomething()
Task.Run(Sub() updateform())
End Sub
Public Delegate Sub formdelegate()
Public Shared Sub updateform()

     Application.Run(Form1)
 End Sub

 Private Declare Function ch341open Lib "ch341dll.dll" (ByVal iindex As Integer) As Integer

End Class

Module module1
Sub main()
Application.Run(New Form1)
Console.WriteLine("ok")
End Sub
End Module
Public Class Form1
Dim WithEvents pluggedInWatcher As ManagementEventWatcher
Dim WithEvents pluggedOutWatcher As ManagementEventWatcher
Dim pluggedInQuery As WqlEventQuery
Dim pluggedOutQuery As WqlEventQuery

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
         Try
             pluggedInQuery = New WqlEventQuery
             pluggedInQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"
             pluggedInWatcher = New ManagementEventWatcher(pluggedInQuery)
             pluggedInWatcher.Start()

             pluggedOutQuery = New WqlEventQuery
             pluggedOutQuery.QueryString = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 3"
             pluggedOutWatcher = New ManagementEventWatcher(pluggedOutQuery)
             pluggedOutWatcher.Start()
         Catch ex As Exception
             MsgBox(ex.Message)
         End Try
     End Sub

     Private Sub pluggedInWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedInWatcher.EventArrived
         MsgBox("Plugged In")
     End Sub

     Private Sub pluggedOutWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedOutWatcher.EventArrived
         MsgBox("Plugged Out")
     End Sub
 End Class

Please help
Thanks

dotnet-visual-basic
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @kavehrahimi-5744 ,
Every Visual Basic application must contain a procedure called Main. This procedure serves as the starting point and overall control for your application.
Check: Main Procedure in Visual Basic


0 Votes 0 ·

Please can you discuss your meaning by example.I see sub main in my code.where do I have to use it?

0 Votes 0 ·

Hi @kavehrahimi-5744 ,
Main method is the entry point of a program, and only the method called in the main method will be executed.
In your Main method, you only call 'Application.Run(New Form1)', so other lines except 'public class form1' don't execute.

0 Votes 0 ·

0 Answers