如何在while循环中杀死线程C#

tpb*_*afk 0 c# multithreading while-loop

我有一个while循环检查true/false.And我也有阻止.如果块计数到300到0.我想杀死线程时它是0和另外两个地方.我看了论坛但我的方法看起来像有点不同.btw我试过中止但没有工作.如果我的帖子中有错误请edit.thx!

            void Test()
            {

                Thread thread = new Thread(() =>
                            {
                                try
                                {
                                    int countdown = 300;
                                    while (true)
                                    {
                                        Thread.Sleep(1000);
                                        paymentService.CheckPayment(pdId);
                                        if (result.Complete == false)
                                        {
                                            countdown--;
                                            if (countdown == 1)
                                            {
                                                //kill thread
                                            }
                                        }
                                        if (result.Complete == true)
                                        {
                                            //kill thread
                                            NavigationService.Navigate(new Uri("Pages/success.xaml", UriKind.Relative));
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                    //kill thread
                                    Application.Current.Shutdown();
                                }
                            });
                thread.Start();

            }
Run Code Online (Sandbox Code Playgroud)

Ven*_*kov 5

return;
Run Code Online (Sandbox Code Playgroud)

会做 - 你只需要退出Thread运行的方法;