Skip to content

Instantly share code, notes, and snippets.

@superswan
Created August 8, 2021 06:34
Show Gist options
  • Save superswan/e8310be725f053abb7f69e72b82cc5b5 to your computer and use it in GitHub Desktop.
Save superswan/e8310be725f053abb7f69e72b82cc5b5 to your computer and use it in GitHub Desktop.
Will spawn XMRig on system Idle
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Timers;
namespace FrozenCoreAgent
{
public class SysTrayApp : Form
{
private static readonly int TimerInterval = Properties.Settings.Default.TimerInterval;
private static readonly int TimeOut = Properties.Settings.Default.TimeOut;
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref Lastinputinfo plii);
internal struct Lastinputinfo
{
public uint CbSize;
public uint DwTime;
}
[STAThread]
public static void Main()
{
Application.Run(new SysTrayApp());
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public SysTrayApp()
{
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
// Icon
trayIcon = new NotifyIcon
{
Text = "Starting",
Icon = new Icon(SystemIcons.Question, 40, 40),
ContextMenu = trayMenu,
Visible = true
};
// Timer
var aTimer = new System.Timers.Timer
{
Interval = TimerInterval * 1000,
Enabled = true
};
aTimer.Elapsed += OnTimedEvent;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing)
trayIcon.Dispose();
base.Dispose(isDisposing);
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
try
{
var ticksIdle = (uint)Environment.TickCount - GetLastInputTime();
if (ticksIdle > TimeOut * 1000)
{
if (!trayIcon.Text.Equals("Working"))
{
trayIcon.ShowBalloonTip(5, "Working!", ticksIdle.ToString(), ToolTipIcon.Info);
trayIcon.Text = "Working";
trayIcon.Icon = new Icon(SystemIcons.Warning, 40, 40);
KillProcess();
foreach (var program in new[]
{
@"C:\Users\Gustav\Documents\FrozenCore\FrozenCore-CPU\xmrig.exe",
@"C:\Users\Gustav\Documents\FrozenCore\xmrig-nvidia-win64\xmrig-nvidia.exe"
})
{
Process process = new Process
{
StartInfo =
{
FileName = program,
Arguments = "",
WindowStyle = ProcessWindowStyle.Minimized
}
};
process.Start();
}
}
}
else
{
if (!trayIcon.Text.Equals("Sleep"))
{
trayIcon.ShowBalloonTip(5, "Sleep!", ticksIdle.ToString(), ToolTipIcon.Info);
trayIcon.Text = "Sleep";
trayIcon.Icon = new Icon(SystemIcons.Shield, 40, 40);
KillProcess();
}
}
}
catch (Exception exception)
{
trayIcon.ShowBalloonTip(5, "Error!", exception.Message, ToolTipIcon.Error);
}
}
public void KillProcess()
{
var processes = Process.GetProcessesByName("xmrig").Union(Process.GetProcessesByName("xmrig-nvidia"));
foreach (var pro in processes)
{
pro.Kill();
}
}
public uint GetLastInputTime()
{
Lastinputinfo lastInPut = new Lastinputinfo();
lastInPut.CbSize = (uint)Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return lastInPut.DwTime;
}
}
}
@superswan
Copy link
Author

Note to self: rewrite in sepples

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment