question

IvanLadcani-8754 avatar image
0 Votes"
IvanLadcani-8754 asked IvanLadcani-8754 commented

VB Multithread code: unable to turn a form application( button trigger )into a console run for scheduling run

Dear,
I have a working Module2 below, it worked when I use a button in a form to trigger the module to run 'Call Module2.Main()'.
But then when I tried to change it to run as a schedule, as usual, I change the Startup Object to use the 'Sub Main' of Module1 in Project Property. It ran but when the code reaches
the two lines:
tasks.Add(Task.Run(AddressOf NonCriminal))
tasks.Add(Task.Run(AddressOf Criminal))
, it just ran over without triggering the functions.
I have also tried compiling the project as a Console by changing the Application type, same result. Program ran, but the functions did not get triggered to run.

I know that if I don't use the ' Async Function Main() As Task' with 'Await Task.WhenAll(tasks)' approach, the scheduling will work. But I need to trace the complete time on those multithread tasks as part of my monitoring routine. Hope anyone could help to make this work.
Thanks.
Martin






Imports System.IO
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Threading
Imports System.Threading.Tasks

Module Module1
Sub Main()
Call Module2.Main()
End Sub

End Module


Module Module2
Public MIDX_Civil_Count As Integer
Public MIDX_Criminal_Count As Integer


 Async Function Main() As Task
     Dim i As Integer
     Dim i2 As Integer


         'Before starting the tasks, this section to log the start date and time worked here
         '...
         '...
            

     Dim tasks As New List(Of Task)()
     tasks.Add(Task.Run(AddressOf NonCriminal))
     tasks.Add(Task.Run(AddressOf Criminal))
     Await Task.WhenAll(tasks)


     ' When the tasks above end, this section to log the end date and time also worked here
     '...
     '...
        
     End


 End Function

 Private Async Function NonCriminal() As Task
     Await Task.Run(Sub()

     Try
             '...
             Catch ex As Exception
     '...
     End Try
        
                                 End Sub)
        
 End Function

 Private Async Function Criminal() As Task
        
     Await Task.Run(Sub()
                        Try
                                                     '...
                        Catch ex As Exception
                               '...
                        End Try
                      End Sub)

 End Function

End Module

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

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered IvanLadcani-8754 commented

@IvanLadcani-8754, based on my test, I reproduced your problem.

Please refer to the following link to run the code when you create a new module to run the Call Module2.Main().

First, Please access these settings via Project Properties > Application.

Second, Please un-check the Enable application framework check box and then select that you want to use Sub Main as your Startup object.

Third, Please try to change the application type from winform to console.
130968-image.png

Finally, Please use the following modified code to run the code.

 Module Module1
     Sub Main()
         Call Module2.Main()
         Console.WriteLine("hello,world")
         Console.ReadKey()
     End Sub
 End Module
    
 Module Module2
    
     Public MIDX_Civil_Count As Integer
     Public MIDX_Criminal_Count As Integer
    
     Async Function Main() As Task
         Dim tasks As New List(Of Task)()
         tasks.Add(Task.Run(AddressOf NonCriminal))
         tasks.Add(Task.Run(AddressOf Criminal))
         Await Task.WhenAll(tasks)
          
         End
     End Function
     Private Async Function NonCriminal() As Task
         Await Task.Run(Sub()
                            Try
                                MessageBox.Show("NonCriminal")
                           
    
                            Catch ex As Exception
                                Console.WriteLine(ex.ToString())
                            End Try
    
                        End Sub)
    
     End Function
     Private Async Function Criminal() As Task
    
         Await Task.Run(Sub()
                            Try
                                MessageBox.Show("Criminal")
                  
                            Catch ex As Exception
                                Console.WriteLine(ex.ToString())
                            End Try
                        End Sub)
     End Function
    
 End Module


Besides, I recommend that you use MessageBox.Show to check if the method is triggered or not. And Console.Readkey is necessary for the app. Because the app will execute will fast without the Console.Readkey().

Result:

131005-3333.gif







If the response 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.






image.png (27.7 KiB)
3333.gif (125.1 KiB)
· 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.

TThank you so much Jack. I appreciated your effort and help. It worked after I made the changes from your recommendations. Martin

0 Votes 0 ·