title: How to Execute a method once when the event triggers very first time?
body: <p>I have this following Event:</p>
  <pre><code>    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, @&quot;\b\d{12}\b&quot;);
          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(@&quot;C:\\intdata\\&quot; + namingid + &quot;.txt&quot;);
              tw.Write(datain);
              tw.Close();
          }
          catch (Exception err)
          {
              MessageBox.Show(err.Message, MessageBoxButtons.OK.ToString());
          }
      }
  </code></pre>
  <p>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.</p>
  <p>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)</p>
  <p>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 ?</p>