Skip to content

Commit

Permalink
Update Readme & Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
valnoxy committed Mar 24, 2024
1 parent 28e2323 commit cfbef3a
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 187 deletions.
28 changes: 0 additions & 28 deletions CHANGELOG.md

This file was deleted.

25 changes: 0 additions & 25 deletions CheckIP.CLI/CheckIP.CLI.sln

This file was deleted.

14 changes: 0 additions & 14 deletions CheckIP.CLI/CheckIP.CLI/CheckIP.CLI.csproj

This file was deleted.

32 changes: 0 additions & 32 deletions CheckIP.CLI/CheckIP.CLI/Program.cs

This file was deleted.

2 changes: 1 addition & 1 deletion CheckIP.Windows/CheckIP/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<TextBlock Grid.Column="1" x:Name="DebugString" Text="Debug" Margin="0,17,50,0" Foreground="Red" TextAlignment="Right" HorizontalAlignment="Right"/>
<TextBlock Grid.Column="1" x:Name="DebugString" Margin="0,17,50,0" Foreground="Red" TextAlignment="Right" HorizontalAlignment="Right"/>
</Grid>
</Grid>
</ui:FluentWindow>
40 changes: 20 additions & 20 deletions CheckIP.Windows/CheckIP/Pages/MyIP.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using Microsoft.Win32;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows;
using System.Net.NetworkInformation;
using System.ComponentModel;
using CheckIP.Common;
using System.Collections.Generic;
using System.Net.Http;

namespace CheckIP
{
Expand All @@ -17,17 +17,17 @@ namespace CheckIP
public partial class MyIP
{
private static string _myIp;
private readonly Queue<Action> workQueue = new Queue<Action>();
private readonly object queueLock = new object();
private readonly BackgroundWorker worker = new BackgroundWorker();
private readonly Queue<Action> _workQueue = new Queue<Action>();
private readonly object _queueLock = new object();
private readonly BackgroundWorker _worker = new BackgroundWorker();

public MyIP()
{
InitializeComponent();

_myIp = Task.Run(_GetIPAddress).GetAwaiter().GetResult();
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
_worker.DoWork += Worker_DoWork;
_worker.RunWorkerCompleted += Worker_RunWorkerCompleted;

// Trigger NetworkChange
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAvailabilityChanged;
Expand All @@ -52,19 +52,19 @@ private void FetchAndParse()
});
}

[Obsolete("Will be replaced to a async version soon.")]
private static async Task<string> _GetIPAddress()
{
string result = null;
using var client = new HttpClient();
try
{
result = await Task.Run(() => new WebClient().DownloadString("https://ifconfig.me/ip"));
result = await client.GetStringAsync("https://ifconfig.me/ip");
}
catch
{
try // Alternative source
{
result = await Task.Run(() => new WebClient().DownloadString("https://api.ipify.org"));
result = await client.GetStringAsync("https://api.ipify.org");
}
catch
{
Expand Down Expand Up @@ -164,24 +164,24 @@ private void ExportBtn_OnClick(object sender, RoutedEventArgs e)

private void EnqueueWork(Action work)
{
lock (queueLock)
lock (_queueLock)
{
workQueue.Enqueue(work);
if (!worker.IsBusy)
_workQueue.Enqueue(work);
if (!_worker.IsBusy)
{
worker.RunWorkerAsync();
_worker.RunWorkerAsync();
}
}
}

private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
Action work = null;
lock (queueLock)
lock (_queueLock)
{
if (workQueue.Count > 0)
if (_workQueue.Count > 0)
{
work = workQueue.Dequeue();
work = _workQueue.Dequeue();
}
}

Expand All @@ -190,11 +190,11 @@ private void Worker_DoWork(object sender, DoWorkEventArgs e)

private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
lock (queueLock)
lock (_queueLock)
{
if (workQueue.Count > 0)
if (_workQueue.Count > 0)
{
worker.RunWorkerAsync();
_worker.RunWorkerAsync();
}
}
}
Expand All @@ -203,7 +203,7 @@ private void NetworkChange_NetworkAvailabilityChanged(object sender, EventArgs e
{
EnqueueWork(() =>
{
_myIp = Task.Run(() => _GetIPAddress()).GetAwaiter().GetResult();
_myIp = Task.Run(_GetIPAddress).GetAwaiter().GetResult();
FetchAndParse();
});
}
Expand Down
Loading

0 comments on commit cfbef3a

Please sign in to comment.