Skip to content
This repository has been archived by the owner on Jan 25, 2023. It is now read-only.

Commit

Permalink
Refactoring + run startup task as SYSTEM and with slight delay
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackDragonBE committed May 23, 2019
1 parent 2d60bcf commit 7a05a05
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 85 deletions.
51 changes: 22 additions & 29 deletions GPDWin2XTUManager/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using System;
using GPDWin2XTUManager.Properties;
using GPDWin2XTUManager.UpdateChecks;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Management;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
using GPDWin2XTUManager.Properties;
using GPDWin2XTUManager.UpdateChecks;
using Newtonsoft.Json;

namespace GPDWin2XTUManager
{
public partial class MainForm : Form
{
private List<Button> _profileButtons = new List<Button>();
private readonly List<Button> _profileButtons = new List<Button>();
private List<XTUProfile> _xtuProfiles = new List<XTUProfile>();

private GithubRelease _newRelease;
Expand Down Expand Up @@ -59,24 +58,21 @@ public MainForm(string[] args = null)
{
MessageBox.Show("Incorrect number of arguments. Expected 4, but was given " + args.Length);
}

}

}

private async void MainForm_Load(object sender, EventArgs e)
{
Text += " v" + Shared.VERSION.ToString(CultureInfo.InvariantCulture);
ReadCurrentValues();

_newRelease = await UpdateChecker.CheckForUpdates();
_newRelease = await UpdateChecker.CheckForUpdates().ConfigureAwait(true);

if (_newRelease != null)
{
btnUpdateAvailable.Visible = true;
btnUpdateAvailable.Text = "v" + _newRelease.tag_name + " is available!\r\nClick for changelog.";
}

}

private void CheckForXTU()
Expand All @@ -90,7 +86,6 @@ private void CheckForXTU()
}

Environment.Exit(0);

}
else
{
Expand All @@ -111,28 +106,26 @@ private void InitializeUI()
private void CheckIntelDriver()
{
ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_DisplayConfiguration");
string driverVersion = string.Empty;
foreach (ManagementObject mo in mos.Get())
{
foreach (PropertyData property in mo.Properties)
{
//Console.WriteLine(property.Name + "=" + property.Value); // DEBUG
if (property.Name == "DriverVersion")
{
driverVersion = property.Value.ToString();
string driverVersion = property.Value.ToString();
//Console.WriteLine(driverVersion);
txtInfo.Text += "Intel driver version: " + driverVersion + "\r\n";
}
}
}

}

private void PrintDriverWarningAndDownloadlink(string warning)
{
txtInfo.Text += warning + "\r\n";
txtInfo.Text += "Recommended version: 6286" + "\r\n";
txtInfo.Text += "Download link: https://downloadcenter.intel.com/download/27988/Intel-Graphics-Driver-for-Windows-10" + "\r\n(You can disable this check in the settings)\r\n\r\n";
txtInfo.Text += "Recommended version: 6286\r\n";
txtInfo.Text += "Download link: https://downloadcenter.intel.com/download/27988/Intel-Graphics-Driver-for-Windows-10\r\n(You can disable this check in the settings)\r\n\r\n";
}

private void ReadCurrentValues()
Expand All @@ -144,25 +137,25 @@ private void ReadCurrentValues()
string cpuUV = ExecuteInXTUAndGetOutput("-t -id 34").Trim();
string gpuUV = ExecuteInXTUAndGetOutput("-t -id 100").Trim();

txtInfo.Text += "Current values: \r\n" + "Min W: \r\n" + minW + "\r\nMax W: \r\n" + maxW + "\r\nCPU UV: \r\n" + cpuUV + "\r\nGPU UV: \r\n" + gpuUV;
txtInfo.Text += "Current values: \r\nMin W: \r\n" + minW + "\r\nMax W: \r\n" + maxW + "\r\nCPU UV: \r\n" + cpuUV + "\r\nGPU UV: \r\n" + gpuUV;
}
catch
{
txtInfo.Text += "Couldn't read current values.";
}


}

private string ExecuteInXTUAndGetOutput(string command)
{
string output = string.Empty;

ProcessStartInfo processStartInfo = new ProcessStartInfo(Shared.XTU_PATH, command);
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
processStartInfo.UseShellExecute = false;
ProcessStartInfo processStartInfo = new ProcessStartInfo(Shared.XTU_PATH, command)
{
RedirectStandardOutput = true,
RedirectStandardError = true,
WindowStyle = ProcessWindowStyle.Normal,
UseShellExecute = false
};

Process process = Process.Start(processStartInfo);
using (StreamReader streamReader = process.StandardOutput)
Expand Down Expand Up @@ -218,8 +211,6 @@ private void RefreshButtonInfo()
_profileButtons[i].Text = "Create profile...";
_profileButtons[i].Image = Resources.Plus;
}


}
}

Expand Down Expand Up @@ -377,8 +368,10 @@ private void btnSettings_Click(object sender, EventArgs e)

private void OpenSettings()
{
Options frmOptions = new Options();
frmOptions.Profiles = _xtuProfiles;
Options frmOptions = new Options
{
Profiles = _xtuProfiles
};
frmOptions.FormClosed += FrmOptionsOnFormClosed;
frmOptions.ShowDialog();
}
Expand All @@ -397,4 +390,4 @@ private void btnUpdateAvailable_Click(object sender, EventArgs e)
}
}
}
}
}
2 changes: 1 addition & 1 deletion GPDWin2XTUManager/GPDWin2XTUManager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{AE4CCB29-5D20-462A-AFA1-89815F42633B}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<RootNamespace>GPDWin2XTUManager</RootNamespace>
<AssemblyName>GPDWin2XTUManager</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
Expand Down
10 changes: 0 additions & 10 deletions GPDWin2XTUManager/Options.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 3 additions & 12 deletions GPDWin2XTUManager/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using GPDWin2XTUManager.Properties;

namespace GPDWin2XTUManager
{
Expand All @@ -22,7 +21,7 @@ private void Options_Load(object sender, EventArgs e)
CheckForLogonTask();
FillIconList();
lstOptionsProfiles.SelectedIndex = 0;
}
}

/// <summary>
/// Removes existing reg key left by previous versions
Expand Down Expand Up @@ -78,14 +77,7 @@ private void UpdateProfileList()
cmbProfileLogOn.Items.Clear();
cmbProfileLogOn.Items.AddRange(Profiles.ToArray());

if (Profiles.Count > 7)
{
btnAddProfile.Enabled = false;
}
else
{
btnAddProfile.Enabled = true;
}
btnAddProfile.Enabled = Profiles.Count <= 7;
}

private void btnSettingsOK_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -212,7 +204,6 @@ private void txtName_TextChanged(object sender, EventArgs e)
cmbProfileLogOn.SelectedIndex = selectedLogonIndex;
txtName.SelectionStart = selectedTextIndex;
}

}

private void Options_FormClosing(object sender, FormClosingEventArgs e)
Expand Down Expand Up @@ -286,4 +277,4 @@ private void cmbProfileImage_SelectedIndexChanged(object sender, EventArgs e)
Profiles[selected].ProfileImage = image;
}
}
}
}
17 changes: 4 additions & 13 deletions GPDWin2XTUManager/Shared.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
using System;
using GPDWin2XTUManager.Properties;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GPDWin2XTUManager.Properties;
using Microsoft.Win32;
using Newtonsoft.Json;

namespace GPDWin2XTUManager
{
public static class Shared
{
public static readonly decimal VERSION = 1.07m;
public static readonly decimal VERSION = 1.09m;
public static readonly string SETTINGS_PATH = "Settings.json";
public static readonly string RUN_AT_LOGON_PATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
public static readonly string APP_NAME_VALUE = "GPDWin2XTUManager";
public static readonly string XTU_PATH = @"C:\Program Files (x86)\Intel\Intel(R) Extreme Tuning Utility\Client\xtucli.exe";
public static Dictionary<ProfileImage, Bitmap> IMAGE_RESOURCES_DICTIONARY = new Dictionary<ProfileImage, Bitmap>();


public static void SaveProfilesToDisk(List<XTUProfile> profiles)
{
JsonSerializer serializer = new JsonSerializer();
Expand All @@ -47,6 +40,4 @@ public static void PrepareImages()
IMAGE_RESOURCES_DICTIONARY.Add(ProfileImage.Videocard, Resources.Videocard);
}
}


}
}
16 changes: 6 additions & 10 deletions GPDWin2XTUManager/StartupTaskManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Task = Microsoft.Win32.TaskScheduler.Task;

Expand Down Expand Up @@ -45,16 +41,16 @@ public static void CreateTask(string profileName)
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = Shared.APP_NAME_VALUE;
td.Principal.LogonType = TaskLogonType.InteractiveToken;

td.Principal.RunLevel = TaskRunLevel.Highest;
td.Principal.UserId = "SYSTEM";

// Add a trigger that will fire the task at logon
td.Triggers.Add(new LogonTrigger());
td.Triggers.Add(new LogonTrigger() { Delay = new TimeSpan(0, 0, 30) });

// Add an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(Application.ExecutablePath, profileName , null));
// Add an action that will launch the app whenever the trigger fires
td.Actions.Add(new ExecAction(Application.ExecutablePath, profileName, null));

td.Settings.DisallowStartIfOnBatteries = false;
td.Principal.RunLevel = TaskRunLevel.Highest;

// Register the task in the root folder
string taskName = Shared.APP_NAME_VALUE;
Expand All @@ -70,4 +66,4 @@ public static void DeleteTask()
}
}
}
}
}
4 changes: 2 additions & 2 deletions GPDWin2XTUManager/UpdateChecks/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static async Task<GithubRelease> CheckForUpdates()
{
_thisVersion = Shared.VERSION;

GithubRelease newestRelease = await GetNewestReleaseInfo();
GithubRelease newestRelease = await GetNewestReleaseInfo().ConfigureAwait(false);

if (newestRelease != null)
{
Expand Down Expand Up @@ -67,7 +67,7 @@ await Task.Run(() =>
{
// Update check failed, ignore
}
});
}).ConfigureAwait(false);

return release;
}
Expand Down
16 changes: 8 additions & 8 deletions GPDWin2XTUManager/XTUProfile.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json;

namespace GPDWin2XTUManager
{
Expand All @@ -12,14 +7,19 @@ public class XTUProfile
{
[JsonProperty]
public string Name;

[JsonProperty]
public double MinimumWatt;

[JsonProperty]
public double MaximumWatt;

[JsonProperty]
public int CPUUndervolt;

[JsonProperty]
public int GPUUndervolt;

[JsonProperty]
public ProfileImage ProfileImage;

Expand All @@ -32,7 +32,7 @@ public XTUProfile()
GPUUndervolt = 0;
ProfileImage = ProfileImage.Gaming;
}

public XTUProfile(string name, double minimumWatt, double maximumWatt, int cpuUndervolt, int gpuUndervolt, ProfileImage profileImage)
{
Name = name;
Expand All @@ -48,4 +48,4 @@ public override string ToString()
return Name;
}
}
}
}

0 comments on commit 7a05a05

Please sign in to comment.