运行另一个操作时显示动画加载文本

时间:2013-07-09 12:36:50

标签: c# loops directory loading

如果我打电话给Directory.GetFiles()需要大约10秒才能完成,我怎么能(同时)显示动画加载文字?我有一个类来处理动画但不确定是否可以使用单个函数调用(而不是循环)。

1 个答案:

答案 0 :(得分:1)

您可以使用Tasks

class Program
    {
        static void Main()
        {

            var tokenSource2 = new CancellationTokenSource();
            var ct = tokenSource2.Token;

            var task = Task.Factory.StartNew(() =>
            {
                //Replase  with yor animation code

                int i = 0;
                while (true)
                {

                    Console.WriteLine(i++/10.0);
                    Task.Delay(100).Wait();

                    if (ct.IsCancellationRequested)
                    {
                        return;
                    }

                }
                // end of replace
            }, tokenSource2.Token);

            Task.Delay(10000).Wait(); //replace with Directory.GetFiles() 

            tokenSource2.Cancel(); // replace with animation stop code

        }
    }