Why is Thread.Sleep so harmful

前端 未结 8 2134
天涯浪人
天涯浪人 2020-11-22 06:30

I often see it mentioned that Thread.Sleep(); should not be used, but I can\'t understand why this is so. If Thread.Sleep(); can cause trouble, are

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 06:49

    I agree with many here, but I also think it depends.

    Recently I did this code:

    private void animate(FlowLayoutPanel element, int start, int end)
    {
        bool asc = end > start;
        element.Show();
        while (start != end) {
            start += asc ? 1 : -1;
            element.Height = start;
            Thread.Sleep(1);
        }
        if (!asc)
        {
            element.Hide();
        }
        element.Focus();
    }
    

    It was a simple animate-function, and I used Thread.Sleep on it.

    My conclusion, if it does the job, use it.

提交回复
热议问题