Hi,
I would Queue the commands and use a lock on the file to processes them…
here is something to get you started, note I didn’t test this … but you get the point…
private static readonly Queue myCmdList = new Queue();
private static readonly AutoResetEvent myCmdListEvent = new AutoResetEvent(false);
static readonly Timer myCmdListTimer = new Timer(new TimerCallback(MyCmdTimer), null, -1, -1);
// <summary>
// Adds a request object to be processed
// </summary>
public static int AddRequest(object request)
{
lock (myCmdList)
{
myCmdList.Enqueue(request);
}
myCmdListEvent.Set();
return myCmdList.Count;
}
public static void TriggerLed(bool state)
{
lock (myCmdList)
{
if (AddRequest(new TriggerMyLed() { Led1 = state }) == 1) myCmdListTimer.Change(50, 100);
}
}
private static void MyCmdTimer(object arg)
{
int count;
TriggerMyLed myNextTrigger = null;
lock (myCmdList)
{
count = myCmdList.Count;
if (count <= 0)
{
myCmdListTimer.Change(-1, -1);
}
else
{
myNextTrigger = (TriggerMyLed)myCmdList.Dequeue();
}
if (myNextTrigger != null)
{
Debug.Print(myNextTrigger.Led1.ToString());
//do something with myNextTrigger.Led1 here with your code...
}
}
}
private class TriggerMyLed
{
private bool _ledState;
public bool Led1
{
get { return _ledState; }
set
{
_ledState = value;
}
}
internal TriggerMyLed()
{
//... you can something here do your thing here
}
}
and you would call it from your code like this
TriggerLed(true);
TriggerLed(false);