public void Stop()
        {
            if (CurrentSession == this)
            {
                CurrentSession = null;
            }

            if (output == null)
            {
                return;
            }

            writer = new System.IO.StreamWriter(output);

            bool first = true;

            //convert to microseconds
            double scale = 1000000.0 / System.Diagnostics.Stopwatch.Frequency;

            BeginDocument();
            var stack = new List <int>();

            for (int i = 0, end = events.Count; i != end; ++i)
            {
                for (int j = stack.Count; j-- > 0;)
                {
                    var p = events[stack[j]];
                    if (p.parentIdx == i)
                    {
                        break;
                    }
                    EndEvent(p.name, p.end * scale);
                }
                var e = events[i];
                BeginEvent(e.name, e.start * scale, first);
                first = false;

                if (i + 1 < end && events[i + 1].parentIdx == i)
                {
                    stack.Add(i);
                }
                else
                {
                    EndEvent(e.name, e.end * scale);
                }
            }
            for (int j = stack.Count; j-- > 0;)
            {
                var p = events[stack[j]];
                EndEvent(p.name, p.end * scale);
            }
            EndDocument();

            writer.Flush();
            writer.Dispose();
            output.Close();
            output.Dispose();
            output = null;
        }
 public ProfileMarker(string name)
 {
     Session = ProfileSession.CurrentSession;
     if (Session != null)
     {
         Name = name;
         Session.Push(name);
     }
     else
     {
         Name = null;
     }
 }
 public ProfileMarker(string name, ProfileMarker parent)
 {
     Session = parent.Session;
     if (Session != null)
     {
         Name = name;
         Session.Push(name);
     }
     else
     {
         Name = null;
     }
 }
 public ProfileSession(System.IO.Stream o)
 {
     output         = o;
     CurrentSession = this;
 }