Results 1 to 3 of 3

Thread: How can I prevent my event handler from being executed on the user interface thread?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2023
    Posts
    31

    How can I prevent my event handler from being executed on the user interface thread?

    Hello,

    I have a Datagridview that receives external data.

    My datagridview refreshes every 10 seconds and during refreshment the table sparkles (i.e. the cells shrink and return to their original sizes).

    After some research, I've read that the problem is that my event handler is running on the UI thread and I freeze that thread every time I call "Wait". I need to get all the data I need first and then initiating a thread or task to do that work.

    Do you have any idea how to do this?

    Code:
        private void dtgvPositions_DataBindingComplete(object sender, 
    DataGridViewBindingCompleteEventArgs e)
    {
        List<string> lstUID = this.ReplaceDownTheLine(this.txtUIDs.Text.Trim());
        Dictionary<string, bool> lstUIDColor = new Dictionary<string, bool>();
        for (int index = 0; index < lstUID.Count; ++index)
            lstUIDColor.Add(lstUID[index], index % 2 == 0);
        this.dtgvPositions.Invoke((Action)(() =>
        {
            foreach (string str in lstUID)
            {
                string uid = str;
                var nickNameTask = Task.Run<string>(async () => await this.GetTraderName(uid));
                nickNameTask.Wait();
                var nickName = nickNameTask.Result;
    
                foreach (DataGridViewRow row in (IEnumerable)this.dtgvPositions.Rows)
                {
                    string key = row.Cells[1].Value.ToString();
                    Decimal num = 0M;
                    try
                    {
                        num = Decimal.Parse(row.Cells[7].Value.ToString());
                    }
                    catch
                    {
                    }
                    bool flag;
                    lstUIDColor.TryGetValue(key, out flag);
                    row.DefaultCellStyle.ForeColor = num >= 0M ? Color.Green : Color.Red;
                    row.DefaultCellStyle.BackColor = flag ? FMain.COLOR_A : FMain.COLOR_B;
                }
            }
        }));
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,998

    Re: How can I prevent my event handler from being executed on the user interface thre

    You can't prevent your event handler being executed on the UI thread because that's the thread that your UI elements, i.e. your controls, raise their events on. On that front, what you can do is use a new thread or task started from your event handler to do background work without freezing the UI while it happens. That said, it doesn't sound like that's your issue. It sounds like your issue is making repeated changes to the UI with updates in between. That's what the "sparkling" is that you're talking about - it's the UI being repainted repeatedly. The solution to that is to make sure that the Ui doesn't get repainted until you've finished making all your changes. One has to wonder why you're waiting for the data to be bound to the grid and then making changes to that data via the grid. Why wouldn't you make the changes to the data in the data source first, before binding it to the grid?

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2023
    Posts
    31

    Re: How can I prevent my event handler from being executed on the user interface thre

    Thank you for your reply,

    I understand what you're saying but I don't know where to start...

    It would be nice if you could publish a piece of code that could solve my problem.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width