'How to Execute a method once when the event triggers very first time?

I have this following Event:

    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            sPort = new SerialPort();
            sPort.PortName = comboBox1.Text;
            sPort.BaudRate = Convert.ToInt32(comboBox5.Text);
            sPort.DataBits = Convert.ToInt32(comboBox3.Text);
            sPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox2.Text);
            sPort.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox4.Text);
            sPort.Handshake = Handshake.None;
            sPort.RtsEnable = true;
            sPort.DtrEnable = true;
            sPort.DataReceived += new SerialDataReceivedEventHandler(sPort_datareceived);

            sPort.Open();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, MessageBoxButtons.OK.ToString());
        }
    }

     private void sPort_datareceived(object sender, SerialDataReceivedEventArgs e)
    {                
        SerialPort sp = (SerialPort)sender;
        datain = sp.ReadExisting();                
        this.Invoke(new EventHandler(idextraction));
    }

    public string namingid;

    private void idextraction(object sender, EventArgs e)
    {
        Match matchid = Regex.Match(datain, @"\b\d{12}\b");
        namingid = matchid.Value;
        namingid = namingid.Substring(namingid.Length - 7);
        this.Invoke(new EventHandler(writesyncdata));
    }

 private void writesyncdata(object sender, EventArgs e)
    {
        try
        {
            TextWriter tw = new StreamWriter(@"C:\\intdata\\" + namingid + ".txt");
            tw.Write(datain);
            tw.Close();
        }
        catch (Exception err)
        {
            MessageBox.Show(err.Message, MessageBoxButtons.OK.ToString());
        }
    }

Suppose this Event triggers X No. of times and then stops and then Triggers Again And the Cycle Goes On. The Time Interval is between 1-2 sec when the event triggers X no. of times. I want to Invoke My method once the very first time the event triggers and stop afterwards but My method should Execute once every time the Cycle Starts.

When idextraction() invokes it doesn't work because the data in buffer is less to process(it takes 1-2 sec to fill full data but my method invokes before that and that's the problem)

I Know the how to execute a method once but as the event triggers many times in short period of time so my method also and i don't want that. Does anybody know how to do it ?



Solution 1:[1]

Every time the event fires, you have no idea how much information will be available. It is your responsibility to buffer up all the data as it is received until you have something useful to do with it. There are a variety of ways to do this, like you could have a queue of bytes. When data is received, you add the data to the end of the queue and check if it is enough to process it. If it is, the call your processing routine. If it is not, then wait for more data to be received. You will need a separate queue for each serial port you open.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John V