In C#, wait on the mainthread while continuing to process UI updates? (.NET 2.0 CF)

前端 未结 10 2201
梦谈多话
梦谈多话 2020-12-18 11:49

I want to otherwise block code execution on the main thread while still allowing UI changes to be displayed.

I tried to come up with a simplified example version of

10条回答
  •  太阳男子
    2020-12-18 12:35

    Just a code snippet: don't have much time sorry :)

        private void StartMyDoSomethingThread() {
            Thread d = new Thread(new ThreadStart(DoSomething));
            d.Start();
        }
    
        private void DoSomething() {
            Thread.Sleep(1000);
            ReportBack("I'm still working");
            Thread.Sleep(1000);
            ReportBack("I'm done");
        }
    
        private void ReportBack(string p) {
            if (this.InvokeRequired) {
                this.Invoke(new Action(ReportBack), new object[] { p });
                return;
            }
            this.Text = p;
        }
    

提交回复
热议问题