question

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

Go from module to form and return

Hi ,I want call in module a form and then return from form to module,but when I do it, the statements that are in my
module don't execute and the form runs immediately after execution.I want after returning to module the statements
go on execution,but it doesn't happen so.
Here is my code:

Imports System.Management
Imports System.Windows.Forms
Imports System.Runtime
Module module1

 Sub main()
     'Dim f As New Form1
     'Application.Run(New Form1())
     Console.WriteLine("ok")
 End Sub
 Private Declare Function ch341open Lib "ch341dll.dll" (ByVal iindex As Integer) As Integer

End Module
Public Class Form1
Dim WithEvents pluggedInWatcher As ManagementEventWatcher
Dim WithEvents pluggedOutWatcher As ManagementEventWatcher
Dim WithEvents button1 As Button
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
             MessageBox.Show(ex.Message)
         End Try
     End Sub

     Private Sub pluggedInWatcher_EventArrived(sender As Object, e As EventArrivedEventArgs) Handles pluggedInWatcher.EventArrived
         MessageBox.Show("Plugged In")
         Application.Exit()
     End Sub


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

Please guide me.
Thanks

dotnet-visual-basic
· 1
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.

Why do you think that Sub Main will run? You've told us what you did but why are you trying to execute a form from a Module?

0 Votes 0 ·

1 Answer

XingyuZhao-MSFT avatar image
0 Votes"
XingyuZhao-MSFT answered XingyuZhao-MSFT edited

Hi @kavehrahimi-5744 ,
Do you want to call the form asynchronously?
Check:

 Module Module1
     Sub Main()
         Dim t As Task = Task.Run(Sub()
                                      Dim f As New Form1
                                      Application.Run(New Form1())
                                  End Sub)
         Console.WriteLine("ok")
         t.Wait()
     End Sub
 End Module

Best Regards,
Xingyu Zhao


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 7
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.

I want receive the "ok" which is written in 7th 'line console.writeline("ok")' but I don't receive.
I don't know what do you mean by 'call the form asynchronously'.

0 Votes 0 ·

Hi @kavehrahimi-5744 ,
I create a ConsoleApp(.NET Framework 4.8) and then add a windows form(Form1) in my project.
After running the code, I can get the following result:

97761-1.png


0 Votes 0 ·
1.png (73.5 KiB)

I did so but I didn't take results Please explain me how and with which code did you that?

0 Votes 0 ·
Show more comments