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 that but my method invokes before that :(
Exactly How do i wait for my method to be executed after all the data has been received in the buffer ? and How do check if it is enough to process using the same event ?
What I have tried:
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 ? Please Share your thoughts.