Minherz

Hello. I have a form that display time in seconds resolution in the status bar. The time is used for some calculations during GUI operations. I have "Load Run Data" action which does several communications and then setup and load received XMLs into DataSet object. I do it in the UI thread in order to block menu and mouse input. Sometimes the loading operation takes more than 1 second so the displayed time becomes invalid. I need to find a valid solution to do the loading without hurting timer. I tried to use Threading.Timer without effect. It continues working but hurted by the higher priority thread (UI thread), so the time is still incorrect. Any suggestions. I am interested to know what Microsoft's default approach for this kind of problems.



Re: Windows Forms General Heavy operation and Windows.Forms.Timer

Alan Robbins

Minherz:

Have you tried using a System.Timer as opposed to a Windows.Forms.Timer






Re: Windows Forms General Heavy operation and Windows.Forms.Timer

nobugz

You'll need to switch from a relative time measure to an absolute time measure. The Timer's Tick event runs at very low priority. If you try to measure time by how often it runs, you'll always fall behind. Absolute time measures are DateTime.Now and Environment.TickCount.





Re: Windows Forms General Heavy operation and Windows.Forms.Timer

Minherz

There is no such thing as System.Timer class. I tried using thread driven timer instead of message driven timer if that was your question. I wrote it in the original post.





Re: Windows Forms General Heavy operation and Windows.Forms.Timer

Minherz

I have to display a time synchronized with my server application. I use SNTP client to get a time from the server and display that time using timer. I resynchronize each 2 minutes. I thought that .NET design and development team had something in mind for situations like this one when did designing of the .NET.
I see two solutions for my problem:
1. I resynchronize after each Loading/Saving operation if it takes longer than a second. This is a suggeted absolute time measure approach. I can use Environment.TickCount and TimeSpan.TicksPerSecond or DateTime.Now.
2. I run Loading/Saving operation in another thread. The main UI thread will open an invisible modal dialog which will wait for the operation to complete ( still do not know to do it) and then closes itself.

What is a "right" way to do i do not know. I like more the second way, but do not know how without blocking/sleeping in UI thread get a notification from another thread. I do not like idea of events but it looks like a single option.





Re: Windows Forms General Heavy operation and Windows.Forms.Timer

nobugz

This is a very odd problem, I might be missing something. First of all, you'd normally setup PCs in a network to obtain their time from the domain controller so all PCs in the LAN are in sync with each other. That would allow you to use your local clock. Secondly, PCs rarely drift more than a few seconds per month if for some reason they can't resync their clocks. Refreshing the time from the server every 2 minutes is total overkill, once a day should be fine.





Re: Windows Forms General Heavy operation and Windows.Forms.Timer

Minherz

This looks odd because it has nothing to do with machines' time synchronization.
I have an application that receives data from server and the data contains date/time values in the server's time. I have to display a progress of execution in the server in terms of the Local time zone. I do not want to be dependant from the gap in time of the local machine, since noone guarantee me that the machine is synchronized. So, i get a server time in UTC, i get a local time zone and can display now the server time in local time zone's offset. Basing on this value i can display an execution progress.
And as i said before it has nothing to do with my question. The question is very simple. I have heavy communication operation that should be executed modally, blocking UI of menu, toolbar and the rest. I have windows timer ticker which is convenient enough for use and has no overload of opening and closing thread on each restart. I need to do the operation, block UI and do not lose timer ticks (each second) in the case the operation continues longer than 1 second.
Now, if you have any ideas / suggestions / hints then please post. If you have questions why i do it then let's take it out of this thread.





Re: Windows Forms General Heavy operation and Windows.Forms.Timer

Minherz

Eventually using nobugz's post i came to solution that satisfied me :-). I calculate the TimeSpan delta between UTC server time and UTC client time and then add that delta to server time value's as well as use it to display current server time in the local time zone by DateTime.Now + delta on each System.Windows.Forms.Timer tick.