2014-02-19 33 views
0

是否有窗口形式的控制,讓我暫停正在進行的串行數據接收過程中,因爲我需要檢查並確認正在圖形上繪製的數據。一旦檢查,我需要恢復過程。它會像一個start .. pause ..resume ..暫停 ..進程。啓動暫停和恢復串行數據繪製在圖

將是巨大的,如果任何人都可以建議我在上面的理想方法。 後臺工作人員是實現此功能的唯一方法嗎?

+0

什麼樣的設備/你從中獲取數據的系統?我以前做的的SerialPort數據繪製的很多,但我總是控制我的設備: 我發送命令的SerialPort然後接收數據,則情節,如果我需要暫停我停止發送命令 – chouaib

+0

的數據是從該發送傳感器來數據只有當一個特定的命令被髮送給它時。當我單擊Windows窗體上的「開始」按鈕時,數據正被繪製在圖上。不過,我的要求是我放了一個暫停按鈕,暫時停止程序,看看圖中有什麼烹飪,然後點擊開始按鈕繼續。我將如何實現這一目標? – Porcupine

回答

0

根據您使用什麼協議,你可能能夠指示發送者不能在你已經停下來送東西,但我認爲最直接的方式將緩衝在隊列或一個簡單的數組傳入數據或什麼的,然後只是在用戶處於暫停狀態時不用新數據更新屏幕。

+0

由於數據是散裝未來,不會是一個問題,如果我不得不將它們存儲在一個數組/隊列中,直到端口再次關閉後,我恢復它接收更多的數據? – Porcupine

+0

我不明白爲什麼。數據是否會丟失? –

0

我的方式做這樣的任務:

其實你需要使用線程,原因只有一個,這就是組織一次 EX:

WRONG:

while(true){ 
GetDataFromSerialPort(); // you don't know how long it takes 10ms, 56ms, 456ms ...? 
DrawData(); // this plots data at randomly spaced intervals 
} 

RIGHT

while(true){ 
Thread th1 = new Thread(new ThreadStart(GetDataFromSerialPort)); // thread to acquire 
th1.IsBackground = true;           // new data 
th1.Start(); 

wait(100); // main thread waits few milliseconds 

Thread th2 = new Thread(new ThreadStart(DrawData)); // draw on zedGraph on other thread 
th2.IsBackground = true; 
th2.Start(); 
} 

現在讓我們做你的主quastion(暫停/恢復...)

您需要定義決定了你的數據採集/繪圖循環中的布爾標誌:

bool isRunning = false; // initially it's stopped 

public void startDrawing() 
{ 
isRunning = true; 

while(isRunning) 
{ 
//thread to get data 
//wait 
//thread to draw it 
//refer to the above "right" example 
} 

} 

// Now let's set buttons work 
private void button1_Click(object sender, EventArgs e) 
{ 
if(button1.text == "START" || button1.text == "RESUME") 
{ 
button1.text = "PAUSE"; 
startDrawing(); 
} 
else 
{ 
button.text = "RESUME"; 
isRunning = false; 
} 
}