Hi,
I'm a junior developer using .NET framework.
I'm dealing with an issue because my GUI freezes when I run my application with big load of data.
I have a grid an a sort of output text box to log strings. The grid has a row for every message expected to arrive.
Then, the application receives messages and the grid updates a cell in the row that corresponds to the message. Also, I write a string in the text box with info about the message.
For example, the textbox will have messages such as:
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
....
And the grid would be something like:
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
....
The problem is that when I receive several messages in a very short of time, the GUI freezes because it is all the time updating the grid and the text box. It freezes until all the messages have arrived and the grid and text output are updated.
Do you know if there is some way to do this in some way that the GUI doesn't freeze? using more than one thread to update the GUI?
I think this is not a BackgroundWorker because the GUI is the one that should do the work but maybe I'm wrong.
EDITED1:
In fact I have a two threads:
1) Main Thread. It's the GUI and it has a BlockingQueue.
private BlockingQueue _guiQueue = new BlockingQueue(1000);
2) Thread1 It receives the messages, does some work after the message is received, and then it queues the result and send it to the GUI:
_guiQueue.Enqueue(new UpdateResult(_message.Name, _message.Result));
I'm using BlockingQueues, this ones: http://www.codeproject.com/KB/recipes/boundedblockingqueue.aspx
Once Main Thread receives the message, it basically updates the Grid and the output text box, nothing else.
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;
}
}
}
}
The problem is when I receive more than one message a second or so.
By instance, I have a STOP button to stop everything, but there is no way to click on it because the GUI is freeze
Thanks!
PS: I'm using DevExpress, the grid is XtraGrid and the output text box is a memoEdit control