Home All Groups Group Topic Archive Search About

Threads and ...CriticalRegion

Author
20 Dec 2006 8:10 PM
Carly
Hi,

I have the following code that uses beginCriticalRegion and
EndCriticalRegion

Imports System.Threading
Public Class Form1
    Shared Sub SimpleWorks(ByVal o As Object)
        Dim info As person = CType(o, person)

        Thread.BeginCriticalRegion()
        Thread.Sleep(100)
        MsgBox(Thread.CurrentThread.ManagedThreadId & " " & info.Name)
       Thread.EndCriticalRegion()
    End Sub
    Dim operation As New ParameterizedThreadStart(AddressOf
SimpleWorks)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

        Dim theThreads1 As New Thread(operation)
        Dim P1 As New person("mimi", " rogers")
        theThreads1.Start(P1)

        Dim P2 As New person("Ina", "Forrester")

        Dim theThreads2 As New Thread(operation)
        theThreads2.Start(P2)


'HERE IS NOT WORKING
        theThreads2.Abort()

        Dim P3 As New person("Dave", "Simpson")
        Dim theThreads3 As New Thread(operation)
        theThreads3.Start(P3)

    End Sub
End Class

I find that theThreads2 is still aborted before displaying the message:
Msgbox.......

Can somebody enlighten me.

Thank you,

Carly

Author
20 Dec 2006 8:33 PM
Gunnar Valur
SHi,


Lets assume thread A is your main thread.  It is creating the other threads
1, 2 & 3.  Now when you do:

theThreads2.Start(P2)

You put thread 2 into ready state.  This however doesn't mean that the
operating system will switch to thread 2 right and thread 2 hasn't really
entered SimpleWorks yet. 

So now you call:

theThreads2.Abort()

And this will abort thread 2 before it even got started.

You can try this:

theThreads2.Start(P2)
Thread.Sleep(20)
theThreads2.Abort()

This should give thread 2 time to enter SimpleWorks and get into the
CriticalRegion

Hope this helps

  Gunnar






Show quoteHide quote
"Carly" wrote:

> Hi,
>
> I have the following code that uses beginCriticalRegion and
> EndCriticalRegion
>
> Imports System.Threading
> Public Class Form1
>     Shared Sub SimpleWorks(ByVal o As Object)
>         Dim info As person = CType(o, person)
>
>         Thread.BeginCriticalRegion()
>         Thread.Sleep(100)
>         MsgBox(Thread.CurrentThread.ManagedThreadId & " " & info.Name)
>        Thread.EndCriticalRegion()
>     End Sub
>     Dim operation As New ParameterizedThreadStart(AddressOf
> SimpleWorks)
>
>     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
>         Dim theThreads1 As New Thread(operation)
>         Dim P1 As New person("mimi", " rogers")
>         theThreads1.Start(P1)
>
>         Dim P2 As New person("Ina", "Forrester")
>
>         Dim theThreads2 As New Thread(operation)
>         theThreads2.Start(P2)
>
>
> 'HERE IS NOT WORKING
>         theThreads2.Abort()
>
>         Dim P3 As New person("Dave", "Simpson")
>         Dim theThreads3 As New Thread(operation)
>         theThreads3.Start(P3)
>
>     End Sub
> End Class
>
> I find that theThreads2 is still aborted before displaying the message:
> Msgbox.......
>
> Can somebody enlighten me.
>
> Thank you,
>
> Carly
>
>