I'm using a WPF application for serial communication where serial port is created by taking specifications from the user in the mainwindow(GUI) and the port is sent to a background worker as an argument. My question is that I have a datareceived event for the port in my mainwindow thread which I use to read sample data from serial port and do the continuous reading in the BG thread. When using the port that I sent as argument to the BG worker should I define a new datareceived event or will the same one work?
private void SerialThread_DoWork(object Sender, DoWorkEventArgs e )
{
BGargs args = e.Argument as BGargs;
SerialPort BGport = args.PORT;
string MODE = args.MODE;
string filePath = args.filepath;
BGport.DataReceived +=new SerialDataReceivedEventHandler(BGport_DataReceived);
Dispatcher.BeginInvoke((Action)delegate() { run_button.IsEnabled = false; });
switch (MODE)
{
case "EXT_trigger":
while (SerialThread.CancellationPending)
{
FileStream file = new FileStream(filePath, FileMode.Append, FileAccess.Write);
using (StreamWriter Writer = new StreamWriter(file))
{
//code to continuously trigger and read data and then write to file
}
}
break;
}
}