Skip to content

Commit

Permalink
Fix (undeclared bug): ProcessHelper.GetProcessOwnerWMI was missing a …
Browse files Browse the repository at this point in the history
…cache update for newly launched processes.

Created TimerBasedPage base class for all pages using a timer.
  • Loading branch information
wokhan committed Oct 29, 2020
1 parent aa2127e commit 7d2287a
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 209 deletions.
9 changes: 8 additions & 1 deletion Common/Processes/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ public static string[] GetProcessOwnerWMI(int owningPid, ref Dictionary<int, str
{
using var searcher = new ManagementObjectSearcher("SELECT ProcessId, Name, ExecutablePath, CommandLine FROM Win32_Process");
using var results = searcher.Get();
previousCache = results.Cast<ManagementObject>().ToDictionary(r => (int)(uint)r["ProcessId"], r => new[] { (string)r["Name"], (string)r["ExecutablePath"], (string)r["CommandLine"] });
previousCache = results.Cast<ManagementObject>().ToDictionary(r => (int)r["ProcessId"], r => new[] { (string)r["Name"], (string)r["ExecutablePath"], (string)r["CommandLine"] });
}

if (!previousCache.ContainsKey(owningPid))
{
using var searcher = new ManagementObjectSearcher($"SELECT ProcessId, Name, ExecutablePath, CommandLine FROM Win32_Process WHERE ProcessId = {owningPid}");
using var r = searcher.Get().Cast<ManagementObject>().FirstOrDefault();
previousCache.Add((int)r["ProcessId"], new[] { (string)r["Name"], (string)r["ExecutablePath"], (string)r["CommandLine"] });
}

return previousCache[owningPid];
Expand Down
11 changes: 5 additions & 6 deletions Common/UI/Theme.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</Style>

<Style TargetType="Button">
<Setter Property="ext:CustomAdorner.CornerRadius" Value="2" />
<Setter Property="ext:CustomAdorner.CornerRadius" Value="4" />
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="10,5" />
Expand All @@ -56,7 +56,7 @@
</Style>

<Style TargetType="ToggleButton">
<Setter Property="ext:CustomAdorner.CornerRadius" Value="2" />
<Setter Property="ext:CustomAdorner.CornerRadius" Value="4" />
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="10,5" />
Expand All @@ -76,7 +76,6 @@

<Style TargetType="ComboBox">
<Setter Property="ext:CustomAdorner.PreserveTemplate" Value="true" />
<Setter Property="ext:CustomAdorner.CornerRadius" Value="10" />
<Setter Property="IsEditable" Value="False" />
<Setter Property="Background" Value="WhiteSmoke" />
<Setter Property="Template">
Expand All @@ -90,14 +89,14 @@
</Style>
</ToggleButton.Style>
</ToggleButton>
<Popup x:Name="PART_Popup" IsOpen="{TemplateBinding IsDropDownOpen}" StaysOpen="False" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide" Margin="0">
<Popup x:Name="PART_Popup" IsOpen="{TemplateBinding IsDropDownOpen}" StaysOpen="False" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide" Placement="Bottom">
<Grid Background="{TemplateBinding Background}" MinWidth="{TemplateBinding FrameworkElement.ActualWidth}" MaxHeight="{TemplateBinding MaxDropDownHeight}" Margin="4,0,4,4">
<Border Background="{TemplateBinding Background}" CornerRadius="5">
<Border Background="{TemplateBinding Background}" CornerRadius="4">
<Border.Effect>
<DropShadowEffect Color="LightGray" ShadowDepth="2" />
</Border.Effect>
</Border>
<Border CornerRadius="5">
<Border CornerRadius="4">
<ScrollViewer>
<ItemsPresenter KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
Expand Down
1 change: 0 additions & 1 deletion Console/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Wokhan.WindowsFirewallNotifier.Common;component/UI/Theme.xaml" />
<ResourceDictionary Source="pack://application:,,,/Wokhan.UI;component/Xaml/Styles/Controls.xaml" />
<ResourceDictionary Source="Resources/Images.xaml" />
</ResourceDictionary.MergedDictionaries>
<BindingConverters:BooleanNegateConverter x:Key="booleanNegator" />
Expand Down
6 changes: 4 additions & 2 deletions Console/UI/Pages/AdapterInfo.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.AdapterInfo"
<pages:TimerBasedPage
xmlns:pages="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Pages"
x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.AdapterInfo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -155,4 +157,4 @@
</ScrollViewer>
</Grid>
</DockPanel>
</Page>
</pages:TimerBasedPage>
45 changes: 8 additions & 37 deletions Console/UI/Pages/AdapterInfo.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.ComponentModel;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Threading;
using Wokhan.WindowsFirewallNotifier.Console.Helpers.ViewModels;
Expand All @@ -12,61 +14,30 @@ namespace Wokhan.WindowsFirewallNotifier.Console.UI.Pages
/// <summary>
/// Interaction logic for AdapterInfo.xaml
/// </summary>
public partial class AdapterInfo : Page, INotifyPropertyChanged
public partial class AdapterInfo : TimerBasedPage, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private static bool trackingState = true;

public bool IsTrackingEnabled
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
get { return timer.IsEnabled; }
set { timer.IsEnabled = value; }
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public List<double> Intervals { get { return new List<double> { 0.5, 1, 5, 10 }; } }
public override List<double> Intervals => new List<double> { 0.5, 1, 5, 10 };

private DispatcherTimer timer = new DispatcherTimer();

private double _interval = 1;
public double Interval
{
get { return _interval; }
set { _interval = value; timer.Interval = TimeSpan.FromSeconds(value); }
}

private List<ExposedInterfaceView> interfacesCollection = NetworkInterface.GetAllNetworkInterfaces().Select(n => new ExposedInterfaceView(n)).OrderByDescending(n => n.Information.OperationalStatus.ToString()).ToList();

public IEnumerable<ExposedInterfaceView> AllInterfaces { get { return interfacesCollection; } }

public AdapterInfo()
{
this.Loaded += AdapterInfo_Loaded;
this.Unloaded += AdapterInfo_Unloaded;

InitializeComponent();

timer.Tick += Timer_Tick;
timer.Interval = TimeSpan.FromSeconds(Interval);
}

private void AdapterInfo_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
timer.Start();
}

private void AdapterInfo_Unloaded(object sender, System.Windows.RoutedEventArgs e)
{
timer.Stop();
}

private void Timer_Tick(object sender, EventArgs e)
protected override async Task OnTimerTick(object sender, EventArgs e)
{
var allnet = NetworkInterface.GetAllNetworkInterfaces();
foreach (var i in allnet)
Expand Down
6 changes: 4 additions & 2 deletions Console/UI/Pages/Connections.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Connections"
<pages:TimerBasedPage
xmlns:pages="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Pages"
x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Connections"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -107,4 +109,4 @@
</DataGrid>
</Grid>
</DockPanel>
</Page>
</pages:TimerBasedPage>
40 changes: 3 additions & 37 deletions Console/UI/Pages/Connections.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;

using Wokhan.WindowsFirewallNotifier.Common.Net.IP;
using Wokhan.WindowsFirewallNotifier.Console.Helpers.ViewModels;

Expand All @@ -16,63 +15,30 @@ namespace Wokhan.WindowsFirewallNotifier.Console.UI.Pages
/// <summary>
/// Interaction logic for Connections.xaml
/// </summary>
public partial class Connections : Page
public partial class Connections : TimerBasedPage
{
private const double ConnectionTimeoutRemove = 5.0; //seconds
private const double ConnectionTimeoutDying = 2.0; //seconds
private const double ConnectionTimeoutNew = 1000.0; //milliseconds

public bool IsTrackingEnabled
{
get { return timer.IsEnabled; }
set { timer.IsEnabled = value; }
}

public List<int> Intervals => new List<int> { 1, 5, 10 };

private DispatcherTimer timer = new DispatcherTimer();

public ObservableCollection<Connection> lstConnections { get; } = new ObservableCollection<Connection>();

public ListCollectionView connectionsView { get; set; }

private int _interval = 1;
public int Interval
{
get { return _interval; }
set { _interval = value; timer.Interval = TimeSpan.FromSeconds(value); }
}

private bool running;

public Connections()
{
this.Loaded += Connections_Loaded;
this.Unloaded += Connections_Unloaded;

//TODO: Use BindingOperations.EnableCollectionSynchronization(lstConnections, locker); instead of Dispatcher invocations

connectionsView = (ListCollectionView)CollectionViewSource.GetDefaultView(lstConnections);
connectionsView.GroupDescriptions.Add(new PropertyGroupDescription("GroupKey"));
connectionsView.SortDescriptions.Add(new SortDescription("GroupKey", ListSortDirection.Ascending));

InitializeComponent();

timer.Interval = TimeSpan.FromSeconds(Interval);
timer.Tick += timer_Tick;
}

private void Connections_Unloaded(object sender, RoutedEventArgs e)
{
timer.Stop();
}

void Connections_Loaded(object sender, RoutedEventArgs e)
{
timer.Start();
}

async void timer_Tick(object sender, EventArgs e)
protected override async Task OnTimerTick(object sender, EventArgs e)
{
if (running)
{
Expand Down
6 changes: 4 additions & 2 deletions Console/UI/Pages/Map.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Map"
<pages:TimerBasedPage
xmlns:pages="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Pages"
x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Map"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -195,4 +197,4 @@
</Grid>
</Grid>
</DockPanel>
</Page>
</pages:TimerBasedPage>
44 changes: 8 additions & 36 deletions Console/UI/Pages/Map.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
Expand All @@ -21,16 +22,10 @@ namespace Wokhan.WindowsFirewallNotifier.Console.UI.Pages
/// <summary>
/// Logique d'interaction pour Map.xaml
/// </summary>
public partial class Map : Page, INotifyPropertyChanged
public partial class Map : TimerBasedPage
{
public Location CurrentCoordinates { get; private set; }

public bool IsTrackingEnabled
{
get { return timer.IsEnabled; }
set { timer.IsEnabled = value; }
}

private bool _isFullRouteDisplayed;
public bool IsFullRouteDisplayed
{
Expand All @@ -51,17 +46,12 @@ public MapMode Mode
set { _mode = value; NotifyPropertyChanged(nameof(Mode)); }
}

public List<int> Intervals { get { return new List<int> { 1, 5, 10 }; } }

private DispatcherTimer timer = new DispatcherTimer();

public ObservableCollection<MapGroupedView> Connections { get; } = new ObservableCollection<MapGroupedView>();

public ListCollectionView ConnectionsView { get; }
public ObservableCollection<GeoConnection2> ConnectionsRoutes { get; } = new ObservableCollection<GeoConnection2>();

private int _interval = 1;


public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(string propertyName)
Expand All @@ -70,31 +60,16 @@ protected void NotifyPropertyChanged(string propertyName)
}


public int Interval
{
get { return _interval; }
set { _interval = value; timer.Interval = TimeSpan.FromSeconds(value); }
}

public Map()
{
this.Loaded += Map_Loaded;
this.Unloaded += Map_Unloaded;


ConnectionsView = (ListCollectionView)CollectionViewSource.GetDefaultView(ConnectionsRoutes);
ConnectionsView.IsLiveGrouping = true;
ConnectionsView.GroupDescriptions.Add(new PropertyGroupDescription("Owner"));

InitializeComponent();

timer.Interval = TimeSpan.FromSeconds(Interval);
timer.Tick += timer_Tick;

}

private void Map_Unloaded(object sender, RoutedEventArgs e)
{
timer.Stop();
}

async void Map_Loaded(object sender, RoutedEventArgs e)
Expand All @@ -105,9 +80,9 @@ async void Map_Loaded(object sender, RoutedEventArgs e)
MessageBox.Show("The IP database cannot be found. The Map feature is disabled.", "Missing database");
return;
}

await GeoConnection2.InitCache().ConfigureAwait(true);

try
{
CurrentCoordinates = GeoConnection2.CurrentCoordinates;
Expand All @@ -117,16 +92,13 @@ async void Map_Loaded(object sender, RoutedEventArgs e)
// TODO: add log
CurrentCoordinates = new Location(0, 0);
}

initialPoint.SetValue(MapLayer.PositionProperty, CurrentCoordinates);

ProgressStack.Visibility = Visibility.Collapsed;

//await Dispatcher.InvokeAsync(() => timer_Tick(null, null));
timer.Start();
}

void timer_Tick(object sender, EventArgs e)
protected override async Task OnTimerTick(object sender, EventArgs e)
{
foreach (var c in IPHelper.GetAllConnections(true)
.Where(co => co.State == ConnectionStatus.ESTABLISHED && !co.IsLoopback && co.OwnerModule != null))
Expand Down
6 changes: 4 additions & 2 deletions Console/UI/Pages/Monitor.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<Page x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Monitor"
<pages:TimerBasedPage
xmlns:pages="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Pages"
x:Class="Wokhan.WindowsFirewallNotifier.Console.UI.Pages.Monitor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Expand Down Expand Up @@ -72,4 +74,4 @@
</Grid>
</Grid>
</DockPanel>
</Page>
</pages:TimerBasedPage>
Loading

0 comments on commit 7d2287a

Please sign in to comment.