Skip to content

Commit

Permalink
Small performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ShirazAdam committed Dec 10, 2024
1 parent 6cfc9f0 commit 965a775
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 47 deletions.
34 changes: 18 additions & 16 deletions TinyWall/PipeClientEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.IO.Pipes;
using System.Threading;
using NeoSmart.AsyncLock;
using System;
using System.IO.Pipes;
using System.Threading.Tasks;

namespace pylorak.TinyWall
{
public class PipeClientEndpoint
{
private readonly object _senderSyncRoot = new();
private readonly AsyncLock _asyncLock = new();
private readonly string _mPipeName;

public PipeClientEndpoint(string clientPipeName)
Expand All @@ -16,24 +18,24 @@ public PipeClientEndpoint(string clientPipeName)
private void SendRequest(TwRequest req)
{
TwMessage ret = TwMessageComError.Instance;
lock (_senderSyncRoot)

Task.Run(async () =>
{
// In case of a communication error,
// retry a small number of times.
for (int i = 0; i < 2; ++i)
using (await _asyncLock.LockAsync())
{
var resp = SendRequest(req.Request);
if (resp.Type != MessageType.COM_ERROR)
for (var i = 0; i < 2; ++i)
{
ret = resp;
break;
var resp = SendRequest(req.Request);
if (resp.Type != MessageType.COM_ERROR)
{
ret = resp;
break;
}
await Task.Delay(TimeSpan.FromMilliseconds(200));
}

Thread.Sleep(200);
}
}

req.Response = ret;
req.Response = ret;
});
}

private TwMessage SendRequest(TwMessage msg)
Expand Down
22 changes: 13 additions & 9 deletions TinyWall/Processes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ private Task UpdateListAsync()
ServicePidMap servicePids = new ServicePidMap();

Process[] procs = Process.GetProcesses();

if (!string.IsNullOrWhiteSpace(_searchItem))
procs = procs.Where(p => p.ProcessName.ToLower().Contains(_searchItem.ToLower())).ToArray();

foreach (var t in procs)
{
using Process p = t;
Expand Down Expand Up @@ -150,16 +154,16 @@ private Task UpdateListAsync()
listView.Items.Clear();
listView.ListViewItemSorter = new ListViewItemComparer(0);

if (!string.IsNullOrWhiteSpace(_searchItem))
itemColl = itemColl.Where(item =>
{
var subItem = item.SubItems;
//if (!string.IsNullOrWhiteSpace(_searchItem))
// itemColl = itemColl.Where(item =>
// {
// var subItem = item.SubItems;

return (subItem[0].Text.ToLower().Contains(_searchItem) ||
subItem[1].Text.ToLower().Contains(_searchItem) ||
subItem[2].Text.ToLower().Contains(_searchItem));
})
.ToList();
// return (subItem[0].Text.ToLower().Contains(_searchItem) ||
// subItem[1].Text.ToLower().Contains(_searchItem) ||
// subItem[2].Text.ToLower().Contains(_searchItem));
// })
// .ToList();

listView.Items.AddRange(itemColl.ToArray());
listView.EndUpdate();
Expand Down
4 changes: 3 additions & 1 deletion TinyWall/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ static int Main(string[] args)
try
{
// Prevent Windows Error Reporting running for us
Utils.SafeNativeMethods.WerAddExcludedApplication(Utils.ExecutablePath, true);
if (File.Exists(Utils.ExecutablePath))
Utils.SafeNativeMethods.WerAddExcludedApplication(Utils.ExecutablePath, true);

}
catch
{
Expand Down
31 changes: 19 additions & 12 deletions TinyWall/Services.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal partial class ServicesForm : Form
{
private string? _selectedServiceName;
private string? _selectedServiceExec;
private string _searchText = string.Empty;
private string _searchItem = string.Empty;

internal static ServiceSubject? ChooseService(IWin32Window? parent = null)
{
Expand Down Expand Up @@ -123,6 +123,12 @@ private Task UpdateListAsync()

ServiceController[] services = ServiceController.GetServices();

if (!string.IsNullOrWhiteSpace(_searchItem))
services = services.Where(s =>
s.ServiceName.ToLower().Contains(_searchItem.ToLower())
|| s.DisplayName.ToLower().Contains(_searchItem.ToLower())
).ToArray();

foreach (var srv in services)
{
try
Expand All @@ -138,18 +144,19 @@ private Task UpdateListAsync()
}
}

if (!string.IsNullOrWhiteSpace(_searchText))
itemColl = itemColl.Where(items =>
{
var subItem = items.SubItems;

return subItem[0].Text.ToLower().Contains(_searchText) || subItem[1].Text.ToLower().Contains(_searchText) ||
subItem[2].Text.ToLower().Contains(_searchText);
}).ToList();

Utils.SetDoubleBuffering(listView, true);
listView.BeginUpdate();
listView.ListViewItemSorter = new ListViewItemComparer(0);

//if (!string.IsNullOrWhiteSpace(_searchItem))
// itemColl = itemColl.Where(items =>
// {
// var subItem = items.SubItems;

// return subItem[0].Text.ToLower().Contains(_searchItem) || subItem[1].Text.ToLower().Contains(_searchItem) ||
// subItem[2].Text.ToLower().Contains(_searchItem);
// }).ToList();

listView.Items.AddRange(itemColl.ToArray());
listView.EndUpdate();

Expand Down Expand Up @@ -194,13 +201,13 @@ private async void btnSearch_Click(object sender, EventArgs e)
return;
}

_searchText = txtBxSearch.Text.ToLower();
_searchItem = txtBxSearch.Text.ToLower();
await UpdateListAsync();
}

private async void btnClear_Click(object sender, EventArgs e)
{
_searchText = string.Empty;
_searchItem = string.Empty;
txtBxSearch.Text = string.Empty;

await UpdateListAsync();
Expand Down
2 changes: 2 additions & 0 deletions TinyWall/TinyWall.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.26100.1742" />
<PackageReference Include="NeoSmart.AsyncLock" Version="3.2.1" />
<PackageReference Include="NeoSmart.Synchronization" Version="2.0.0" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
25 changes: 16 additions & 9 deletions TinyWall/UwpPackagesForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ private Task UpdateListAsync()
var itemColl = new List<ListViewItem>();

var packages = UwpPackage.GetPackages();

if (!string.IsNullOrWhiteSpace(_searchItem))
packages = packages.Where(p =>
p.Name.ToLower().Contains(_searchItem.ToLower())
|| p.Publisher.ToLower().Contains(_searchItem.ToLower())
).ToArray();

foreach (var package in packages)
{
// Add list item
Expand All @@ -106,15 +113,15 @@ private Task UpdateListAsync()
listView.Items.Clear();
listView.ListViewItemSorter = new ListViewItemComparer(0);

if (!string.IsNullOrWhiteSpace(_searchItem))
itemColl = itemColl.Where(item =>
{
var subItem = item.SubItems;

return (subItem[0].Text.ToLower().Contains(_searchItem) ||
subItem[1].Text.ToLower().Contains(_searchItem));
})
.ToList();
//if (!string.IsNullOrWhiteSpace(_searchItem))
// itemColl = itemColl.Where(item =>
// {
// var subItem = item.SubItems;

// return (subItem[0].Text.ToLower().Contains(_searchItem) ||
// subItem[1].Text.ToLower().Contains(_searchItem));
// })
// .ToList();

listView.Items.AddRange(itemColl.ToArray());
listView.EndUpdate();
Expand Down

0 comments on commit 965a775

Please sign in to comment.