.NET GUI freezes when large data load

I am a junior developer using the .NET framework.

I am dealing with a problem because my GUI freezes when I run my application with a lot of data loading.

I have a grid as an output text box for writing strings. The grid has a row for each expected message.

The application then receives the messages, and the grid updates the cell in the row that corresponds to the message. In addition, I write a line in the text box with information about the message.

For example, a text field 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 will look something like this:

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?

, BackgroundWorker, GUI - , , , , .

EDITED1:

:

1) . GUI, BlockingQueue.

private BlockingQueue _guiQueue = new BlockingQueue(1000);

2) Thread1 , , :

_guiQueue.Enqueue(new UpdateResult(_message.Name, _message.Result));

BlockingQueues, : http://www.codeproject.com/KB/recipes/boundedblockingqueue.aspx

, Main Thread , Grid output, .

    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

+3
4

- . , , , . 250 . , . "BeginUpdate\EndUpdate", , .

ConcurrentQueue? BlockingQueue (.. ) , .

+2
  • , . (BackgroundWorker)
  • . , X . , , - , .
+1

?

BackgroundWorker .

.

0

, , XtraGrid. Demos\Components\XtraGrid\CS\GridTutorials\GridVirtualData. , , . , 100000 . , . , , , . , .

IList.this [int fIndex]

, :).

GUI, Application.DoEvents().

, MemoEdit . TextBox , :( , , . , MemoEdit XtraGrid. .

, , :

List<LogMessage> list = new List<LogMessage>();
for(int i = 0;i < 100000;i++) 
    list.Add(new LogMessage());
vList = new VirtualList(list);
grid.DataSource = vList;

...

    public class LogMessage {
        public LogMessage() {
            TimeStamp = DateTime.Now;
            Description = "Message at " + TimeStamp.Ticks.ToString();
        }
        public DateTime TimeStamp;
        public string Description;
    }

    public abstract class LogMessagePropertyDescriptor : PropertyDescriptor {
        bool fIsReadOnly;

        public LogMessagePropertyDescriptor(string fPropertyName, bool fIsReadOnly)
            : base(fPropertyName, null) {
            this.fIsReadOnly = fIsReadOnly;
        }

        public override bool CanResetValue(object component) { return false; }
        public override bool IsReadOnly {get { return fIsReadOnly; } }
        public override Type ComponentType { get { return typeof(LogMessage); } }
        public override void ResetValue(object component) {}
        public override bool ShouldSerializeValue(object component) { return true; }
    }
    public class LogMessageTimeStampPropertyDescriptor: LogMessagePropertyDescriptor {
        public LogMessageTimeStampPropertyDescriptor(bool fIsReadOnly)
            : base("TimeStamp", fIsReadOnly) {
        }
        public override Type PropertyType {get {return typeof(DateTime); } }
        public override object GetValue(object component) {
            LogMessage message = (LogMessage)component;
            return message.TimeStamp;
        }
        public override void SetValue(object component, object val) {
            LogMessage message = (LogMessage)component;
            message.TimeStamp = (DateTime)val;
        }
    }
    public class LogMessageDescriptionPropertyDescriptor: LogMessagePropertyDescriptor {
        public LogMessageDescriptionPropertyDescriptor(bool fIsReadOnly)
            : base("Description", fIsReadOnly) {
        }

        public override Type PropertyType { get { return typeof(string); } }

        public override object GetValue(object component) {
            LogMessage message = (LogMessage)component;
            return message.Description;
        }
        public override void SetValue(object component, object val) {
            LogMessage message = (LogMessage)component;
            message.Description = (string)val;
        }
    }
    public class VirtualList : IList, ITypedList {
        PropertyDescriptorCollection fColumnCollection;
        List<LogMessage> messages;

        public VirtualList(List<LogMessage> messages) {
            this.messages = messages;
            CreateColumnCollection();
        }
        public int RecordCount { get {return messages.Count; } }
        public int ColumnCount { get { return fColumnCollection.Count; } }
        protected virtual void CreateColumnCollection() {
            List<PropertyDescriptor> pds = new List<PropertyDescriptor>();
            pds.Add(new LogMessageTimeStampPropertyDescriptor(true));
            pds.Add(new LogMessageDescriptionPropertyDescriptor(false));
            fColumnCollection = new PropertyDescriptorCollection(pds.ToArray());
        }

        #region ITypedList Interface
        object IList.this[int fIndex] { get { return messages[fIndex]; } set { } }
        PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] descs) { return fColumnCollection; }
        string ITypedList.GetListName(PropertyDescriptor[] descs) { return ""; }
        #endregion
        #region IList Interface
        public virtual int Count { get { return RecordCount; } }
        public virtual bool IsSynchronized { get { return true; } }
        public virtual object SyncRoot { get { return true; } }
        public virtual bool IsReadOnly{ get { return false; } }
        public virtual bool IsFixedSize{ get { return true; } }
        public virtual IEnumerator GetEnumerator() { return null; }
        public virtual void CopyTo(System.Array array, int fIndex) {}
        public virtual int Add(object val) { throw new NotImplementedException(); }
        public virtual void Clear() { throw new NotImplementedException(); }
        public virtual bool Contains(object val) { throw new NotImplementedException(); }
        public virtual int IndexOf(object val) { throw new NotImplementedException(); }
        public virtual void Insert(int fIndex, object val) { throw new NotImplementedException(); }
        public virtual void Remove(object val) { throw new NotImplementedException(); }
        public virtual void RemoveAt(int fIndex) { throw new NotImplementedException(); }
        #endregion
    }
0

Source: https://habr.com/ru/post/1753490/


All Articles