0
我正在使用WPF應用程序進行串行通信,其中通過在主窗口(GUI)中從用戶獲取規範來創建串行端口,並將該端口作爲參數發送給後臺工作人員。我的問題是我的主窗口線程中有一個端口的數據收集事件,我使用它從串口讀取樣本數據並在BG線程中進行連續讀取。當使用我發送給BG工作人員的參數的端口時,我應該定義一個新的datareceived事件,還是將同一個事件工作?從標題data_received事件在哪個線程上運行?
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;
}
}
@quetz ...感謝您的回覆,是的,我能夠使用相同的事件處理程序來執行我的操作。 – ranjith1512 2012-08-09 13:17:46