Using BackgroundWorker to complete two methods one after the other WPF/C#

后端 未结 2 905
故里飘歌
故里飘歌 2021-01-22 09:16

In my program I have two methods that takes a while to complete, about few minutes each. While these methods are being executed, I display a Progress Bar in a separate window wh

2条回答
  •  花落未央
    2021-01-22 09:57

    An option i prefer is to have those 2 methods in a different thread and use a while loop to check if thread is still running and if it is use Task.Delay() EG.

    private async void BlahBahBlahAsync()
        {
            Thread testThread = new Thread(delegate () { });
            newThread = new Thread(delegate ()
            {
                Timeconsuming();
            });
            newThread.Start();
            while (testThread.IsAlive)
            {
                await Task.Delay(50);
            }
        }
    
        private void Timeconsuming()
        {
            // stuff that takes a while
        }
    

提交回复
热议问题