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

Commit

Permalink
Tentative fix again for Start with windows with a schedule tasked.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyrules committed Jan 15, 2022
1 parent ee0707e commit 680a0ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 79 deletions.
93 changes: 26 additions & 67 deletions VRSuspender/MainFormViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
using Microsoft.Win32;
using Windows.Storage;
using VRSuspender.Objects;
using IWshRuntimeLibrary;
using System.Windows.Navigation;
using Microsoft.Win32.TaskScheduler;
using System.Security.Principal;

namespace VRSuspender
{
Expand Down Expand Up @@ -65,7 +66,7 @@ public MainFormViewModel()

}

public async Task Initialize()
public async System.Threading.Tasks.Task Initialize()
{
await RefreshProcess();
}
Expand Down Expand Up @@ -152,7 +153,7 @@ async void StopWatch_EventArrived(object sender, EventArrivedEventArgs e)
await ApplyStopVRActionToProcess();

}
public async Task ApplyStopVRActionToProcess()
public async System.Threading.Tasks.Task ApplyStopVRActionToProcess()
{
if(!VrRunning) return;
await RefreshProcess();
Expand Down Expand Up @@ -234,9 +235,9 @@ private void LoadTrackedProcessProfiles()
}
}

private static async Task RefreshProcess(TrackedProcess process)
private static async System.Threading.Tasks.Task RefreshProcess(TrackedProcess process)
{
await Task.Run(() =>
await System.Threading.Tasks.Task.Run(() =>
{
Process[] p = Process.GetProcessesByName(process.ProcessName);
if (p.Length > 0)
Expand Down Expand Up @@ -283,7 +284,7 @@ await Task.Run(() =>
});
}

private async Task RefreshProcess()
private async System.Threading.Tasks.Task RefreshProcess()
{
foreach (TrackedProcess process in _listTrackedProcess)
{
Expand Down Expand Up @@ -369,9 +370,9 @@ private bool CanAutoDetectProcess()
return VrRunning == false;
}

private async Task AutoDetectProcess()
private async System.Threading.Tasks.Task AutoDetectProcess()
{
await Task.Run(() =>
await System.Threading.Tasks.Task.Run(() =>
{
foreach (TrackedProcess profile in ProfileDBManager.ListProfiles)
{
Expand Down Expand Up @@ -493,79 +494,37 @@ private void SaveSettings()

private void SetStartWithWindows(bool start)
{
string startup = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
string lnk = Path.Combine(startup, "VRSuspender.lnk");

if(start)
{
if(!System.IO.File.Exists(lnk))
{
WshShell shell = new();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(lnk);

shortcut.Description = "VR Suspender";
shortcut.TargetPath = Environment.ProcessPath;
shortcut.WorkingDirectory = Path.GetFullPath(Environment.ProcessPath);
shortcut.Save();
WriteToLog("VRSuspender will start with windows.");
}
else
{
WriteToLog("Shortcut already exists. Skipping creation.");
}
}
else
using TaskService ts = new();
if (start)
{
try
{
System.IO.File.Delete(lnk);
WriteToLog("VRSuspender has been removed from Windows startup applications.");
}
catch(DirectoryNotFoundException)
{
WriteToLog("Unable to delete Shortcut. Folder not found.");
}
catch(FileNotFoundException)
{
WriteToLog("Unable to delete Shortcut. Shortcut not found.");
}
catch (UnauthorizedAccessException)
{
WriteToLog("Unable to delete Shortcut. Permission denied.");
}
catch (Exception ex)
{
WriteToLog($"Unable to delete Shortcut. {ex.Message}");
}
}


/*
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

if (start)
{
if (rkApp.GetValue("VRSuspender") == null)
if (!ts.RootFolder.Tasks.Any(t => t.Name == "VRSuspender"))
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Start VR Suspender with windows.";
td.Principal.RunLevel = TaskRunLevel.Highest;
td.Triggers.Add(new LogonTrigger() { Enabled = true, UserId = WindowsIdentity.GetCurrent().Name });
td.Actions.Add(new ExecAction(Environment.ProcessPath, null, Path.GetDirectoryName(Environment.ProcessPath)));
ts.RootFolder.RegisterTaskDefinition(@"VRSuspender", td);
WriteToLog("VRSuspender will start with windows.");
string exe = $"\"{Environment.ProcessPath}\"";
rkApp.SetValue("VRSuspender", exe);
}
else
{
WriteToLog("VR Suspender is already set to start with Windows.");
WriteToLog("Scheduled Task already exists. Skipping creation.");
}
}
else
{
WriteToLog("VRSuspender has been removed from Windows startup applications.");
rkApp.DeleteValue("VRSuspender", false);
}*/

ts.RootFolder.DeleteTask(@"VRSuspender", false);
}
SaveSettings();


}

private async Task AddProces()
private async System.Threading.Tasks.Task AddProces()
{
EditTrackedProcessForm ProcessEditor = new()
{
Expand Down Expand Up @@ -672,7 +631,7 @@ private bool CanEditSuspendedProcess()

private void WriteToLog(string message)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
Application.Current.Dispatcher.BeginInvoke(new System.Action(() => {
string msg = $"[{DateTime.Now}] - {message}.";
Log.Insert(0, msg);
LastLogMessage = msg;
Expand Down
13 changes: 1 addition & 12 deletions VRSuspender/VRSuspender.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@
<None Remove="Resources\suspend.png" />
</ItemGroup>

<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="AdonisUI" Version="1.17.1" />
<PackageReference Include="AdonisUI.ClassicTheme" Version="1.17.1" />
Expand All @@ -50,6 +38,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="System.Management" Version="6.0.0" />
<PackageReference Include="TaskScheduler" Version="2.9.3" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 680a0ec

Please sign in to comment.