Help me with that CrossThread?

前端 未结 3 1143
小鲜肉
小鲜肉 2020-12-22 06:49

This code is executed by many way. When it\'s executed by the form button it works (the button start a thread and in the loop it call this method = it works). BUT it doesn\'

3条回答
  •  不知归路
    2020-12-22 07:04

    You need to return at the end of the if block - otherwise you'll resize it in the right thread, and then do it in the wrong thread as well.

    In other words (if you'd cut and paste the code instead of a picture, this would have been easier...)

    private void resizeThreadSafe(int width, int height)
    {
        if (this.form.InvokeRequired)
        {
            this.form.Invoke(new DelegateSize(resizeThreadSafe,
                new object[] { width, height });
            return;
        }
        this.form.Size = new Size(width, height);
        this.form.Location = new Point(0, SystemInformation.MonitorSize // whatever comes next
    }
    

    Alternatively just put the second half of the method in an "else" block.

提交回复
热议问题