我是一個使用.NET框架的初級開發人員。當大數據量的數據時,.NET GUI凍結
我正在處理一個問題,因爲當我用大量數據運行我的應用程序時,我的GUI凍結。
我有一個網格輸出文本框來記錄字符串。網格對於預期到達的每條消息都有一行。
然後,應用程序接收消息,網格更新與該消息相對應的行中的單元格。另外,我在文本框中寫入了一個關於該消息的信息的字符串。
例如,文本框將有消息,如:
10:23:45 Message 1 arrived and the result is OK
10:23:45 Message 2 arrived and the result is OK
10:23:45 Message 3 arrived and the result is FAIL
10:23:45 Message 4 arrived and the result is OK
10:23:46 Message 5 arrived and the result is OK
....
和電網會是這樣的:
MESSAGE_ID | RESULT <------- HEADER
Message_1 | OK
Message_2 | FAIL
Message_3 | OK
Message_4 | OK
Message_5 | OK
Message_6 | Waiting
Message_7 | Waiting
....
的問題是,當我在很短的接收多條消息時間,GUI凍結,因爲它始終更新網格和文本框。它會凍結,直到所有消息到達並且更新了網格和文本輸出。
您是否知道是否有某種方式可以通過某種方式來實現GUI不凍結?使用多個線程來更新GUI?
我認爲這不是一個BackgroundWorker,因爲GUI是應該做的工作,但也許我錯了。
EDITED1:
其實我有兩個線程:
1)主線程。這是GUI,它有一個BlockingQueue。
private BlockingQueue _guiQueue = new BlockingQueue(1000);
2)線程1 它接收消息,在接收到消息後,做了一些工作,然後它會將結果,並將其發送到GUI:
_guiQueue.Enqueue(new UpdateResult(_message.Name, _message.Result));
我使用BlockingQueues ,這個: http://www.codeproject.com/KB/recipes/boundedblockingqueue.aspx
一旦主線程收到消息,它基本上更新網格和輸出文本框,沒有別的。
public MainThread(IMainForm mainView)
{
// presenter
_mainView = mainView;
....
// Blocking queues
_guiQueue = new BlockingQueue(1000);
....
// Timer
logger.Debug("Initializing Timer");
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(10);
// Call handleMessages method everytime the timer wakes up
_timer.Tick += HandleMessages;
_timer.Start();
...
// Order Passing Thread
logger.Debug("Launching OPThread");
_orderPassingThread = new OPThread(_OPQueue, _commonObjects);
_orderPassingThreadProcess = new Thread(new ThreadStart(_orderPassingThread.OPThreadProcess));
_orderPassingThreadProcess.Start();
...
}
private void HandleMessages(Object sender, EventArgs args)
{
Presenter.Messages.Message message;
while ((message = _guiQueue.Dequeue(10)) != null)
{
switch (message.MessageType)
{
case messageTypes.updateResult:
UpdateResult updateStepMsg = (UpdateResult) message;
_mainView.updateStepResult(updateStepMsg.Name, updateStepMsg.Result); // updates Grid and text box
break;
....
default:
break;
}
}
}
}
問題是,當我接收多於一個消息的第二左右。
通過實例,我有一個STOP按鈕停止一切,但沒有辦法來點擊它,因爲GUI是凍結
謝謝!
PS:我使用的DevExpress,網格是XtraGrid中,輸出文本框是memoEdit控制
你能告訴我們代碼產生bac kground線程?包含while循環的方法的全部內容? – 2010-07-07 09:42:39
添加了該代碼。隊列是這些: http:// www。codeproject.com/KB/recipes/boundedblockingqueue.aspx?bcsi_scan_1B08F4CB0D5234CC=BK/P3lTlELaTMlVRd7WPTA4AAADxi/AF&bcsi_scan_filename=boundedblockingqueue.aspx – pedroruiz 2010-07-07 09:57:07
如果這是Java的,我可能會在GUI線程策略上進行同步:_mainView.updateStepResult(updateStepMsg.Name,updateStepMsg.Result ) 也許,你可以得到線索: http://stackoverflow.com/questions/530211/creating-a-blocking-queuet-in-net – eee 2010-07-07 10:16:32