Trouble with block access to resource
I have a SerialPort with a GSM modem, i must send a SMS with AT command and wait a "OK" from the modem to know that the send is right.
SerialPort has a event that indicate data arrival, and i write a function to send AT commands to modem. I want send a AT command and lock the modem until receive a OK.
First i try:
DataArrival_Event()
{
lock(MySerialPort)
{
// Read Buffer & Proccess data
}
}
SendAT_Function()
{
lock(MySerialPort)
{
// Send the AT command.
}
}
But can do several calls followed to SendAT_Function and then DataArrival_Event receive a lot of "OK" in the same answer, i can't discern to as send corresponds each "OK".
After I try:
private Mutex MyMutex = new Mutex(); { // Read Buffer & Proccess data } { // Send the AT command. }
DataArrival_Event()
MyMutex.ReleaseMutex();
SendAT_Function()
MyMutex.WaitOne();
Same problem. I monitorize the ThreadID and view that is always the same, this is the problem, of course.
After again, i try:
private Mutex MyMutex = new Mutex(); DataArrival_Event() { // Read Buffer & Proccess data MyMutex.ReleaseMutex(); } { MyMutex.WaitOne(); // Send the AT command. }
SendAT_LikeThread_Function() // <-- I call this function instead SendAT_Function
{
Thread NewThread = new Thread(new ThreadStart(SendAT_Function));
NewThread.Start();
}
SendAT_Function()
Now I get a AbandonedMutexException, because the NewThread leave the SendAT_Function without release the mutex.
I don't know more ways to do it :S
Same idea?
Regards.

