Stable Thread Timing?

  • Thread starter Thread starter gregory_may
  • Start date Start date
G

gregory_may

I have an application where I am using a System Thread to capture the screen
& Broadcast it to clients. Its "working", but the timing on the background
thread gets wildly erratic at times. Some times, its right away, some times
after 10 seconds. I have included the setup of the process and the outline
of the Call Back Method.

As posted, the callback method can be mildly erratic (only doing a
debug.writeline) I am guessing up to an 80% variance on the time it takes to
print the Debug messages with an average variance of about 15%-25%. It gets
even worse when I add in the real program logic with up to 300% variance on
the loop timing.

If I stop & start the thread, it always clears up the timing for a few
seconds. Then its back to its crazy erratic self.

I think it makes since to try this on another box, but I need to know if my
approach is a good one or if I am doing something wildly silly to start
with.

Thanks!


Setting up the Process:
MyBroadcast = New BroadcastClass(Broad_IPAddress,
Convert.ToInt16(Broad_Port), PollingDelay, BroadcastInterleaveDelay)

MyBroadcast.Background_Capture_Process = New
System.Threading.Thread(AddressOf MyBroadcast.Capture_Process)

MyBroadcast.My_Process_Enabled = True

MyBroadcast.Background_Capture_Process.Start()




Basics of the CallBack Method:
public shared My_Process_Enabled as boolean

Public Sub Capture_Process()



While My_Process_Enabled

Try



For MyLoop = 0 To 5





Next

Debug.WriteLine(" Done:" & TimeOfDay)

Application.DoEvents()

Debug.Flush()

Application.DoEvents()

Threading.Thread.Sleep(Convert.ToInt16(PollingSpeed))

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End While

End Sub
 
Using Thread.Sleep to control the time methods are executed is going to get
you in trouble. Have you tried the System.Threading.Timer class? Also,
calling Application.DoEvents is *rarely* a good idea (although a thousand and
one other people could explain why better than me).

Although the below provides code smaples in C#, it is becoming a standard
text on the ngs, and is very good:

http://www.yoda.arachsys.com/csharp/threads/

Hope this helps
Dan
 
Thanks for the link!

I have had VERY BAD luck with timers. They just can just stop firing for no
good reason. Usually when I start more than 5 or 10.

I was trying with/without doEvents. Didnt seem to affect anything. My hope
was it would let the Debug.Writeline work properly ... no reall effect.
 
That article was great. Using this code, I was able to track down some
other threads that were causing timing problems. BTW, is there a slicker
way to do the Monitor.Enter/Exit, C# has the "LOCK () {}" Construct.

Thanks!


Public ReadOnly Capture_lock As New Object
......

System.Threading.Monitor.Enter(Capture_lock)

StartTime = TimeOfDay.Now.Millisecond

Debug.WriteLine("Begin:" & TimeOfDay.Now & ":" & StartTime)

StopTime = TimeOfDay.Now.Millisecond

Debug.WriteLine("Done:" & TimeOfDay.Now & ":" & StopTime)

Debug.WriteLine("Elapsed: " & StopTime - StartTime)

System.Threading.Monitor.Exit(Capture_lock)
 
Back
Top