|
Uh sorry, I've been writing Java, try 'readonly' (probably 'static readonly'). epochStart can be a psuedo-constant at the class level as it will never change.
I don't understand why you keep trying to bring Now into the equation. The start time is defined by the epoch start and its tick count, nothing to do with the present.
If you have procs.StartTime then, well, I don't understand the problem any more because that is what you are trying to find, right? Just use that DateTime directly ...
|
|
|
|
|
Thanks for your patience Bob,
Not sure why I included the Now operation in my post to you, sorry.
OK, so I tried this
long dt2 = procs.StartTime.Ticks;
DateTime epochStart = new DateTime(1600, 01, 01, 0, 0, 0);
DateTime startTime = new DateTime(dt2 - epochStart.Ticks);
and got
02/01/0002 00:00:00
if I go
DateTime startTime = new DateTime(dt2 + epochStart.Ticks);
I get 01/01/3200 00:00:00.
Interestingly (?) though, if I do this
long st = procs.StartTime.ToFileTime();
depite Luc's instructions, I get 504911232000000000. Dividing that by DateTime Ticks (10000000) I get 504911232 which is 31/12/1985 21:07:12 according to http://www.epochconverter.com/[^]
I think I need to pursue the method described in my reply to Alan's post.
|
|
|
|
|
What is procs.StartTime? Is it this property[^]? That's already a DateTime.
Edit: just seen Alan's post, apparently this property gives you a nonsense value (which you could have checked with the debugger).
|
|
|
|
|
full(ish) code, I am inserting the data int a SQL db thus the "inprocs" stuff...
Process[] processlist = Process.GetProcesses();
foreach (Process procs in processlist)
{
if (procs.ProcessName == "System")
{
insprocs.Parameters.Add("@PStart", SqlDbType.NVarChar, 30).Value = startTime.ToString();
}
}
I am currently ToString-ing it for testing but that may/may not remain the case.
If you try to GetProcesses without separating out the "System" (and "Idle")process,even though it is a DateTime, you get the error "System.ComponentModel.Win32Exception: Unable to enumerate the process modules", probably because of the "System" process's starttime properties.
So I am running GetProcesses 3 times-
1) if ((procs.ProcessName != "Idle") && (procs.ProcessName != "System")) // get everything
2) if (procs.ProcessName == "System") // get everything except "MainModule.FileName" (unenumerable for the System process)
3) if (procs.ProcessName == "Idle") // get only ProcessID, ProcessName (I think I can get RAM in use but I am not sure yet)
Depending on how you manipulate the starttime value, you get different values.
|
|
|
|
|
Process.StartTime is a DateTime. 'Manipulating' it means converting it into some other form that will give you a wrong answer. Now, according to Alan, on the System process this will give you a nonsense answer. That just means that Windows will not give you a sensible answer for that process (presumably because it's not a real process and various things in Windows internals are faking it). If you don't want that nonsense answer in your DB, then you need to use one of the other approaches to calculate a different value.
|
|
|
|
|
Yup, that's pretty much where I am at the moment too, at least until I can figure out the relationship between a known bootup time and the long st = procs.StartTime.ToFileTime().
I have gone back to this, but am struggling to work out how to make "stime" available;
long st = procs.StartTime.ToFileTime();
DateTime stDt = new DateTime(1601, 1, 1, 0, 0, 0).AddSeconds(st);
string stime = String.Empty;
stime += (Environment.TickCount / 86400000).ToString() + ",";
stime += Convert.ToInt32(Environment.TickCount / 3600000 % 24) + ",";
stime += Convert.ToInt32(Environment.TickCount / 120000 % 60) + ",";
stime += (Environment.TickCount / 1000 % 60);
DateTime result = DateTime.Now - new TimeSpan(stime);
currently erroring with "System.FormatException: Input string was not in a correct format."
|
|
|
|
|
Hi Bob,
Other approach used..[^]... can't figure out why I get 2 different results though...
Thanks for all your time today
|
|
|
|
|
Hi Luc,
Tried you code like so
DateTime dt1 = DateTime.Now;
TimeSpan span = dt1 - procs.StartTime;
string text1 = span.Days + "d " + span.Hours + "h " + span.Minutes + "m " + span.Seconds + "s";
string text2 = span.ToString();
result - 150191d 18h 18m 21s
How am I supposed to translate that in to 10/03/2012 09:15:22 (which is the approximate process start time)?
|
|
|
|
|
My bad, I meant to say Ticks are 100 nanosecond units.
TickCount becomes inaccurate (without conditional calculation) if the system is up for more than 24,9 days
To get system uptime I used this
DateTime Utime = new DateTime(1970, 1, 1).AddSeconds(t);
string uptime = String.Empty;
uptime += (Environment.TickCount / 86400000).ToString() + "d";
uptime += (Environment.TickCount / 3600000 % 24) + "h";
uptime += (Environment.TickCount / 120000 % 60) + "m";
uptime += (Environment.TickCount / 1000 % 60) + "s";
|
|
|
|
|
This bit of code will give you the system uptime regardless of how long the system has been running:
TimeSpan uptime = TimeSpan.Zero;
using (var ut = new PerformanceCounter("System", "System Up Time")) {
ut.NextValue();
uptime = TimeSpan.FromSeconds (ut.NextValue());
} /ravi
|
|
|
|
|
The Windows "System" process has a StartTime value of 01/01/1601 00:00:00. It is what it is and there is nothing you can do to transform the value into the actual start time of the process.
You have to accept that Windows is not giving you the start time and assume that 01/01/1601 means "not available". If you want to assign a hypothetical start time corresponding to computer's boot time then do so, but be clear in your own mind that the calculation you do to get that has nothing to do with January 1, 1601.
Alan.
modified 19-Mar-12 9:10am.
|
|
|
|
|
Hi Alan,
I am aware of the unix epoch time value for Window's "System" process starttime but I have to disagree that it is 01/01/1601, that is just what it reports.
As I posted in my original question, if you do this
string stime = String.Empty;
stime += (Environment.TickCount / 86400000).ToString() + "d";
stime += (Environment.TickCount / 3600000 % 24) + "h";
stime += (Environment.TickCount / 120000 % 60) + "m";
stime += (Environment.TickCount / 1000 % 60) + "s";
you can get the amount of time that the process has been running. From that, a better coder than I could deduce the starttime by somehow converting the resultant stime string to a datetime then subtracting it from datetime.now.
|
|
|
|
|
1) Unix time's epoch is 1/1/1970
2) 01/01/1601 is the epoch for Windows FileTime which is used by the operating system to report process start times. See for example the documentation for the win32 function GetProcessTimes[^].
3) I stand by my assertion that a reported process start time of 01/01/1601 is Windows way of telling you that it's not telling you.
4) Has this whole thread been about this?
private void Boot() {
DateTime boot = DateTime.Now - TimeSpan.FromMilliseconds(Environment.TickCount);
Console.WriteLine("System started at {0}", boot);
}
Alan.
|
|
|
|
|
Hi,
4) No, I already have a way to get the system uptime, I would like to get a full listing of all processes and as much information as I can get for each process.
Will get back to you on the others.
|
|
|
|
|
Well isn't that strange.
Okay, Environment.TickCount it is, but that resets every 232 ms, so that's not really useful either.
|
|
|
|
|
Ravi's answer gives the proper way to do it using a performance counter, thus avoiding the Int32.MaxValue problem.
Also see GetTickCount[^] for some useful info.
Alan.
|
|
|
|
|
Hello,
can any one tell me how can i bind multidimension array with datalgrid.
|
|
|
|
|
The first link here[^] is a CodeProject article.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
Hello C# Experts, can you please read this and reply.......?
I would like to make a basic video downloader. Looking for advice from the experts. Please advice and assist me.........Thannks.
|
|
|
|
|
Member 8735736 wrote: I would like to make a basic video downloader.
This is a very broad question that cannot be easily answered in a forum like this. Take a look at the articles[^] section, or try some Google searches to get some ideas and sample code.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thank you. I thought there are many experts and would be able to get someone straight from here.
|
|
|
|
|
|
Member 8735736 wrote: get someone straight from here
You just did.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
You did. The problem is no the lack of "experts" seeing your question.
The problem is that your question is so broad and requires an answer so large that it won't fit in a few forum posts.
The quality of the answer you get is directly dictated by the quality of your question.
BTW, using a downloader on sites like YouTube, or pretty much any other, is a violation of their terms of service. You are basically stealing their content and can be arrested for doing so.
|
|
|
|