When is multi-threading not multi-threading

I have a VB.NET console application that uses a System.Timers.Timer and monitors a folder using a System.IO.FileSystemWatcher.  The timer works fine but stops after having run 25 times or 50 on a multi processor server.  This suggests to me that it is hitting the maximum number of threads and then giving up.  I'm don't really know anything about multi-threading applications and haven't coded this as multi-threaded but I do use System.Threading.Thread.Sleep to get the app to pause in several places.

I have also tried swapping the timer to use the System.Threading.Timer but I get the same issue of it stopping after 25 goes.

Anyone out there that can help me sort out me timers?

Thanks, Stu
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
How often is your timer firing?  One possibility is that you are getting reentrant code in your timer.  What you can do is turn off the timer when it fires, do something and then turn it back on:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        ' do something in here...

    End Sub

Without further detail into how your app works or seeing more code we can only make vauge guesses though...

If you have not programmed your application to use several threads, it's not multithreaded.

The System.Threading.Thread.CurrentThread.Sleep() Blocks the current thread for the specified number of milliseconds. Since your application exists of at least 1 thread this is your main thread which is being suspended.

What this timer doind anyway
thew00dAuthor Commented:
I realise it would be quite difficult to "accidently" code something multi-threaded :-)

As for the timer, it checks for Zetafax status messages and runs a couple of stored procedures - nothing really

Is there a way to increase or decrease the maximum number of threads per processor - this would then confirm that it is in fact this limit it is hitting for some reason.

What else might cause it to stop firing after 25 runs?
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

This might sound a little anoying, but you're sure it not you who is disabling the timer?
thew00dAuthor Commented:
I am definately not disabling the timer - Its absolutely valid to ask - I've checked the code and rechecked for anywhere I may accidently be doing that.  That wouldnt explain why it stops after 25 times on my PC and 50 times on a multi-processor PC though...............
thew00dAuthor Commented:

That appears to have worked. Tried it on my PC and it happily went past 25 Timer fires.  During each Timer event I had been Disabling and Re-enabling the timer rather than using Stop and Start.  Once I've tried the changes on the server I will get back.  Thanks to Idle_Mind and RikeR for the comments so far.  This problem has been annoying me for months and I've only just had time to try to sort it out.
thew00dAuthor Commented:
Spot on.  Nice one.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.