Я сделал простой прослушиватель портов, который прослушивает порт, и если отправленный запрос удовлетворительный, он переводит компьютер в спящий режим. Однако приложение работает при запуске, когда я перехожу в компьютер, а затем просыпаюсь; до разблокировки Windows приложение не ответит. Есть ли способ заставить приложение реагировать еще до разблокировки Windows? (Python консольное приложение будет прослушивать даже перед разблокировкой, но я не хочу, чтобы окно cmd открывалось на панели задач.)
Код доступен ниже
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace remoteShutdownandOtherStuffServer
{
public partial class Form1 : Form
{
CancellationTokenSource ts = new CancellationTokenSource();
public Form1()
{
InitializeComponent();
//SystemEvents.PowerModeChanged += OnPowerChange;
}
//I have tried adding OnPowerChange event, but to no avail.
//private void OnPowerChange(object s, PowerModeChangedEventArgs e)
//{
// switch (e.Mode)
// {
// case PowerModes.Resume:
// ts.Cancel();
// CancellationToken ct = ts.Token;
// Task.Factory.StartNew(() =>
// {
// remoteShutdownListener(ct);
// }, ct);
// break;
// case PowerModes.Suspend:
// break;
// }
//}
private void remoteShutdownListener(CancellationToken ct)
{
TcpListener server = null;
textBox1.Invoke((MethodInvoker)delegate {
textBox1.Text = "";
textBox2.Text = "";
});
try
{
Int32 port = 10000;
IPAddress localAddr = IPAddress.Parse("192.168.1.5");
server = new TcpListener(localAddr, port);
server.Start();
Byte[] bytes = new Byte[1024];
String data = null;
while (true)
{
if (ct.IsCancellationRequested)
break;
TcpClient client = server.AcceptTcpClient();
data = null;
NetworkStream stream = client.GetStream();
int i;
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
bool allowed = false;
textBox1.Invoke((MethodInvoker)delegate {
textBox1.Text += "Received: " + data.ToString();
textBox2.Text += client.Client.RemoteEndPoint.ToString() ;
});
if(data.Contains("go to sleep"))
{
allowed = true;
}
var ipStrArray = ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString().Split('.');
if(allowed && ipStrArray[0] =="192" && ipStrArray[1] == "168" && ipStrArray[2] == "1")
{
MessageBox.Show("it works");
Application.SetSuspendState(PowerState.Hibernate, true, true);
}
}
client.Close();
}
}
catch (Exception exc1)
{
//Console.WriteLine("SocketException: {0}", exc1);
}
finally
{
server.Stop();
}
}
private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.Icon = (System.Drawing.Icon)Properties.Resources.remoteIcon;
CancellationToken ct = ts.Token;
Task.Factory.StartNew(() =>
{
remoteShutdownListener(ct);
}, ct);
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
Close();
}
else if (e.Button == MouseButtons.Left)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
}
//private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
//{
// this.Show();
// this.WindowState = FormWindowState.Normal;
//}
}
}