By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,533 Members | 1,497 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,533 IT Pros & Developers. It's quick & easy.

Stable Thread Timing?

P: n/a
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(PollingSpee d))

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End While

End Sub
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"gregory_may" wrote:
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(PollingSpee d))

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End While

End Sub

Nov 21 '05 #2

P: n/a
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.

"Dan Kelley" <Da*******@discussions.microsoft.com> wrote in message
news:E2**********************************@microsof t.com...
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

"gregory_may" wrote:
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(PollingSpee d))

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End While

End Sub

Nov 21 '05 #3

P: n/a
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)

"gregory_may" <None> wrote in message
news:e1*************@TK2MSFTNGP12.phx.gbl...
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(PollingSpee d))

Catch ex As Exception

Debug.WriteLine(ex.Message)

End Try

End While

End Sub

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.