|
The system timer has a resolution, typically, of 15 milliseconds. The actual value depends on the exact hardware you have. The physical timer periodically interrupts a processor, which the OS handles to fire off timer events, and to decide whether to reschedule the CPUs if there are multiple threads that can run.
The operating system counts time in 100ns intervals, it's true. However, it simply adds a fixed value to the 'current wall-clock time' variable every time a timer interrupt occurs. That value can be altered with the SetSystemTimeAdjustment API, and it's used to compensate for the slight inaccuracies that occur with the actual versus theoretical run rate of the clock, and slight issues surrounding responsiveness to the clock interrupt.
The divergence you're seeing between DateTime.Now.Ticks (which is the system clock time) and the timer interval is probably the latency in having the timer fire. If you want a time stamp with more resolution, see the QueryPerformanceCounter API, which is generally based on a processor performance monitoring register. However, on some processors this can be unpredictable as it is directly related to the core's running clock speed, and power management can vary the clock speed, so the counter doesn't count at a constant rate!
The physical hardware timer circuit is programmable. To support multimedia applications that require a better resolution for their timers, the OS can reprogram it for more frequent interrupts, which it does if an application uses the multimedia timer APIs. When all applications free their multimedia timers, the OS goes back to the default timer interval. Presumably this is to reduce the overhead in thread time accounting and thread dispatching.
|
|
|
|
|
I'm replying here because, although I got the notification message, your other post didn't show up on the forum!
Heritos Gger wrote: Don't get me wrong, I'm not arguing with you. You obviously know a lot more about it than me, I admit.
However, I still don't understand how you would say that the "system timer" has a resolution of 15 milliseconds and the "system clock time" has a resolution of 100ns. System.Windows.Forms.Timer has a resolution of one millisecond (better than 15). Are these three different timers we're talking about?(100ns,1ms,15ms resolution)
I was under the impression that there's one clock in the CPU, and everything gets chronological measurements from that (although, now that I've typed that, it sounds way too simple for CPU guts).
Thank you for your patience with me!
I need to point you to Raymond Chen's blog post, "Precision is not the same thing as accuracy"[^].
The system's clock measurements are based entirely around the Programmable Interval Timer component. This component, on x86/x64 computers, dates back to the original IBM PC, in which it was an Intel 8253 chip. In modern computers it's a circuit somewhere in one of the chipset chips (on my Intel 850-based P4 system it's hanging off the Low-Pin-Count controller, according to Device Manager where it appears as 'System timer' - this is the successor of the antique ISA bus, for devices that aren't expected to handle the complexities of PCI!)
(I've seen documentation on Wikipedia that this interval timer function is actually now performed by the Local APIC, which is a circuit of the processor which manages interrupts, so this may not be completely accurate, if your system also has an I/O APIC and Windows is using it, which most modern systems do. This would explain why the 'System timer' is shown as being in power state D3, which is the state with highest power savings and effectively means 'off'.)
Anyway, this timer component is programmed to interrupt the processor on a regular basis, by default every 15.625ms on my system. (You can find out the clock resolution using the ClockRes[^] utility.) Why the odd number? It's actually 1/64th of a second.
The SetTimer API (and hence System.Windows.Forms.Timer ) accepts an interval specified in milliseconds, GetTickCount and GetSystemTime offer a multiple of milliseconds as their result, and GetSystemTimeAsFileTime (hence DateTime.Now.Ticks ) offers multiples of 100ns. However, none of these APIs can offer this actual resolution. They're all tied to the programmable interval timer circuit's tick rate.
The system also has a real-time clock circuit introduced in the IBM AT, but that is useless for actual timekeeping since it requires a total of 6 I/O bus write/I/O bus read pairs to retrieve the actual time, and it only counts in 1 second intervals anyway. The operating system only reads from it at startup and writes to it when adjusting the clock (SetSystemTime or SetLocalTime , not every tick from the PIT circuit!) and on shutdown.
Why does the FILETIME structure (hence Ticks ) have such an absurdly high resolution? I assume it's because NT was meant to be, and was, portable to other architectures which didn't have such lame timer capabilities. You'd need at least microsecond resolution with that default clock rate; if you only had millisecond resolution and added 15ms every tick, your clock would be 4% slow, losing nearly 2 1/2 minutes every hour. If the clock is varied down to approx 1ms (which would be implemented on the PIT as 1/1024 seconds, 976.5625us) you're starting to hit the actual resolution of the structure. 62.5ns will be lost every time the timer ticks in this case, but that's less than one part per million, far better than the hardware can actually do.
-- modified at 17:17 Sunday 27th August, 2006 (messed up closing format tag)
|
|
|
|
|
The reason I replied off the forum was because I didn't want to task others' patience on what is turning out to be a detailed explanation of timers. It's venturing a little from C#.
From what you've explained, it sounds like they need to come up with a better way to track time. Perhaps they could use current sub-micron semiconductor manufacturing technology to make a quartz crystal that ocillated reliably (although I realize it would have to be a lot faster than those in watches).
My original idea was finding out exactly how much time a process took from start to finish, so that I could repeat it with as little idle time between finish and start as possible.
The more I work on this program (I've added more to its iterative processes), the more time I have to add to myTimer.Interval. I guess as long as the process doesn't take more than 15 fps, I should be fine (because, after all, it's just a stupid game... a game I get the feeling I'm making more complicated that it needs to be).
Thanks for the information!
-Daniel
Typing too fast fro my owngood
|
|
|
|
|
Does anyone know what format string I need to pass into the DateTime ToString() method to display the current date and time as 08/25/2006 14:48? Thanks.
|
|
|
|
|
See the "Custom date formatting" section here[^].
/ravi
|
|
|
|
|
Use the "g" or "G" for general date. The lower case g will give you to minutes as you asked (short date and short time). If you use capital G, you also get seconds (short date and long time.
If you do not want the AM/PM notation, use a custom format mask "M/d/yyyy m:h". Note, capitalization is important. Use M for month and m for minutes. If you use h you get 12 hour notation, if you use H, you get 24 hour notation.
David
|
|
|
|
|
DateTime yourDateTime = DateTime.Now;
String.Format("{0:M/d/yyyy h:m}", yourDateTime);
-Have Fun!
|
|
|
|
|
If you are calling the ToString method of the DateTime object, you would want to use the following:
public void sample()
{
// Create a new DateTime instance set to
// 9/1/2006 7:22 PM
DateTime d = new DateTime(2006, 9, 1, 14, 22, 0);
Console.WriteLine(d.ToString(@"MM/dd/yyyy HH:mm"));
}
|
|
|
|
|
I am getting this error when trying to use the Threading in PingReply:
Cross-thread operation not valid: Control 'lstHosts' accessed from a thread other than the thread it was created on.
THIS IS THE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading;
namespace HostHealth
{
public partial class frmMain : Form
{
private Thread ping_thread = null;
int listview_item;
public frmMain()
{
InitializeComponent();
}
private void ping_to_host()
{
for (listview_item = 0; listview_item <= lstHosts.Items.Count - 1; listview_item++)
{
Ping ping_sender = new Ping();
PingOptions ping_options = new PingOptions();
ping_options.DontFragment = true;
PingReply ping_reply = ping_sender.Send(lstHosts.Items[listview_item].SubItems[1].Text);
if (ping_reply.Status == IPStatus.Success)
{
lstHosts.Items[listview_item].SubItems[0].Text = "Success";
lstHosts.Items[listview_item].SubItems[2].Text = ping_reply.RoundtripTime.ToString();
lstHosts.Items[listview_item].SubItems[3].Text = ping_reply.Options.Ttl.ToString();
lstHosts.Items[listview_item].BackColor = Color.Black;
}
else
{
lstHosts.Items[listview_item].SubItems[0].Text = ping_reply.Status.ToString();
lstHosts.Items[listview_item].BackColor = Color.Red;
}
toolStart.Text = "Idle - " + DateTime.Now;
ping_thread.Abort();
}
}
private void frmMain_Shown(object sender, EventArgs e)
{
lstHosts.Columns.Add("Status");
lstHosts.Columns.Add("Where");
lstHosts.Columns.Add("time");
lstHosts.Columns.Add("ttl");
lstHosts.Columns[0].Text = "Status";
lstHosts.Columns[1].Text = "Where";
lstHosts.Columns[2].Text = "time";
lstHosts.Columns[3].Text = "ttl";
lstHosts.Columns[0].Width = 200;
lstHosts.Columns[1].Width = 200;
lstHosts.Columns[2].Width = 80;
lstHosts.Columns[3].Width = 80;
lstHosts.Columns[0].TextAlign = HorizontalAlignment.Left;
lstHosts.Columns[1].TextAlign = HorizontalAlignment.Left;
lstHosts.Columns[2].TextAlign = HorizontalAlignment.Center;
lstHosts.Columns[3].TextAlign = HorizontalAlignment.Center;
DataSet dsHosts = new DataSet("hosts");
dsHosts.ReadXml("C:\\Users\\Jassim\\Documents\\Visual Studio 2005\\Projects\\HostHealth\\hosts.xml");
foreach (DataRow drHosts in dsHosts.Tables[0].Rows)
{
lstHosts.Items.Add("Pending..");
lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add(drHosts["IPAddress"].ToString());
lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add("");
lstHosts.Items[lstHosts.Items.Count - 1].SubItems.Add("");
}
}
private void toolStart_Click(object sender, EventArgs e)
{
if (toolStart.Text == "START")
{
toolStatus.Text = "Idle";
toolStart.Text = "STOP!!";
toolStart.ForeColor = Color.Red;
toolStart.ToolTipText = " Stop Monitoring ";
timerPing.Enabled = true;
}
else
{
timerPing.Enabled = false;
toolStatus.Text = "Off";
toolStart.Text = "START";
toolStart.ForeColor = Color.Black;
toolStart.ToolTipText = " Start Monitoring ";
}
}
private void timerPing_Tick(object sender, EventArgs e)
{
this.ping_thread = new Thread(new ThreadStart(this.ping_to_host));
this.ping_thread.Start();
ping_to_host();
}
}
}
|
|
|
|
|
It is rather self-explanatory. You are tring to access the listbox in the thread you call ping_to_host on. UI elements may only be manipulated from the thread on which they were created. You need to create an event handler to update your listbox or use invoke so it will be called on the proper thread.
only two letters away from being an asset
|
|
|
|
|
Hi all
i want to receive fax with C# 2005 .
Help me please.
thx a lot
|
|
|
|
|
|
i try to find sample code but I can't find
|
|
|
|
|
Have you read the article?!!
Here is the source code of the article in this Link[^]!!
Regards
|
|
|
|
|
^
^
it is send Fax. ??
it not Receive Fax.
i am beginer plZ help me.
thx
|
|
|
|
|
Hi, I was wondering if anyone can help me with my problem.
I have created a windows application (service). This service will run every day at 7am and it will generate a PDF (that was created in crystal reports) which will then be emailed to certain users.
My problem is, I want the PDF to only be generated to the output file folder. I do not want the PDF to be generated to the screen.
I know how to do this in a web application. I have tried using the same code in the windows application and it does not work. When in debug mode, when you hit the line reportDoc.Export();, the service will stop itself.
I have tried using the CrystalReportViewer as well but I get a prompt to enter a file name and to save to the hard drive.
Can anyone help me? Thanks.
// Get ReportDocument
ReportDocument reportDoc = new ReportDocument();
reportDoc.Load(rptPath + "rptDailyDriverExceptionReport.rpt");
string outputFile = "";
// Setup the export options
DiskFileDestinationOptions diskOpts = new DiskFileDestinationOptions();
outputFile = outPath + "DailyDriverExceptionReport_" + GetDateTimeString() + ".pdf";
diskOpts.DiskFileName = outputFile;
ExportOptions exportOpts = reportDoc.ExportOptions;
exportOpts.DestinationOptions = diskOpts;
exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;
exportOpts.ExportFormatType = ExportFormatType.PortableDocFormat;
//Update DB connection settings
TableLogOnInfo dbConn = reportDoc.Database.Tables[0].LogOnInfo;
dbConn.ConnectionInfo.DatabaseName = dbName;
dbConn.ConnectionInfo.ServerName = dbServer;
dbConn.ConnectionInfo.UserID = dbUserID;
dbConn.ConnectionInfo.Password = dbPassword;
reportDoc.Database.Tables[0].ApplyLogOnInfo(dbConn);
reportDoc.Export();
Eva
|
|
|
|
|
Hello Everyone,
I need to print form data to an okidata printer. Anyone know how I can go about doing this? It's a track feed printer.
Thanks
|
|
|
|
|
I have COM object written in C# that exposes connection points and deployed as out of process server.
I wrote VB6 application that supplies handler for the events fired by this COM object.
To do that I added reference in my VB project to type library generated for C# COM.
Next I used WithEvents statement in my VB code to declare variable of the events firing type exported from the COM.
The application works fine. I can consume the events from the C# COM in my VB6 code.
Problem arises when I stop my VB application.
The delegate that implements the event in C# remains assigned and exception is thrown when attempt is made to call it.
When you implement Event Sink say in C++ you use Advise – Unadvise paradigm to assign and clean up your handlers.
What you supposed to do in VB6?
Any help will be appreciated.
|
|
|
|
|
Please don't cross post.
---
b { font-weight: normal; }
|
|
|
|
|
I would like to use a global DLL that could be called by any program in the app. In VB6, all I had to do is create a code module which inited the DLL variables, some of which come from the database. I am migrating this app with new features to C# and have been unsuccessful in getting my ALLINFO.DLL to be availabe. Can anyone help me with this?
Thanks
David
|
|
|
|
|
Hello
drdavef wrote: called by any program in the app
What is a program?? What is an app??!!
drdavef wrote: and have been unsuccessful in getting my ALLINFO.DLL to be availabe
Why? What is your problem exactly??
In C# you make a new project for your dll, and in other projects in the solution, you make a reference by project to the dll -look for the project tab in the reference dialog-.
Regards
|
|
|
|
|
What i mean by a program is an executable that will do a variety of tasks. When I say app, I mean the entire application, which consists of 15-25 programs and a number of DLLs.
I want a DLL, that I call AppInfo, which contains general info that all programs need access to, such as SchoolYear. The application is involved with schools and school services. One thing it tracks is the type of student involved in the school year, is the student a Senior, Undergrad, or Postgrad (college). This is a toggle that the user of a given program will select to determine functionality for each type of student. This AppInfo DLL also contains functions specific for client specifications. For example, a rounding function that always rounds to the next nickle (no pennies wanted ).
I am looking for a way to create this DLL such that initialization is very simple. I used to simply call the AppInfo.Init from Main to set it up. I have not found a way to create a DLL that is available to all program functions. A program may consist of many forms and support DLLs.
Thanks for any support
David
|
|
|
|
|
Hello
I've posted the way that is used to make dlls in C# in my previous post! Is that what you want?
drdavef wrote: I want a DLL, that I call AppInfo, which contains general info that all programs need access to, such as SchoolYear
I thought SchoolYear should be stored as a variable, and since you are a programmer you know about scope. How would you store a variable in another module?!! Even in VB I don't think that's possible!!
drdavef wrote: I am looking for a way to create this DLL such that initialization is very simple.
Well, I haven't worked much in VB, but I've spent a lot of time in C# and I've never heard of Dll init!! dlls are referenced, not intitialized, specially that you are writting these dlls. I have a feeling somehow I still don't get you!! Please forgive me for that
Another approach to store shared info in an executable is to make it as a win service. I don't think that's what you want, is it?!!
Anyway. The way I know it, you want dll it's simple. Make a dll project and reference to it in other projects. You want shared info, store them in a file -careful here for read/write operations- or use a database!!
If you managed to store variables in a dll, and share it with other programs, I beg you to write an article about it.I'll be dying to read it
Regards
|
|
|
|
|
Hi,
I'm very new in programing, I create a report in Crystal reports and I want to call it from my project in sharp develop.
Somebody could me explain step by step what things I have to do?
I'm using sharp develop 2.0.0 Build 1591.
Thanks
|
|
|
|
|
Hi,
We have a function which parses csv into datatables written in C#
Want to use this function in my VB.net code. How would we go about doing this? I tried using the function name nd it doesn't seem to work, it comes back with swiggly lines. using VS 2005.
thanks.
|
|
|
|
|