Skip to content

Commit

Permalink
Replace BeginInvoke with 5 times faster .NET Core compatible 'Task' a…
Browse files Browse the repository at this point in the history
…lternative.
  • Loading branch information
EJocys committed Apr 11, 2019
1 parent e2b1f7f commit 99bb58f
Show file tree
Hide file tree
Showing 20 changed files with 1,648 additions and 1,590 deletions.
2 changes: 1 addition & 1 deletion x360ce.App.Beta/Controls/CloudUserControl.cs
Expand Up @@ -108,7 +108,7 @@ public void Add(CloudItem item)

public void Add<T>(CloudAction action, T[] items, bool split = false, Guid[] checksums = null)
{
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
var allow = MainForm.Current.OptionsPanel.InternetAutoSaveCheckBox.Checked;
if (!allow)
Expand Down
6 changes: 3 additions & 3 deletions x360ce.App.Beta/Controls/GamesGridUserControl.cs
Expand Up @@ -38,9 +38,9 @@ public void InitControl()
// WORKAROUND: Remove SelectionChanged event.
GamesDataGridView.SelectionChanged -= GamesDataGridView_SelectionChanged;
GamesDataGridView.DataSource = SettingsManager.UserGames.Items;
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
BeginInvoke((MethodInvoker)delegate ()
{
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
ControlsHelper.BeginInvoke(() =>
{
GamesDataGridView.SelectionChanged += GamesDataGridView_SelectionChanged;
GamesDataGridView_SelectionChanged(GamesDataGridView, new EventArgs());
});
Expand Down
5 changes: 3 additions & 2 deletions x360ce.App.Beta/Controls/LogUserControl.cs
Expand Up @@ -4,6 +4,7 @@
using JocysCom.ClassLibrary.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
using JocysCom.ClassLibrary.Controls;

namespace x360ce.App.Controls
{
Expand Down Expand Up @@ -93,7 +94,7 @@ public LogItem Add(string format, params object[] args)
public void Add(LogItem e)
{
if (IsDisposed || Disposing || !IsHandleCreated) return;
BeginInvoke((Action)(() =>
ControlsHelper.BeginInvoke(() =>
{
// Calculate time from last inserted item.
if (lastItem != null)
Expand All @@ -106,7 +107,7 @@ public void Add(LogItem e)
lastItem = e;
AddGridRow(LogDataGridView, LogList, e, LogSize);
}
}));
});
}

// Contains number for how many row add/remove actions to perform scroll.
Expand Down
4 changes: 2 additions & 2 deletions x360ce.App.Beta/Controls/OptionsUserControl.cs
Expand Up @@ -458,8 +458,8 @@ void RefreshViGEmStatus()
// Get Virtual Bus and HID Guardian status.
var bus = DInput.VirtualDriverInstaller.GetViGemBusDriverInfo();
var hid = DInput.VirtualDriverInstaller.GetHidGuardianDriverInfo();
BeginInvoke((MethodInvoker)delegate ()
{
ControlsHelper.BeginInvoke(() =>
{
// Update Bus status.
var busStatus = bus.DriverVersion == 0
? "Not installed"
Expand Down
8 changes: 3 additions & 5 deletions x360ce.App.Beta/Controls/PadControl.cs
Expand Up @@ -14,6 +14,7 @@
using System.Reflection;
using JocysCom.ClassLibrary.Runtime;
using JocysCom.ClassLibrary.Controls;
using JocysCom.ClassLibrary.Extensions;

namespace x360ce.App.Controls
{
Expand Down Expand Up @@ -133,7 +134,7 @@ public void InitPadData()
MappedDevicesDataGridView.SelectionChanged -= MappedDevicesDataGridView_SelectionChanged;
MappedDevicesDataGridView.DataSource = mappedItems;
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(()=>
{
MapNameComboBox.DataSource = SettingsManager.Layouts.Items;
MapNameComboBox.DisplayMember = "Name";
Expand Down Expand Up @@ -340,12 +341,9 @@ void ComboBox_DropDown(object sender, EventArgs e)
var oldLeft = cbx.Left;
// Move default DropDown away from the screen.
cbx.Left = -10000;
var del = new ComboBoxDropDownDelegate(ComboBoxDropDown);
BeginInvoke(del, new object[] { cbx, oldLeft });
ControlsHelper.BeginInvoke(() => { ComboBoxDropDown(cbx, oldLeft); });
}

delegate void ComboBoxDropDownDelegate(ComboBox cbx, int oldLeft);

void ComboBoxDropDown(ComboBox cbx, int oldLeft)
{
cbx.IntegralHeight = !cbx.IntegralHeight;
Expand Down
12 changes: 6 additions & 6 deletions x360ce.App.Beta/Controls/PerformanceTestUserControl.cs
Expand Up @@ -127,7 +127,7 @@ void CheckTimer()
{
if (!IsHandleCreated)
return;
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
CpuTextBox.Text = "";
});
Expand Down Expand Up @@ -162,7 +162,7 @@ private void CpuTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
_Counter = new CpuUsage();
}
var process_cpu_usage = _Counter.GetUsage();
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
CpuTextBox.Text = process_cpu_usage.HasValue
? string.Format("{0:0.000} %", Math.Round(process_cpu_usage.Value, 3))
Expand Down Expand Up @@ -294,7 +294,7 @@ private void TestButton_Click(object sender, EventArgs e)

private void _timer_TestFinished(object sender, EventArgs e)
{
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
MessageBox.Show(_TestTimer.TestResults);
});
Expand Down Expand Up @@ -346,7 +346,7 @@ void CheckDevices()
list.Add(string.Format("{0,4} unknown devices", unknown.Length));
if (Disposing || IsDisposed)
return;
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
LogTextBox.Text = "";
var result = MessageBox.Show("Do you want to remove:\r\n\r\n" + string.Join("\r\n", list), "Remove Devices", MessageBoxButtons.YesNo);
Expand Down Expand Up @@ -374,13 +374,13 @@ void CleanupDevices()
if (Disposing || IsDisposed)
return;
var item = list[i];
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
LogTextBox.Text = string.Format("{0}/{1} - {2}", i + 1, list.Count, item.Description);
});
DeviceDetector.RemoveDevice(item.DeviceId);
}
BeginInvoke((MethodInvoker)delegate ()
ControlsHelper.BeginInvoke(() =>
{
LogTextBox.Text = string.Format("{0} devices removed", list.Count);
});
Expand Down
19 changes: 10 additions & 9 deletions x360ce.App.Beta/Controls/ProgramsGridUserControl.cs
@@ -1,4 +1,5 @@
using JocysCom.ClassLibrary.Runtime;
using JocysCom.ClassLibrary.Controls;
using JocysCom.ClassLibrary.Runtime;
using JocysCom.ClassLibrary.Web.Services;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,10 +29,10 @@ public void InitPanel()
ProgramsDataGridView.SelectionChanged -= ProgramsDataGridView_SelectionChanged;
ProgramsDataGridView.DataSource = SettingsManager.Programs.Items;
var handle = this.Handle;
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
BeginInvoke((MethodInvoker)delegate ()
{
ProgramsDataGridView.SelectionChanged += ProgramsDataGridView_SelectionChanged;
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
ControlsHelper.BeginInvoke(() =>
{
ProgramsDataGridView.SelectionChanged += ProgramsDataGridView_SelectionChanged;
ProgramsDataGridView_SelectionChanged(ProgramsDataGridView, new EventArgs());
});
UpdateControlsFromPrograms();
Expand Down Expand Up @@ -251,10 +252,10 @@ void RefreshProgramsListFromCloud()

void ProgramsWebServiceClient_GetProgramsCompleted(object sender, SoapHttpClientEventArgs e)
{
// Make sure method is executed on the same thread as this control.
BeginInvoke((MethodInvoker)delegate ()
{
MainForm.Current.AddTask(TaskName.GetPrograms);
// Make sure method is executed on the same thread as this control.
ControlsHelper.BeginInvoke(() =>
{
MainForm.Current.AddTask(TaskName.GetPrograms);
if (e.Error != null)
{
var error = e.Error.Message;
Expand Down
9 changes: 5 additions & 4 deletions x360ce.App.Beta/Controls/UserDevicesUserControl.cs
Expand Up @@ -6,6 +6,7 @@
using x360ce.Engine;
using x360ce.App.Forms;
using JocysCom.ClassLibrary.ComponentModel;
using JocysCom.ClassLibrary.Controls;

namespace x360ce.App.Controls
{
Expand Down Expand Up @@ -36,10 +37,10 @@ public void AttachDataSource(SortableBindingList<UserDevice> data)
ControllersDataGridView_SelectionChanged(DevicesDataGridView, new EventArgs());
return;
}
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
BeginInvoke((MethodInvoker)delegate ()
{
DevicesDataGridView.SelectionChanged += ControllersDataGridView_SelectionChanged;
// WORKAROUND: Use BeginInvoke to prevent SelectionChanged firing multiple times.
ControlsHelper.BeginInvoke(() =>
{
DevicesDataGridView.SelectionChanged += ControllersDataGridView_SelectionChanged;
ControllersDataGridView_SelectionChanged(DevicesDataGridView, new EventArgs());
});
}
Expand Down

0 comments on commit 99bb58f

Please sign in to comment.