#region Copyright
// Diagnostic Explorer, a .Net diagnostic toolset
// Copyright (C) 2010 Cameron Elliot
//
// This file is part of Diagnostic Explorer.
//
// Diagnostic Explorer is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Diagnostic Explorer is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Diagnostic Explorer. If not, see <http://www.gnu.org/licenses/>.
//
// http://diagexplorer.sourceforge.net/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiagnosticExplorer.Silverlight.WebServices;
using System.Windows.Threading;
using System.ComponentModel;
using DiagnosticExplorer.Silverlight.Controls;
namespace DiagnosticExplorer.Silverlight.Controls
{
public partial class DiagnosticDisplayer : UserControl
{
public DiagnosticDisplayer()
{
InitializeComponent();
Loaded += HandleLoaded;
}
private void HandleLoaded(object sender, RoutedEventArgs e)
{
if (DesignerProperties.GetIsInDesignMode(this)) return;
}
public static DependencyProperty GroupProperty = DependencyProperty.Register(
"Group", typeof(DiagGroup), typeof(DiagnosticDisplayer), new PropertyMetadata(HandleGroupChanged));
public DiagGroup Group
{
get { return (DiagGroup)GetValue(GroupProperty); }
set { SetValue(GroupProperty, value); }
}
private static void HandleGroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DiagnosticDisplayer displayer = (DiagnosticDisplayer)d;
displayer.SetupGroup();
}
private void SetupGroup()
{
if (CurrentContext != null)
{
CurrentContext.IsActive = false;
CurrentContext.RequestTimer.Stop();
CurrentContext = null;
}
DiagContext newContext = new DiagContext(Group);
newContext.RequestTimer = new DispatcherTimer();
newContext.RequestTimer.Interval = App.GetRefreshInterval();
newContext.RequestTimer.Tick += (sender, e) => BeginRequest(newContext);
CurrentContext = newContext;
BeginRequest(newContext);
}
private void HandleEventSelected(object sender, EventSelectedEventArgs e)
{
eventDetail.Event = e.Event;
traceViewer.TraceText = e.Event == null ? null : e.Event.Detail;
tabControl.SelectedIndex = traceViewer.IsShowingTrace ? 1 : 0;
traceViewer.ExpandTree();
}
private DiagContext CurrentContext
{
get { return (DiagContext)DataContext; }
set { DataContext = value; }
}
private void BeginRequest(DiagContext context)
{
context.RequestTimer.Stop();
context.RequestStart = DateTime.Now;
context.Status = "Requesting Diagnostics...";
DiagnosticWebProxyClient client = App.CreateDiagClient();
client.GetDiagnosticsAsync(context.Process.Id, context.RequestContext, context);
client.GetDiagnosticsCompleted += HandleDiagnosticsArrived;
}
private void HandleDiagnosticsArrived(object sender, GetDiagnosticsCompletedEventArgs e)
{
DiagContext context = (DiagContext)e.UserState;
if (context.IsActive)
{
try
{
if (e.Error != null)
{
context.Status = "Failed";
context.Error = e.Error.Message;
txtError.Height = 20;
}
else if (e.Result.ExceptionMessage != null)
{
context.Process.Uri = e.Result.Uri;
context.Status = "Failed";
context.Error = e.Result.ExceptionMessage;
txtError.Height = 20;
}
else
{
DiagContext toMerge = new DiagContext(context.Process, e.Result);
context.Merge(toMerge);
TimeSpan duration = DateTime.Now - context.RequestStart;
context.Status = string.Format("Request took {0:N2} seconds", duration.TotalSeconds);
context.Error = null;
txtError.Height = 0;
}
}
finally
{
context.RequestTimer.Start();
}
}
}
}
}