Timer problem in CF 1.1 on WinCE 4.2

  • Thread starter Thread starter hakan aktan
  • Start date Start date
H

hakan aktan

I have a timer and a few sequent function inside it. Functions are called
in a "For ..Each" code block.Program does not response any request that made
by form while code goes into timer. I used an "Application.DoEvents()" at
just beginning of timer.tick but i can not use it inside "For ..Each".
If i do that program makes stg strange and stop working. How may i make my
program to response user in all these sequent events.Any advice would be
nice..
Thanx in advance..
 
You have to explain your problem better by showing a code sample.

Even though I don't understand what issue you are facing here are two common
*mistakes* devs make when it comes to timers:
1. Specify a small interval for the timer. In a NETCF app anything under a
second for a Windows.Forms.Timer and you really have to question the reasons
behind it.
2. Leave the timer running without coding for reentrant conditions. In other
words, what you should be doing is starting your timer, stopping it when it
fires and then restarting it if you have to at the end of the timer_tick
handling code.

Cheers
Daniel
 
And you know where guessing gets you...oh wait, that's assuming. Same
principal anyway.

-Chris
 
I presumed that guessing != assuming :-)

(and given that I am on the side of the pond that invented the language I've
got to be right, right?)

Cheers
Daniel
 
Here some code from my code ( timer is form timer).I added explanations of
my problem inside code.
Thanx in advance.

Private Sub timerTank_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles timerTank.Tick

'Application.DoEvents() works here but whern i use it inside FOR as below
program stops.

Application.DoEvents()

For counter = 0 To hashTableTank.Count - 1

'Here is the code (Just inside for) where i want to use Application.DoEvents
becuase when code runs here , form does not response requests.

If portSettings() AndAlso portOpen() Then

Select Case Trim(currentTankControl.probeType)

Case "Cylindir"

CalculateCylindir()

Case "Rectangle"

CalcuateRectangle()

End Select

Else

MsgBox("Check Comm Ports.")

Exit Sub

End If

Next

End Sub

Note: CalculateCylindir() & CalcuateRectangle() methods have a few
sleep like ( System.Threading.Thread.Sleep( 100 ) ) inside them.
 
So you are not addressing the two concerns I raised.
1. What is the interval of the timer?
2. I don't see you disabling the timer in timerTank_Tick (and if necessary
re-enabling it on exit)

Once you've done the second and verified that the answer to the first is
1000 then we can proceed.

FYI, there are very few cases where DoEvents is justified and it is
sometimes an indication of code requiring worker threads (or some wrapper
like BackgroundWorker). This will certainly be the case here if your two
calculate methods are long running (which, btw, shouldn't have Sleep in them
since you are calling them on the UI thread)

Cheers
Daniel
 
Sure, the behavior you describe is expected. The Forms.Timer handler runs
in the UI thread context. While it's running, the UI won't service other
stuff, so it will appear locked. Use a Threading.Timer.

-Chris
 
It is seldom a good idea to call Application.DoEvents (though there are
exceptions). You often can use a worker thread instead, and achieve better
performance. I suggest threading only when needed, but if your loop takes
seconds to run, then it may be a good idea. However, if the loop completes
in a couple hundred mS, then don't bother... And, don't bother with
Application.DoEvents.


--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
Back
Top