Need some help with a suspend and resume question please :)

TechTeach

New member
Joined
Jan 4, 2013
Messages
4
Programming Experience
Beginner
I am currently teaching a vb.net programming class and I am totally new to the VB Net world. I have a student who completed a suspend and resume assignment in the delegates and events unit. I've copied the work below. He has the threads working so they suspend and resume in the proper order but he wants to have the program print "It works!" at the end.

The question is why does the program print "It works!" before the threads even though the command for writeline is at the end of the program? Thank you all for any help you can give :)

[FONT=Courier New, monospace]ModuleModule1

[FONT=Courier New, monospace]Publicthing1 As System.Threading.Thread = New Threading.Thread(AddressOfecho1)[/FONT]
[FONT=Courier New, monospace]Publicthing2 As System.Threading.Thread = New Threading.Thread(AddressOfecho2)[/FONT]


[FONT=Courier New, monospace]Subecho1()[/FONT]
[FONT=Courier New, monospace]Dimlooped As Integer[/FONT]
[FONT=Courier New, monospace]Forlooped = 1 To 10[/FONT]
[FONT=Courier New, monospace]Iflooped = 5 Then[/FONT]
[FONT=Courier New, monospace]thing2.start()[/FONT]
[FONT=Courier New, monospace]Ifthing1.threadstate = Threading.ThreadState.Running Then[/FONT]
[FONT=Courier New, monospace]thing1.Suspend()[/FONT]
[FONT=Courier New, monospace]EndIf[/FONT]
[FONT=Courier New, monospace]EndIf[/FONT]
[FONT=Courier New, monospace]Console.WriteLine("Thiswill show up first and last, but not in the middle.")[/FONT]
[FONT=Courier New, monospace]Next[/FONT]
[FONT=Courier New, monospace]EndSub[/FONT]
[FONT=Courier New, monospace]Subecho2()[/FONT]
[FONT=Courier New, monospace]Dimlooped As Integer[/FONT]
[FONT=Courier New, monospace]Forlooped = 0 To 5[/FONT]
[FONT=Courier New, monospace]Console.WriteLine("Thisshold be in the middle.")[/FONT]
[FONT=Courier New, monospace]Next[/FONT]
[FONT=Courier New, monospace]Ifthing1.threadstate = Threading.ThreadState.Suspended Then[/FONT]
[FONT=Courier New, monospace]thing1.resume()[/FONT]
[FONT=Courier New, monospace]EndIf[/FONT]
[FONT=Courier New, monospace]EndSub[/FONT]
[FONT=Courier New, monospace]SubMain()[/FONT]
[FONT=Courier New, monospace]thing1.Start()[/FONT]
[FONT=Courier New, monospace] Console.WriteLine(“ItWorks!”)[/FONT]
[FONT=Courier New, monospace]Console.ReadLine()[/FONT]
[FONT=Courier New, monospace]EndSub[/FONT]


[FONT=Courier New, monospace]EndModule[/FONT]
[/FONT]
 
Hi,

To help you understand better what is going on here:-

If you were NOT using multi-threading and you ran through a series of loops like this which were writing to the screen, then you can imagine that as having a SINGLE pen in your hand and writing the statements in the loops onto a piece of paper one at a time. You therefore end up with a list of statements in the order you would expect.

When using multi-Threading, as in this example, you can imagine this as having ONE Pen in your Right hand and ONE Pen in your Left hand. If you then said "GO", the first Pen to reach the paper, or the screen as in this case, gets to write it's statement first. This is why you will find the "It Works!" statement sometimes comes first or midway in the list of statements.

To be specific then, your Sub Main() is running on the Main Thread and your Echo1 and Echo2 subroutines run separately on additional Threads, but at any one point in time in the program there are always TWO Threads (Pens) writing to the screen at any one point in time.

To fix this then you would need to put the last two lines of code in Sub Main(), being, Console.WriteLine("It Works!") and Console.ReadLine() at the end of the last executing thread. That being at the end of the Echo1 subroutine.

Hope that helps to explain this for you.

Cheers,

Ian
 
Since you're a teacher, please teach your students to read the documentation, and read them yourself also. Thread.Suspend/Resume should not be used for thread synchronization, since .Net 2.0 these methods are marked as obsolete and should no longer be used at all. For thread synchronization you could here use AutoResetEvent class.
 
Back
Top