Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

MultiThreading Problem

$
0
0
Hi to all

I have a problem that's driving me crazy and i would be very appreciated if someone could shed some light.
I must add that im very inexperienced in MultiThreading.
I have a Sub with arguments that i want to run multiple times with different arguments on different threads like this:

Code:

    Private Sub StartApplication()
        Dim i As Integer, MyArguments As Object
        Dim NewThread As WaitCallback = New WaitCallback(AddressOf MySub)
        For i = 1 To 100
            MyArguments = i
            ThreadPool.QueueUserWorkItem(MySub, MyArguments)
        Next
    End Sub

This runs fine, it creates the threads and the sub MySub starts to run the code.
Then this is the MySub:

Code:

    Private Sub MySub(ByVal MyArguments As Object)
        'Do Some Stuff
        RegisterInDataBase(Argument1, Argument2)
    End Sub
    Private Sub RegisterInDataBase(ByVal Argument1 As Object, ByVal Argument2 As Object)

    End Sub

The MySub calls some functions outside the procedure, the RegisterInDataBase for example.
My problem is that whenever the code reaches the RegisterInDataBase function it stops.
I don't know how to debug a thread in VS 2010 Express so i have putted Debug.Print lines to check where it stops working and i have discovered that whenever i call a function on the MySub the code just stops and the thread seems to die.

I don't know if im missing something fundamental and this is the expected behavior or if it's something specific from my code (the lines i have posted follow the logic of the code but are just examples).

I have tried the same thing with a code like this:

Code:

  Private Sub StartApplication2()
        Dim TryThis As Thread, MyArguments As Object
        TryThis = New Thread(AddressOf MySub)
        TryThis.Start(MyArguments)
    End Sub

With this code, without using a ThreadPool, the code doesn't even start.
I don't know what im missing. Any help is much appreciated. Thanks

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>