Cannot detect computer sleep in a service - c#

I wanna make a service that writes on a .txt file whenever you start or stop using your computer.
It can detect computer shutdown, but cannot detect sleeps or wake ups.
Here's the code I use:
public Service1()
{
InitializeComponent();
Microsoft.Win32.SystemEvents.SessionEnded += new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
SystemEvents.PowerModeChanged += OnPowerChange;
CanHandlePowerEvent = true;
}
protected virtual bool OnPowerEvent(System.ServiceProcess.PowerBroadcastStatus powerStatus)
{
if (powerStatus == PowerBroadcastStatus.QuerySuspend || powerStatus == PowerBroadcastStatus.Suspend)
{
WriteToFile("computer turned off at : " + DateTime.Now);
return false;
}
else if (powerStatus == PowerBroadcastStatus.ResumeCritical || powerStatus == PowerBroadcastStatus.ResumeSuspend || powerStatus == PowerBroadcastStatus.ResumeAutomatic)
{
WriteToFile("computer turned on at : " + DateTime.Now);
return true;
}
return true;
}
private void OnPowerChange(object s, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
WriteToFile("computer turned on at : " + DateTime.Now);
break;
case PowerModes.Suspend:
WriteToFile("computer turned off at : " + DateTime.Now);
break;
}
}
I'll be grateful for any help.
note: WriteToFile is a custom method for writing to a .txt file

You can get it working with help of HiddenForm. The changes which are required are:
First: Subscribe to PowerModeChanged even in say HiddenForm_Load
private void HiddenForm_Load(object sender, EventArgs e)
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerChange);
}
Second: Start Message pump using a thread as:
protected override void OnStart(string[] args)
{
//DO work needed in OnStart
// Message Pump will run in separate thread
new Thread(ThreadWork.RunMessagePump).Start();
}
//Thread class to RunMessagePump
public class ThreadWork
{
public static void RunMessagePump()
{
Application.Run(new HiddenForm());
}
}
Third: Check tick box "Allow service to interact with desktop" in Log On tab of properties page.
Reference to example can be found at LINK

Related

Use windows service to run a method every on second

Hi I wrote a method in my program which call a Rest Api and get some information.
I want to call every minute. I fill OnStart and OnStop and all timer_Elapsed in which my method is there. I install my service and start it but It just run just for the first time and never repeat again would if anyone know the solution help me. In advanced I thanked you
On start :
protected override void OnStart(string[] args)
{
///just for log to show program is working
ayandehBLL.Save_Log("Service started...", nameof(OnStart));
if (timer == null)
{
timer = new Timer();
timer.AutoReset = true;
timer.Interval = 3000; //*
Convert.ToDouble(ConfigurationManager.AppSettings["IntervalMinutes"]);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
}
protected override void OnStop()
{
timer.Stop();
timer.Enabled = false;
ayandehBLL.Save_Log("Service stoped", nameof(OnStop));
//WriteErrorLog("Test window service Stoped");
}
-----------------------------
private void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
var a = ayandehBLL.GetProductCode();
ayandehBLL.Save_Log($"Request national code is {a}", "test");
if (a != null)
{
Request_DOM request = new Request_DOM();
request.ProductCode= a;
try
{
var result = ayandehBLL.GetMyProductInfo(request);
if (result != null)
{
ayandehBLL.Save_Log(JsonConvert.SerializeObject(result), nameof(OnStart));
}
else
{
ayandehBLL.Save_Log("GetMyProductInfo() returned null", nameof(OnStart));
}
}
catch (Exception ex)
{
ayandehBLL.Save_Log(ex.Message.ToString(), "OnElapsedTime");
}
}
else
{
ayandehBLL.Save_Log("Request national code is null", "OnElapsedTime");
}
//WriteErrorLog("OnElapsedTime done");
}
The method triggered by a timer is being timed and run in a separate thread. If your main thread reaches the program end, the program will stop and the timer is discontinued. Therefore you have to capture the main thread before the end of the program in a loop where the program won't end until you want it to do so.

Multithread C# serialPort1_DataReceived event does not fire

I read/write data to serial port and I want to see reading on listbox right away. I created a new thread to send command to serial port. I keep the main thread empty, so, it can update the UI and also serial port event handler wont be interrupted with something else.(I am not sure is it right approach?)
The following code works with while (!dataRecieved) { Thread.Sleep(4000); } but does not works with while (!dataRecieved) { Thread.Sleep(100); }.
The problem is if I use 100ms sleep, serial port event handler fire only once and then program stops!(If I debug with breakpoint 100ms works because I create additional time when stepping into the code.) If I wait 4000ms the program works. Also, I check the time between sending data and receiving data from serial port is 200ms. So, 100ms is reasonable.
Here is the code:
public bool dataRecieved = false;
public Form1()
{
InitializeComponent();
}
public void AppendTextBox(string value)
{
this.Invoke((MethodInvoker)delegate { richTextBox1.Text += value + "\n";});
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.Open();
Thread testThread = new Thread(() => sendThread());
testThread.Start();
}
public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
data = serialPort1.ReadLine();
dataRecieved = true;
}
public void sendThread()
{
for(int i = 0; i<10; i++)
{
serialPort1.WriteLine("AT" + i);
// Following line creates odd situation:
// if Thread.Sleep(100), I receive only first data, then program stops(serial port handler doesnt fire!).
// if Thread.Sleep(4000), I receive all data, successfuly works.
// But I do not want to wait 4000ms, because I receive answer from device in 200ms.
while (!dataRecieved) { Thread.Sleep(100); }
AppendTextBox("Received" + "AT" + i);
dataRecieved = false;
}
}
Where I am wrong? Can you please provide a solution?
I even didn't use a new Thead for write and read on SerialPort. You just need use update control in Invoke() is ok. Below is my update on richTextBox. You can change form richTextBox to your listbox.
public void update_RichTextBox(string message)
{
Invoke(new System.Action(() =>
{
txtReceivedData.Text += message;
txtReceivedData.Refresh();
txtReceivedData.SelectionStart = txtReceivedData.Text.Length;
txtReceivedData.ScrollToCaret();
}));
}
and the way to use above void:
if (ComPort.IsOpen)
{
ComPort.Write(_inputdata + "\r");
Form1._Form1.update_RichTextBox(_inputdata + "\r");
string _receviedData = ComPort.ReadExisting();
Form1._Form1.update_RichTextBox(respond);
ComPort.DiscardInBuffer();//delete all data in device's received buffer
ComPort.DiscardOutBuffer();// delete all data in transmit buffer
}
else
{
MessageBox.Show("haven't yet open COM port");
return "FLASE";
}
I use something I call "Cross Thread Linker"
#region Cross Thread Linker
public bool ControlInvokeRequired(Control c, Action a)
{
if (c.InvokeRequired) c.Invoke(new MethodInvoker(delegate { a(); }));
else return false;
return true;
}
void Update_RichTextBox(RichTextBox rtb, string Text)
{
if (ControlInvokeRequired(rtb, () => Update_RichTextBox(rtb, Text))) return;
rtb.AppendText(Text + Environment.NewLine);
}
#endregion
Then:
Update_RichTextBox(richTextBox1, "Text to append");

Delegate loads the Alert Form but I can't use any of the components.. its stuck

The problem is below. Here's my code...
// Contents of Form1.cs
// Usual includes
namespace ProcessMonitor
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Boolean getStatus()
{
// Returns true if the system is active
if (label1.Text.Equals("Active"))
return true;
return false;
}
private void button1_Click(object sender, EventArgs e)
{
if(getStatus())
{
label1.Text = "Not Active";
button1.Text = "Activate";
}
else
{
label1.Text = "Active";
button1.Text = "Deactivate";
}
}
private void Form1_Load(object sender, EventArgs e)
{
Monitor mon = new Monitor(this);
mon.Run();
}
}
}
// Contents of Monitor.cs
// Usual includes
using System.Management;
using System.Diagnostics;
using System.Threading;
namespace ProcessMonitor
{
class Monitor
{
Form1 parent;
private void ShowAlert(Alert al)
{
al.Show();
}
public Monitor(Form1 parent)
{
this.parent = parent;
}
public void InvokeMethod()
{
//This function will be on main thread if called by Control.Invoke/Control.BeginInvoke
Alert frm = new Alert(this.parent);
frm.Show();
}
// This method that will be called when the thread is started
public void Run()
{
var query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 0, 0, 1),
"TargetInstance isa \"Win32_Process\");
while (true)
{
using (var watcher = new ManagementEventWatcher(query))
{
ManagementBaseObject mo = watcher.WaitForNextEvent();a
//MessageBox.Show("Created process: " + ((ManagementBaseObject)mo["TargetInstance"])["Name"] + ",Path: " + ((ManagementBaseObject)mo["TargetInstance"])["ExecutablePath"]);
ManagementBaseObject o = (ManagementBaseObject)mo["TargetInstance"];
String str = "";
foreach (PropertyData s in o.Properties)
{
str += s.Name + ":" + s.Value + "\n";
}
this.parent.Invoke(new MethodInvoker(InvokeMethod), null);
}
}
}
}
}
Alert.cs is just a blank form with a label that says “new process has started”. I intend to display the name of the process and location, pid, etc. by passing it to this alert form via the Thread (i.e. class Monitor). I have deliberately made the thread load in form_load so that I can resolve this error first. Adding it as a thread properly after the main form loads fully is a later task. I need to fix this first..
The delegate creates the Alert form but I can’t click on it, its just stuck. Need help to solve this.
Your while loop in Run is blocking the UI thread.
by passing it to this alert form via the Thread
You never actually create a new thread or task here - you just run code which executes in the UI thread, and causes an infinite loop. This will prevent the main form, as well as your Alert form, from ever displaying messages.
You need to push this into a background thread in order for it to work, ie:
private void Form1_Load(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(_ =>
{
Monitor mon = new Monitor(this);
mon.Run();
});
}

Value of volatile variable doesn't change in multi-thread

I have a winform application that runs in background with a BackgroundWorker that has an infinite loop that execute something every hour. My UI Form class is something like this:
public partial class frmAutoScript : Form
{
private volatile bool _isDownloading = false;
private bool IsDownloading { get { return this._isDownloading; } set { this._isDownloading = value; } }
public frmAutoScript()
{
InitializeComponent();
this.RunAutoSynchronization();
}
private void RunAutoSynchronization()
{
bool isDownloading = this.IsDownloading;
BackgroundWorker bgwDownloader = new BackgroundWorker();
bgwDownloader.WorkerReportsProgress = true;
bgwDownloader.ProgressChanged += (sndr, evnt) =>
{
if (evnt.ProgressPercentage == 2)
isDownloading = this.IsDownloading;
else
{
this.IsDownloading = evnt.ProgressPercentage == 1;
isDownloading = this.IsDownloading;
}
};
bgwDownloader.DoWork += (sndr, evnt) =>
{
while (true)
{
if (DateTime.Now.Hour == 16 &&
DateTime.Now.Minute == 0)
{
try
{
bgwDownloader.ReportProgress(2);
if (!isDownloading)
{
bgwDownloader.ReportProgress(1);
new Downloader().Download();
}
bgwDownloader.ReportProgress(0);
}
catch { }
}
System.Threading.Thread.Sleep(60000);
}
};
bgwDownloader.RunWorkerAsync();
}
}
And in that frmAutoScript, I also have a button named btnDownload that when clicked, it will download and change the value of the volatile varialbe _isDownloading. The event of the button is something like this:
private void btnDownload_Click(object sender, EventArgs e)
{
if (IsDownloading)
MessageBox.Show("A download is currently ongoing. Please wait for the download to finish.",
"Force Download", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
else
{
this.IsDownloading = true;
BackgroundWorker bgwDownloader = new BackgroundWorker();
bgwDownloader.DoWork += (sndr, evnt) =>
{
try
{
new Downloader().Download();
}
catch(Exception ex)
{
MessageBox.Show("An error occur during download. Please contact your system administrator.\n Exception: " +
ex.GetType().ToString() + "\nError Message:\n" + ex.Message + " Stack Trace:\n" + ex.StackTrace, "Download Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
};
bgwDownloader.RunWorkerCompleted += (sndr, evnt) =>
{
this.IsDownloading = false;
};
bgwDownloader.RunWorkerAsync();
}
}
But when I click the button btnDownload and the _isDownloading is set to true, and when the system time hit the 4:00 PM, the new Downloader().Download(); is executed again eventhough the _isDownloading is set to true. Why was it like this?
My code is in C#, framework 4, project is in winforms, build in Visual Studio 2010 Pro.
Your code is not testing against the volatile field - it is testing against isDownloading, which looks like a "local", but (because it is captured) is in fact a regular (non-volatile) field. So: either use some kind of memory barrier, or force that to be a volatile read. Or more simply: get rid of isDownloading completely, and check against the property.
Incidentally, the cache-defeating properties of volatile are not the intent of the keyword, but rather: a consequence. It'll work, but personally I'd suggest writing the code to work by intent rather than by consequence, perhaps using either a simple lock or something like Interlocked.

MSNP-Sharp Example fails to login, gives SocketException

I've just downloaded the MSNP-Sharp library with the aim of creating my own messaging client, however I am struggling to get the example to sign in. The code all compiles and runs, but when I provide my login details and select "Login" I almost immediately get the following SocketException:
"No connection could be made because the target machine actively refused it 64.4.9.254:1863"
I've stepped through the code and it's the messenger.Connect() function that is causing this, somewhat obviously. When I run the example I only change the login and password details. I am running Windows 7 x86 with the latest version of Windows Live Messenger.
I have tried disabling my antivirus, even going as far as to temporarily uninstall it in case that was the error.
I have also tried disabling Windows Firewall, with no luck.
Firstly, use the stable version of MSNPSharp (that is, 3.0). Since it is a SocketException, this may relate to a problem within the internet protocol (a firewall for instance). Try to ensure that nothing is blocking the program from accessing to the MSN protocol. Since you have said you have disabled your Windows Firewall, could there be anything else that could be blocking it?
Secondly, have you tried using MSN Messenger Live for a test. If that works, MSNPSharp client should probably work too. Ensure you have .NET Framework 2.0 or within their version of the .NET Framework. If it constantly appears to be a problem, I don't believe this is a problem from the MSNPSharp client (I'm not sure however).
here is a demo,i hope it would be useful
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Color;
namespace MSNRobot
{
using MSNPSharp;
using MSNPSharp.Core;
using MSNPSharp.DataTransfer;
class RobotConversation
{
private Conversation _conversation = null;
private RobotMain _robotmain = null;
public RobotConversation(Conversation conv, RobotMain robotmain)
{
Console.WriteLine("==> Struct a conversation");
_conversation = conv;
_conversation.Switchboard.TextMessageReceived += new EventHandler<TextMessageEventArgs>(Switchboard_TextMessageReceived);
_conversation.Switchboard.SessionClosed += new EventHandler<EventArgs>(Switchboard_SessionClosed);
_conversation.Switchboard.ContactLeft += new EventHandler<ContactEventArgs>(Switchboard_ContactLeft);
_robotmain = robotmain;
}
//online status
private void Switchboard_TextMessageReceived(object sender, TextMessageEventArgs e)
{
Console.WriteLine("==>Received Msg From " + e.Sender.Mail + " Content:\n" + e.Message.Text);
//echo back ///////////// TODO /////////////////
_conversation.Switchboard.SendTextMessage(e.Message);
}
private void Switchboard_SessionClosed(object sender, EventArgs e)
{
Console.WriteLine("==>Session Closed, Remove conversation");
_conversation.Switchboard.Close();
_conversation = null;
_robotmain.RobotConvlist.Remove(this);
}
private void Switchboard_ContactLeft(object sender, ContactEventArgs e)
{
Console.WriteLine("==>Contact Left.");
}
}
class RobotMain
{
private Messenger messenger = new Messenger();
private List<RobotConversation> _convs = new List<RobotConversation>(0);
public RobotMain()
{
messenger.NameserverProcessor.ConnectionEstablished += new EventHandler<EventArgs>(NameserverProcessor_ConnectionEstablished);
messenger.Nameserver.SignedIn += new EventHandler<EventArgs>(Nameserver_SignedIn);
messenger.Nameserver.SignedOff += new EventHandler<SignedOffEventArgs>(Nameserver_SignedOff);
messenger.NameserverProcessor.ConnectingException += new EventHandler<ExceptionEventArgs>(NameserverProcessor_ConnectingException);
messenger.Nameserver.ExceptionOccurred += new EventHandler<ExceptionEventArgs>(Nameserver_ExceptionOccurred);
messenger.Nameserver.AuthenticationError += new EventHandler<ExceptionEventArgs>(Nameserver_AuthenticationError);
messenger.Nameserver.ServerErrorReceived += new EventHandler<MSNErrorEventArgs>(Nameserver_ServerErrorReceived);
messenger.Nameserver.ContactService.ReverseAdded += new EventHandler<ContactEventArgs>(Nameserver_ReverseAdded);
messenger.ConversationCreated += new EventHandler<ConversationCreatedEventArgs>(messenger_ConversationCreated);
messenger.Nameserver.OIMService.OIMReceived += new EventHandler<OIMReceivedEventArgs>(Nameserver_OIMReceived);
messenger.Nameserver.OIMService.OIMSendCompleted += new EventHandler<OIMSendCompletedEventArgs>(OIMService_OIMSendCompleted);
}
public List<RobotConversation> RobotConvlist
{
get
{
return _convs;
}
}
private void NameserverProcessor_ConnectionEstablished(object sender, EventArgs e)
{
//messenger.Nameserver.AutoSynchronize = true;
Console.WriteLine("==>Connection established!");
}
private void Nameserver_SignedIn(object sender, EventArgs e)
{
messenger.Owner.Status = PresenceStatus.Online;
Console.WriteLine("==>Signed into the messenger network as " + messenger.Owner.Name);
}
private void Nameserver_SignedOff(object sender, SignedOffEventArgs e)
{
Console.WriteLine("==>Signed off from the messenger network");
}
private void NameserverProcessor_ConnectingException(object sender, ExceptionEventArgs e)
{
//MessageBox.Show(e.Exception.ToString(), "Connecting exception");
Console.WriteLine("==>Connecting failed");
}
private void Nameserver_ExceptionOccurred(object sender, ExceptionEventArgs e)
{
// ignore the unauthorized exception, since we're handling that error in another method.
if (e.Exception is UnauthorizedException)
return;
Console.WriteLine("==>Nameserver exception:" + e.Exception.ToString());
}
private void Nameserver_AuthenticationError(object sender, ExceptionEventArgs e)
{
Console.WriteLine("==>Authentication failed:" + e.Exception.InnerException.Message);
}
private void Nameserver_ServerErrorReceived(object sender, MSNErrorEventArgs e)
{
// when the MSN server sends an error code we want to be notified.
Console.WriteLine("==>Server error received:" + e.MSNError.ToString());
}
void Nameserver_ReverseAdded(object sender, ContactEventArgs e)
{
//Contact contact = e.Contact;
//contact.OnAllowedList = true;
//contact.OnPendingList = false;
//messenger.Nameserver.ContactService.AddNewContact(contact.Mail);
Console.WriteLine("==>ReverseAdded contact mail:" + e.Contact.Mail);
//messenger.Nameserver.AddNewContact(
e.Contact.OnAllowedList = true;
e.Contact.OnForwardList = true;
}
private void messenger_ConversationCreated(object sender, ConversationCreatedEventArgs e)
{
Console.WriteLine("==>Conversation created");
_convs.Add(new RobotConversation(e.Conversation, this));
}
//offline status
void Nameserver_OIMReceived(object sender, OIMReceivedEventArgs e)
{
Console.WriteLine("==>OIM received at : " + e.ReceivedTime + " From : " +
e.NickName + " (" + e.Email + ") " + e.Message);
TextMessage message = new TextMessage(e.Message);
message.Font = "Trebuchet MS";
//message.Color = Color.Brown;
message.Decorations = TextDecorations.Bold;
Console.WriteLine("==>Echo back");
messenger.OIMService.SendOIMMessage(e.Email, message.Text);
}
void OIMService_OIMSendCompleted(object sender, OIMSendCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine("OIM Send Error:" + e.Error.Message);
}
}
public void BeginLogin(string account, string password)
{
if (messenger.Connected)
{
Console.WriteLine("==>Disconnecting from server");
messenger.Disconnect();
}
// set the credentials, this is ofcourse something every MSNPSharp program will need to implement.
messenger.Credentials = new Credentials(account, password, MsnProtocol.MSNP16);
// inform the user what is happening and try to connecto to the messenger network.
Console.WriteLine("==>Connecting to server...");
messenger.Connect();
//displayImageBox.Image = global::MSNPSharpClient.Properties.Resources.loading;
//loginButton.Tag = 1;
//loginButton.Text = "Cancel";
// note that Messenger.Connect() will run in a seperate thread and return immediately.
// it will fire events that informs you about the status of the connection attempt.
// these events are registered in the constructor.
}
/// <summary>
/// main()
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string robot_acc = "";
string robot_passwd = "";
if (args.Length == 0)
{
Console.WriteLine("USAGE:MSNRobot.exe <msn_account> [password]");
return;
}
robot_acc = args[0];
if (args.Length == 2)
robot_passwd = args[1];
else
{
Console.WriteLine("Password for " + robot_acc + ":");
robot_passwd = Console.ReadLine();
}
RobotMain app = new RobotMain();
app.BeginLogin(robot_acc, robot_passwd);
while (true)
{
Console.WriteLine("I am a MSN robot:" + robot_acc);
Console.ReadLine();
}
}
}
}
Have you tried the example client for MSNPSharp?

Categories

Resources