Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. same enumeration, different results?

same enumeration, different results?

Scheduled Pinned Locked Moved C#
questioncsharpcomworkspace
25 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PIEBALDconsult

    Don't you simply want procs.StartTime ? :confused: (I tried to test it, but I'm at work and got System.ComponentModel.Win32Exception (0x80004005): Access is denied ) :sigh: .

    C Offline
    C Offline
    CCodeNewbie
    wrote on last edited by
    #3

    Hi PIEBALDconsult, from my previous post; Quote

    Quote:

    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.

    Unquote

    P 1 Reply Last reply
    0
    • C CCodeNewbie

      Hi PIEBALDconsult, from my previous post; Quote

      Quote:

      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.

      Unquote

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #4

      What is it you are trying to accomplish? Do you want the StartTime of the "System" process or not?

      C 1 Reply Last reply
      0
      • P PIEBALDconsult

        What is it you are trying to accomplish? Do you want the StartTime of the "System" process or not?

        C Offline
        C Offline
        CCodeNewbie
        wrote on last edited by
        #5

        Absolutely... Problem is, it has some weird Tick value that I can't get my hands on. Last time I did this (which seems to be just about the only way to get any meaningful value)

        long st = procs.StartTime.ToFileTime();

        I got "504911232000000000" which bears no relationship to the actual StartTime that I can find...

        P 1 Reply Last reply
        0
        • C CCodeNewbie

          Hello again, Following on from my question in which I was trying the patience of others and to get the StartTime of the Window's "System" process [^], I have found a way. Please forgive its current inelegance and downright clumsiness but it (kinda) works. Please bear with me on all the string d_x_ stuff;

          Process[] processlist = Process.GetProcesses();
          foreach (Process procs in processlist)
          {
          if (procs.ProcessName == "System")
          {
          long st = procs.StartTime.ToFileTime();
          DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
          string stime = String.Empty;
          stime += (Environment.TickCount / 86400000);
          stime += (Environment.TickCount / 3600000 % 24);
          stime += (Environment.TickCount / 120000 % 60);
          stime += (Environment.TickCount / 1000 % 60);
          string d = stime[0].ToString();
          string d1 = (stime[1]) + (stime[2]).ToString();
          string d2 = (stime[3]) + (stime[4]).ToString();
          string d3 = (stime[5]) + (stime[6]).ToString();

          DateTime temp = DateTime.Now - new TimeSpan(9, 13, 51, 22);// at the time of posting

          DateTime result = DateTime.Now - new TimeSpan(Convert.ToInt32(d), Convert.ToInt32(d1), Convert.ToInt32(d2), Convert.ToInt32(d3));

          Console.WriteLine("s :" + stime);
          Console.WriteLine("d :" + d);
          Console.WriteLine("d1 :" + d1);
          Console.WriteLine("d2 :" + d2);
          Console.WriteLine("d3 :" + d3);

          Console.WriteLine(temp);
          Console.WriteLine(result);

          Console.ReadLine();
          }
          }

          The output is; s :9135538 d :9 d1 :13 d2 :55 d3 :38 10/03/2012 10:14:22 10/03/2012 08:12:48 My question is therefore, given that the "new TimeSpan" timespan is being fed the same input (unless something is happening in the Convert.ToInt32 process) why is the output different and, can something be done about it? As always, thanks for your time.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          X|

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          C 2 Replies Last reply
          0
          • L Luc Pattyn

            X|

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            C Offline
            C Offline
            CCodeNewbie
            wrote on last edited by
            #7

            I know...

            1 Reply Last reply
            0
            • L Luc Pattyn

              X|

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              C Offline
              C Offline
              CCodeNewbie
              wrote on last edited by
              #8

              I don't know what to do about it yet. The pseudo -code posted is a prototype to demonstrate that getting the "System" process's StartTime can be exposed (accurately when coded correctly). What would your suggestion be?

              1 Reply Last reply
              0
              • C CCodeNewbie

                Absolutely... Problem is, it has some weird Tick value that I can't get my hands on. Last time I did this (which seems to be just about the only way to get any meaningful value)

                long st = procs.StartTime.ToFileTime();

                I got "504911232000000000" which bears no relationship to the actual StartTime that I can find...

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #9

                Either the StartTime gives you what you want directly or not at all, no amount of manipulation is going to give you what it doesn't have so stop trying that. Now, what is it you are trying to accomplish? Do you want to know when the system started up? I recommend creating a Windows Scheduled Task to run at system startup and log that somewhere; it could be as simple as echo %date% %time% >> %appdata%\Reboot.log .

                L C 3 Replies Last reply
                0
                • C CCodeNewbie

                  Hello again, Following on from my question in which I was trying the patience of others and to get the StartTime of the Window's "System" process [^], I have found a way. Please forgive its current inelegance and downright clumsiness but it (kinda) works. Please bear with me on all the string d_x_ stuff;

                  Process[] processlist = Process.GetProcesses();
                  foreach (Process procs in processlist)
                  {
                  if (procs.ProcessName == "System")
                  {
                  long st = procs.StartTime.ToFileTime();
                  DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
                  string stime = String.Empty;
                  stime += (Environment.TickCount / 86400000);
                  stime += (Environment.TickCount / 3600000 % 24);
                  stime += (Environment.TickCount / 120000 % 60);
                  stime += (Environment.TickCount / 1000 % 60);
                  string d = stime[0].ToString();
                  string d1 = (stime[1]) + (stime[2]).ToString();
                  string d2 = (stime[3]) + (stime[4]).ToString();
                  string d3 = (stime[5]) + (stime[6]).ToString();

                  DateTime temp = DateTime.Now - new TimeSpan(9, 13, 51, 22);// at the time of posting

                  DateTime result = DateTime.Now - new TimeSpan(Convert.ToInt32(d), Convert.ToInt32(d1), Convert.ToInt32(d2), Convert.ToInt32(d3));

                  Console.WriteLine("s :" + stime);
                  Console.WriteLine("d :" + d);
                  Console.WriteLine("d1 :" + d1);
                  Console.WriteLine("d2 :" + d2);
                  Console.WriteLine("d3 :" + d3);

                  Console.WriteLine(temp);
                  Console.WriteLine(result);

                  Console.ReadLine();
                  }
                  }

                  The output is; s :9135538 d :9 d1 :13 d2 :55 d3 :38 10/03/2012 10:14:22 10/03/2012 08:12:48 My question is therefore, given that the "new TimeSpan" timespan is being fed the same input (unless something is happening in the Convert.ToInt32 process) why is the output different and, can something be done about it? As always, thanks for your time.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  24.85

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  C 1 Reply Last reply
                  0
                  • C CCodeNewbie

                    Hello again, Following on from my question in which I was trying the patience of others and to get the StartTime of the Window's "System" process [^], I have found a way. Please forgive its current inelegance and downright clumsiness but it (kinda) works. Please bear with me on all the string d_x_ stuff;

                    Process[] processlist = Process.GetProcesses();
                    foreach (Process procs in processlist)
                    {
                    if (procs.ProcessName == "System")
                    {
                    long st = procs.StartTime.ToFileTime();
                    DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
                    string stime = String.Empty;
                    stime += (Environment.TickCount / 86400000);
                    stime += (Environment.TickCount / 3600000 % 24);
                    stime += (Environment.TickCount / 120000 % 60);
                    stime += (Environment.TickCount / 1000 % 60);
                    string d = stime[0].ToString();
                    string d1 = (stime[1]) + (stime[2]).ToString();
                    string d2 = (stime[3]) + (stime[4]).ToString();
                    string d3 = (stime[5]) + (stime[6]).ToString();

                    DateTime temp = DateTime.Now - new TimeSpan(9, 13, 51, 22);// at the time of posting

                    DateTime result = DateTime.Now - new TimeSpan(Convert.ToInt32(d), Convert.ToInt32(d1), Convert.ToInt32(d2), Convert.ToInt32(d3));

                    Console.WriteLine("s :" + stime);
                    Console.WriteLine("d :" + d);
                    Console.WriteLine("d1 :" + d1);
                    Console.WriteLine("d2 :" + d2);
                    Console.WriteLine("d3 :" + d3);

                    Console.WriteLine(temp);
                    Console.WriteLine(result);

                    Console.ReadLine();
                    }
                    }

                    The output is; s :9135538 d :9 d1 :13 d2 :55 d3 :38 10/03/2012 10:14:22 10/03/2012 08:12:48 My question is therefore, given that the "new TimeSpan" timespan is being fed the same input (unless something is happening in the Convert.ToInt32 process) why is the output different and, can something be done about it? As always, thanks for your time.

                    A Offline
                    A Offline
                    Alan N
                    wrote on last edited by
                    #11

                    Your calculation is wrong.

                    Quote:

                    stime += (Environment.TickCount / 120000 % 60);

                    Can you see it now? Alan.

                    C 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Either the StartTime gives you what you want directly or not at all, no amount of manipulation is going to give you what it doesn't have so stop trying that. Now, what is it you are trying to accomplish? Do you want to know when the system started up? I recommend creating a Windows Scheduled Task to run at system startup and log that somewhere; it could be as simple as echo %date% %time% >> %appdata%\Reboot.log .

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #12

                      Environment.TickCount[^] is more than adequate IMO. And that is what I recommended days ago, to no avail. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      C 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Either the StartTime gives you what you want directly or not at all, no amount of manipulation is going to give you what it doesn't have so stop trying that. Now, what is it you are trying to accomplish? Do you want to know when the system started up? I recommend creating a Windows Scheduled Task to run at system startup and log that somewhere; it could be as simple as echo %date% %time% >> %appdata%\Reboot.log .

                        C Offline
                        C Offline
                        CCodeNewbie
                        wrote on last edited by
                        #13

                        I already know what time the computer/system started up. I want to know what time the "System" process, as viewed in the Task Manager started.

                        P 1 Reply Last reply
                        0
                        • L Luc Pattyn

                          Environment.TickCount[^] is more than adequate IMO. And that is what I recommended days ago, to no avail. :)

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          C Offline
                          C Offline
                          CCodeNewbie
                          wrote on last edited by
                          #14

                          I already know what time the computer/system started up. I want to know what time the "System" process, as viewed in the Task Manager started.

                          1 Reply Last reply
                          0
                          • A Alan N

                            Your calculation is wrong.

                            Quote:

                            stime += (Environment.TickCount / 120000 % 60);

                            Can you see it now? Alan.

                            C Offline
                            C Offline
                            CCodeNewbie
                            wrote on last edited by
                            #15

                            Yup, saw it - thanks for pointing it out. Got it working, code is in my reply to PIEBALDconsult. Wanted to say a quick thanks for your help.

                            1 Reply Last reply
                            0
                            • P PIEBALDconsult

                              Either the StartTime gives you what you want directly or not at all, no amount of manipulation is going to give you what it doesn't have so stop trying that. Now, what is it you are trying to accomplish? Do you want to know when the system started up? I recommend creating a Windows Scheduled Task to run at system startup and log that somewhere; it could be as simple as echo %date% %time% >> %appdata%\Reboot.log .

                              C Offline
                              C Offline
                              CCodeNewbie
                              wrote on last edited by
                              #16

                              Still not perfect - but it works (Improvements very welcome)

                              TimeSpan uptime = TimeSpan.Zero;
                              using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time"))
                              {
                              ut.NextValue();
                              uptime = TimeSpan.FromSeconds(ut.NextValue());
                              }
                              Console.WriteLine(uptime.ToString());
                              //result - 03:53:39.7870000

                              Process[] processlist = Process.GetProcesses();
                              foreach (Process procs in processlist)
                              {
                              if (procs.ProcessName == "System")
                              {
                              long st = procs.StartTime.ToFileTime();
                              DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
                              float Days = (Environment.TickCount / 86400000);
                              int dd = (int)Days;
                              float Hours = (Environment.TickCount / 3600000 % 24);
                              int HH = (int)Hours;
                              float Mins = (Environment.TickCount / 60000 % 60);
                              int mm = (int)Mins;
                              float Secs = (Environment.TickCount / 1000 % 60);
                              int ss = (int)Secs;
                              DateTime result = DateTime.Now - new TimeSpan(dd, HH, mm, ss);
                              }
                              }
                              //result - 20/03/2012 08:31:33

                              code run @ ~ 12:24 pm

                              A 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                24.85

                                Luc Pattyn [My Articles] Nil Volentibus Arduum

                                C Offline
                                C Offline
                                CCodeNewbie
                                wrote on last edited by
                                #17

                                Hi Luc, Got it working but wanted to say thank you for your help (code is in my reply to PIEBALDconsult). Would welcome your thoughts and comments...

                                1 Reply Last reply
                                0
                                • C CCodeNewbie

                                  I already know what time the computer/system started up. I want to know what time the "System" process, as viewed in the Task Manager started.

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #18

                                  For what purpose? Task Manager says mine started in 2009 which is certainly untrue. So I don't see how you can do better. P.S. Even as the administrator on my home system (Win 7) I still get the access violation when I try to get the StartTime of the System process.

                                  C 1 Reply Last reply
                                  0
                                  • C CCodeNewbie

                                    Still not perfect - but it works (Improvements very welcome)

                                    TimeSpan uptime = TimeSpan.Zero;
                                    using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time"))
                                    {
                                    ut.NextValue();
                                    uptime = TimeSpan.FromSeconds(ut.NextValue());
                                    }
                                    Console.WriteLine(uptime.ToString());
                                    //result - 03:53:39.7870000

                                    Process[] processlist = Process.GetProcesses();
                                    foreach (Process procs in processlist)
                                    {
                                    if (procs.ProcessName == "System")
                                    {
                                    long st = procs.StartTime.ToFileTime();
                                    DateTime stDt = new DateTime(1601, 1, 1).AddSeconds(st);
                                    float Days = (Environment.TickCount / 86400000);
                                    int dd = (int)Days;
                                    float Hours = (Environment.TickCount / 3600000 % 24);
                                    int HH = (int)Hours;
                                    float Mins = (Environment.TickCount / 60000 % 60);
                                    int mm = (int)Mins;
                                    float Secs = (Environment.TickCount / 1000 % 60);
                                    int ss = (int)Secs;
                                    DateTime result = DateTime.Now - new TimeSpan(dd, HH, mm, ss);
                                    }
                                    }
                                    //result - 20/03/2012 08:31:33

                                    code run @ ~ 12:24 pm

                                    A Offline
                                    A Offline
                                    Alan N
                                    wrote on last edited by
                                    #19

                                    I'm glad you've come to a solution that you are happy with. One point that I'm not sure you appreciate is that the up time obtained from the performance counter and the TickCount are essentially the same value.

                                    private void CompareCounterAndTickCount() {
                                      using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time")) {
                                        ut.NextValue();
                                        TimeSpan counterUpTime = TimeSpan.FromSeconds(ut.NextValue());
                                        TimeSpan tickCountUpTime = TimeSpan.FromMilliseconds(Environment.TickCount);
                                        Console.WriteLine("Up Time");
                                        Console.WriteLine("  from performance counter {0}", counterUpTime);
                                        Console.WriteLine("  from tickcount           {0}", tickCountUpTime);
                                      }
                                    }
                                    

                                    On my system the duration reported by TickCount is ~0.2s longer than the performance counter's value which presumably just means the two counters do not share the same origin. Also note the use of the FromMilliseconds method to convert the TickCount to a TimeSpan, much simpler than your multistep calculation! Alan.

                                    C 1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      For what purpose? Task Manager says mine started in 2009 which is certainly untrue. So I don't see how you can do better. P.S. Even as the administrator on my home system (Win 7) I still get the access violation when I try to get the StartTime of the System process.

                                      C Offline
                                      C Offline
                                      CCodeNewbie
                                      wrote on last edited by
                                      #20

                                      You need to run it as System (mine impersonates System to get the info, Administrator doesn't have enough privileges) The purpose is a) because it completes the Process listing and allows me to cross-reference against netstat & system boot-time.

                                      P 1 Reply Last reply
                                      0
                                      • A Alan N

                                        I'm glad you've come to a solution that you are happy with. One point that I'm not sure you appreciate is that the up time obtained from the performance counter and the TickCount are essentially the same value.

                                        private void CompareCounterAndTickCount() {
                                          using (PerformanceCounter ut = new PerformanceCounter("System", "System Up Time")) {
                                            ut.NextValue();
                                            TimeSpan counterUpTime = TimeSpan.FromSeconds(ut.NextValue());
                                            TimeSpan tickCountUpTime = TimeSpan.FromMilliseconds(Environment.TickCount);
                                            Console.WriteLine("Up Time");
                                            Console.WriteLine("  from performance counter {0}", counterUpTime);
                                            Console.WriteLine("  from tickcount           {0}", tickCountUpTime);
                                          }
                                        }
                                        

                                        On my system the duration reported by TickCount is ~0.2s longer than the performance counter's value which presumably just means the two counters do not share the same origin. Also note the use of the FromMilliseconds method to convert the TickCount to a TimeSpan, much simpler than your multistep calculation! Alan.

                                        C Offline
                                        C Offline
                                        CCodeNewbie
                                        wrote on last edited by
                                        #21

                                        Yes, but I am not trying to get the system start-time. I can already get that using Ravi's post (see my previous question). I explicitly want the System process's start-time.

                                        A 1 Reply Last reply
                                        0
                                        • C CCodeNewbie

                                          Yes, but I am not trying to get the system start-time. I can already get that using Ravi's post (see my previous question). I explicitly want the System process's start-time.

                                          A Offline
                                          A Offline
                                          Alan N
                                          wrote on last edited by
                                          #22

                                          I can't understand your reasoning in this matter. The SystemUp Time performance counter description is System Up Time is the elapsed time (in seconds) that the computer has been running since it was last started. This counter displays the difference between the start time and the current time. The Environment.TickCount description is A 32-bit signed integer containing the amount of time in milliseconds that has passed since the last time the computer was started. Subtracting either from the current time gets the time when the computer was started. I don't see why (or how) you differentiate the system start time and the "System" process start time. Alan.

                                          C 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups