I am using my desktop application written for Windows 8 in C# to provide the readings of different device sensors on clipboard. Now, there is an external application (I dont have any control over its structure) that my application will be interacting with. Both the applications use a mutual text file as a switch. When the external app needs my readings, it renames the text file to 'SensorsTurn.txt' and puts a trigger word on the clipboard, such as ("sensors") When my application sees that the file has been named such, it reads the clipboard for trigger, collect data accordingly, puts it on clipboard and renames the text file back to 'RBsTurn.txt'. The problem is, I need my program to continuously check for the name of that file as long as it is running. One very basic way that I thought of was to throw the program into an infinite while loop. But that obviously is a very bad approach. When I see my application in the task manager, it is taking up crazy amount of CPU processing which it shouldn't. Another suggestion that I found was to make my loop a background thread, this was a little more efficient. But the time between two consecutive readings is slowed down. The guy that I am working for reported this: "The first time I try to write to the clipboard, it writes very quickly but on subsequent writes it takes about 3 seconds (I have often had two program communicate with the clipboard). I suspect that your program is not releasing the clipboard in some way. As soon as I am able to write to the clipboard, I change the file name and get your data back immediately. So the problem stems from the clipboard write.... " Here is a part of the code:
namespace sampleApplication
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Initialize sensor variables
LightSensorReading _newLightSensorReading;
LightSensor _LightSensor = LightSensor.GetDefault();
string _pathString = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "dummy");
private void Form1_Load(object sender, EventArgs e)
{
//InitializeDevices();
System.IO.Directory.CreateDirectory(_pathString);
string _filePathSensors = System.IO.Path.Combine(_pathString, "SensorsTurn.txt");
string _filePathRBsTurn = System.IO.Path.Combine(_pathString, "RBsTurn.txt");
string _triggerString = "";
int x = 1;
Thread th = new Thread(() =>
{
while (x == 1)
{
if (System.IO.File.Exists(_filePathSensors))
{
_triggerString = Clipboard.GetText();
switch (_triggerString)
{
case "sensors":
if (_LightSensor != null)
{
_newLightSensorReading = _LightSensor.GetCurrentReading();
string _deviceReading = "LightSensor" + "," + _newLightSensorReading.IlluminanceInLux.ToString();
Clipboard.SetText(_deviceReading);
System.IO.File.Move(_filePathSensors, _filePathRBsTurn);
}
break;
case "stop":
Clipboard.Clear();
System.IO.File.Move(_filePathSensors, _filePathRBsTurn);
Application.Exit();
break;
}
}
}
});
th.IsBackground = true;
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
}
}
To cut the long story short, there are two problems: 1) how to continuously check for file name without using inefficient infinite loops? Can I define an event somehow?? 2) My program is not releasing the clipboard fast enough after using it. What could be the reason?