I'm somewhat new to C# on the whole and I'm developing a small program to render my backlog of CAD stuff when I'm idle. I'm using the MouseKeyboardActivityMonitor library found here: https://globalmousekeyhook.codeplex.com/ but having major problems such as input frozen upon the launch of my program and from there out no user input is detected by the hooks. I am using Windows 8.1 x64 and compiling both the DLL and my own executable towards .NET 4.0
Note: I am using Version 3 of the project
This is my code (just a small test of the hooks):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MouseKeyboardActivityMonitor;
using MouseKeyboardActivityMonitor.WinApi;
using System.Windows.Forms;
namespace HookTest1
{
class Program
{
static MouseHookListener mouseListener;
static KeyboardHookListener keyListener;
static void Main(string[] args)
{
Activate();
while (true)
{
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
}
private static void OnUserActivity()
{
Console.Write("UserAct");
}
private static void Activate()
{
mouseListener = new MouseHookListener(new GlobalHooker());
mouseListener.Enabled = true;
mouseListener.MouseDown += OnMouseActivity;
mouseListener.MouseMove += OnMouseActivity;
mouseListener.MouseWheel += OnMouseActivity;
keyListener = new KeyboardHookListener(new GlobalHooker());
mouseListener.Enabled = true;
keyListener.KeyDown += OnKeyActivity;
}
private static void OnMouseActivity(Object sender, System.Windows.Forms.MouseEventArgs e)
{
OnUserActivity();
}
private static void OnKeyActivity(Object sender, System.Windows.Forms.KeyEventArgs e)
{
OnUserActivity();
}
}
}
Thanks for any and all help!
You may have added this somewhere and just removed it in your sample... but have you called
Application.Run();
in Main()? As I understand it, you're attempting to hook window events without a message loop going. Application.Run will get it running.
Related
I made a simple C# app some time ago (as a console app), now I'm making a new version as a Windows Application in Sharpdevelop. I'm pretty new to C# and graphical applications and I got this confusing error.
What I want to do: on the UI, I have a button which checks if COM3 serial port is opened and if not, open it, if it's open, close it and write a message about it on a listbox. The problem is that wherever I put the serial handling part, I get errors about it not being in the current context, or get an "Object reference not set to an instance of an object" error.
I created the app with Sharpdevelop's template, so I have code in several files:
Program.cs:
using System;
using System.Windows.Forms;
namespace SerialClient_v2
{
class Program
{
[STAThread]
void Main(string[] args)
{
SerialHandle SH=new SerialHandle();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(SH));
}
}
}
Mainform.cs:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO.Ports;
namespace SerialClient_v2
{
public partial class MainForm : Form
{
private SerialHandle _sh;
public MainForm(SerialHandle sh)
{
InitializeComponent();
_sh = sh;
}
void ConnectBtnClick(object sender, EventArgs e)
{
try{
if(_sh._serialPort.IsOpen){
listBox1.Items.Add(DictionaryClass.strDisconnecting);
//Program._serialPort.Close();
}
else{
//Program._serialPort.Open();
listBox1.Items.Add(DictionaryClass.strConnecting);
}
}
catch (Exception ex)
{
listBox1.Items.Add("Error opening/writing to serial port :: " + ex.Message);
}
}
}
}
SerialHandle.cs: (this is my file, the stuff which used to be in the Main function in the console app comes here. DictionaryClass is just a translation helping class to easily switch between two languages)
using System;
using System.IO.Ports;
namespace SerialClient_v2
{
public class SerialHandle
{
bool SerialComm;
public SerialPort _serialPort;
public SerialHandle()
{
SerialPort _serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.None;
_serialPort.ReadTimeout = 1000;
_serialPort.Open();
}
}
public static class DictionaryClass{
//DICTIONARY:
public static string strConnecting="Initializing serial communication!";
public static string strDisconnecting="Closing serial port";
}
}
The problem is that SH._serialPort.IsOpen can't be checked as it's not in the context. SH is created in the Main function, so I don't get why it's not seen. Sorry for the beginner question.
The simplest solution is to pass the SH object instance to the main form:
In SerialHandle.cs (use _sh._serialPort.IsOpen):
private SerialHandle _sh;
public MainForm(SerialHandle sh)
{
_sh = sh;
}
In Program.cs:
Application.Run(new MainForm(SH));
Simply move the initialization statement SerialHandle SH=new SerialHandle(); into MainForm.cs class
I created two projects(Proj_1,Proj_2), Proj_1 contains Proj_1_Program.cs and ProjectOneClass.cs, Proj_2 contains Proj_2_Program.cs,and I need OnInformed trigger both Informed1 and Informed2 this is how I got so far:
//Proj_1_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
class Proj_1_Program
{
static void Main(string[] args)
{
ProjectOneClass obj1 = new ProjectOneClass();
obj1.Inform += new EventHandler(Informed1);
obj1.InformNow();
Console.ReadLine();
}
private static void Informed1(object sender, EventArgs e)
{
Console.WriteLine("Informed1");
}
}
}
//ProjectOneClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CrossProjectEvent
{
public class ProjectOneClass
{
public event EventHandler Inform;
public void InformNow()
{
OnInformed(new EventArgs());
}
private void OnInformed(EventArgs eventArgs) // I want this method both trigger Informed1 and Informed2
{
if (Inform != null)
{
Inform(this, eventArgs);
}
}
}
}
//Proj_2_Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrossProjectEvent;
namespace Proj_2
{
public class ProjectTwoClass
{
public ProjectOneClass obj_proj_1;
public ProjectTwoClass()
{
obj_proj_1 = new ProjectOneClass();
obj_proj_1.Inform += new EventHandler(Informed2);
}
private static void Informed2(object sender, EventArgs e)
{
Console.WriteLine("Informed2");
}
}
class Project2
{
static void Main(string[] args)
{
}
}
}
But it seems like only Informed1 being triggered, so how to fix this? thanks!
This is a typical problem requiring inter-process communication. There are a billion different techniques and approaches possible for it.
One solution would be to use Remoting using named pipes (Sample), but also TCP and NetSockets are possible. This might be one of the simplest solutions.
If you are building a larger application requiring a lot of inter-process communication, the actor model, especially the AKKA.NET libary, would be worth mentioning.
But these are just a few of the options you have.
I can detect the network connection change event while running a C# code, how would I register an exe when Windows detects this event. What all details would I need. Below is how I am using this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
testing t = new testing();
Console.Read();
}
}
public class testing{
public testing()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
Console.WriteLine("network is available");
}
}
}
}
What you could maybe do is in your method that is triggered to start a new process and execute your exe
Good morning,
I have several questions but I am not sure which is the important one to ask so I'll first state my overall problem. I can't close my Winform App. I have searched and found many answers but they either don't work, I don't understand or both.
If I do all my work and then call Application.Exit the form never closes. Same results if I put this.Close. However if I place a button on the form and call Application.Exit it closes the form.
I obviously do not understand the flow, I hope it is clear to someone what I am trying to do. As a non-programmer I have been piecing this project together for a few months and this is my last step - Close the form after work is complete if it was run from command line with arguments. I would try longer to work it out but my Visual Studio trial runs out this week so I turn to the experts :)
Thank you,
Todd
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(String[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//add if
if (args.Length == 0)
{
Application.Run(new Form1("Form"));
}
else
{
Application.Run(new Form1(args[0]));
}
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProgramCSToormTest
{
public partial class Form1 : Form
{
string CLArg1;
string ReturnText;
public Form1(string Arg1)
{
InitializeComponent();
if (Arg1 != null)
{
CLArg1 = Arg1;
textBox1.Text = Display(CLArg1);
//button1.Enabled = false;
}
else
{
textBox1.Text = "click button to start";
}
Application.Exit(); //This seems to be ignored
}
public void button1_Click(object sender, EventArgs e)
{
CLArg1 = null;
textBox1.Text = Display("Hello World");
Application.Exit();
}
public string Display(string DisplayText)
{
if (CLArg1 != null)
{
ReturnText = CLArg1;
return(ReturnText);
}
else
{
ReturnText = DisplayText;
return(ReturnText);
}
}
}
}
See this question. Application.Close() only works if an application has been created. This is done by calling Application.Run(). Now. in your code you call Application.Exit() from the constructor of your form. Which is executed before the Application.Run() that is needed to create the application.
To fix this, either wait until after Application.Run(). Or, if you want to quit the application in the constructor use Environment.Exit(int statusCode). When using Environment.Exit(int statusCode) keep this in mind though.
You can't close the Application when the Form is Loading from the Program class. Try calling the Exit method after Loading the Form:
private void Form1_Load(object sender, EventArgs e)
{
if (CLArg1 != String.Empty)
Application.Exit();
}
I am trying to record audio in C# using NAudio. After looking at the NAudio Chat Demo, I used some code from there to record.
Here is the code:
using System;
using NAudio.Wave;
public class FOO
{
static WaveIn s_WaveIn;
static void Main(string[] args)
{
init();
while (true) /* Yeah, this is bad, but just for testing.... */
System.Threading.Thread.Sleep(3000);
}
public static void init()
{
s_WaveIn = new WaveIn();
s_WaveIn.WaveFormat = new WaveFormat(44100, 2);
s_WaveIn.BufferMilliseconds = 1000;
s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
s_WaveIn.StartRecording();
}
static void SendCaptureSamples(object sender, WaveInEventArgs e)
{
Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
}
}
However, the eventHandler is not being called. I am using .NET version 'v2.0.50727' and compiling it as:
csc file_name.cs /reference:Naudio.dll /platform:x86
If this is your whole code, then you are missing a message loop. All the eventHandler specific events requires a message loop. You can add a reference to Application or Form as per your need.
Here is an example by using Form:
using System;
using System.Windows.Forms;
using System.Threading;
using NAudio.Wave;
public class FOO
{
static WaveIn s_WaveIn;
[STAThread]
static void Main(string[] args)
{
Thread thread = new Thread(delegate() {
init();
Application.Run();
});
thread.Start();
Application.Run();
}
public static void init()
{
s_WaveIn = new WaveIn();
s_WaveIn.WaveFormat = new WaveFormat(44100, 2);
s_WaveIn.BufferMilliseconds = 1000;
s_WaveIn.DataAvailable += new EventHandler<WaveInEventArgs>(SendCaptureSamples);
s_WaveIn.StartRecording();
}
static void SendCaptureSamples(object sender, WaveInEventArgs e)
{
Console.WriteLine("Bytes recorded: {0}", e.BytesRecorded);
}
}
Just use WaveInEvent instead of WaveIn and the code will work. Then the handling happens on a separate thread instead of in a window message loop, which isn't available in a console application.
Further reading:
https://github.com/naudio/NAudio/wiki/Understanding-Output-Devices#waveout-and-waveoutevent
(The feature was added in 2012, so at the time of the question it wasn't available)