Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
We are creating 16 user control on a windows form. The user control has many gif animation get displayed. I am trying to call the user control to update these animation using the below code. guiLogUpdateButton_Click is the button in the windows form. There is a task that will run the testing function after button is pressed. SlotModel is the user control object that contains all information. UpdateView is the function called the view of the user control. And as the animation get updated to the windows form, the windows form get stucked/freezed on the software.

private void guiLogUpdateButton_Click(object sender, EventArgs e)
        {
                            Task b = Task.Run(() =>
                                {
                                    testing();
            });
        }

        private void testing()
        {
                SlotModel[] slotModel = new SlotModel[16];
                for (int slotNumber = 0; slotNumber < 16; slotNumber++)
                {
                    int i = slotNumber;
                    slotModel[i] = new SlotModel(i);
                    Task u = Task.Run(() =>
                    {
                        slotModel[i].status.Clear();
                        slotModel[i].status.Add(enumSlotStatus.Running);
                        UpdateView(slotModel[i]);
                        Thread.Sleep(500);
                        slotModel[i].status.Clear();
                        slotModel[i].status.Add(enumSlotStatus.Pass);
                        UpdateView(slotModel[i]);
                        Thread.Sleep(500);
                        slotModel[i].status.Clear();
                        slotModel[i].status.Add(enumSlotStatus.Fail);
                        UpdateView(slotModel[i]);
                        Thread.Sleep(500);
                    });
                }

        }



//This is the code for the user control below
public void UpdateView(Gen5.Model.SlotModel slotModel)
        {

if (InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate { UpdateView(slotModel); });
            }
            else
            {
//this is where the user control get updated
}<pre><pre>


What I have tried:

We try other ways to do it but all of them create the lag on the software.
Posted
Updated 8-Jul-17 9:14am

1 solution

The main cause for freezing your application is Thread.Sleep(500) function, using Task.Delay(500) will fix your issue.

Alongwith that I also prefer you to set the value of DoubleBuffered to true in Form properties.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900