Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm try to use AutoResetEvent to pause the execution of the click event until the incoming event is fired but it is not working it only works after i do not need it anymore.

here is the sample code for it

C#
using System;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace ATModem
{
    public partial class frmMain : Form
    {
        public AutoResetEvent receiveNow = new AutoResetEvent(false);

        Queue<string> q = new Queue<string>();

        public frmMain()
        {
            InitializeComponent();
			objclsSMS.incoming += objclsSMS_incoming;
        }

        void objclsSMS_incoming(string response)
        {
            log_events("Incoming Event: " + response);
            q.Enqueue(response);
            receiveNow.Set();
        }
		
        private void test_Click(object sender, EventArgs e)
        {
            receiveNow.Reset();
            test.Enabled = false;
            test.Text = "Waiting...";
			
   	    if (receiveNow.WaitOne(5000)){
 		while (q.Count != 0)
		{
		    try{
			MessageBox.Show(q.Dequeue());
		        }
			catch (Exception ex){
				MessageBox.Show("Error: \n" + ex.Message);
			}
		}
	    }else{
	     MessageBox.Show("Request Timed out");
            }
        }
    }
}
Posted
Comments
Dave Kreskowiak 20-Feb-14 13:01pm    
I don't know what you're doing, but this looks very very wrong for any purpose at all.

Perhaps you'd better go into more detail as to what you expect this code to do, outside of "pause the execution of an event handler".
Baba_Dee 20-Feb-14 16:30pm    
I think you were right, it's running on the same thread and the approach is definitely wrong
Frank R. Haugen 20-Feb-14 13:10pm    
Please give us a commented sample, or a step-by-step description of what this code is supposed to achieve, (e.g. a pseudocode explaination)
Baba_Dee 20-Feb-14 16:35pm    
I'm working with a gsm modem that communicate via serial port. I'm using SIM900 at commands to access the STK(Sim Toolkit) menu that await a response from the network(Service Provider). That's why i created the event to listen for the incoming message(sms) and the method is expected to wait for the message before exiting the method.

Any help will be appreciated
Paulo Zemek 20-Feb-14 13:22pm    
The AutoResetEvent pauses the current thread.
The objclsSMS_incoming is probably running on the same thread and so, it is not going to be invoked until the test_Click returns.

In fact your problem is related to how the windows messages are processed. Any event (like click) is a windows message and it must finish its work before processing another message. So, you are waiting for the sms_incoming while the sms_incoming will wait until this method returns.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900