WPF线程延迟 [英] WPF Thread Delay

查看:1582
本文介绍了WPF线程延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我称之为一个新的任务与方法

  //获取调度员UI线程
VAR uiDispatcher = Dispatcher.CurrentDispatcher;
Task.Factory.StartNew(()=> BackgroundThreadProc(uiDispatcher));
 

在方法 BackgroundThreadProc()我需要几秒钟的延迟。我尝试了与DispatcherTimer和task.delay功能,但没有奏效。其工作的唯一的事情是 System.Threading.Thread.Sleep(1)但是我觉得的Thread.Sleep()的功能是不是最好的解决方案。

这是我的功能:

 公共无效BackgroundThreadProc(分派uiDispatcher)
{
    对于(VAR I = 0; I< 100;我++)
    {
        VAR任务= Task.Delay(1000).ContinueWith(T =>
        {
            //创建对象
            VAR动物=新的动物{名称=测试+ I};
            uiDispatcher.Invoke(新动作(()=>登录(动物)));
        });
    }
}
 

当我发现它不工作,因为DispatcherTimer是在UI线程中运行。我如何能做到的是在其他线程不是UI线程?

该函数的延迟

更新:

现在我试图与计时器:

 公共无效BackgroundThreadProc(分派uiDispatcher)
    {
        对于(VAR I = 0; I< 100;我++)
        {
            VAR _delayTimer =新System.Timers.Timer的();
            _delayTimer.Interval = 1000;
            //_delayTimer.Enabled = TRUE;
            _delayTimer.Elapsed + =委托
            {
                VAR动物=新的动物{名称=测试+ I};
                uiDispatcher.Invoke(新动作(()=>登录(动物)));
                _delayTimer.Stop();
            };
            _delayTimer.Start();
        }
    }
 

解决方案

使用 Task.Delay 来asynchrnoously引入的延迟:

  VAR任务= Task.Delay(1000)
    .ContinueWith(吨=> BackgroundThreadProc());
 

I have a method which I call in a new task with

// get the dispatcher for the UI thread
var uiDispatcher = Dispatcher.CurrentDispatcher;
Task.Factory.StartNew(() => BackgroundThreadProc(uiDispatcher));

In the method BackgroundThreadProc() I need a delay of few seconds. I tried it with the DispatcherTimer and the task.delay function but it didn't work. The only thing which worked was the System.Threading.Thread.Sleep(1) but I think the Thread.Sleep() function isn't the best solution.

This is my function:

public void BackgroundThreadProc(Dispatcher uiDispatcher)
{
    for (var i = 0; i < 100; i++)
    {
        var task = Task.Delay(1000).ContinueWith(t =>
        {
            // create object
            var animal = new Animal { Name = "test" + i };
            uiDispatcher.Invoke(new Action(() => log(animal)));
        });
    }
}

As I found out it didn't work because the DispatcherTimer is running in the UI thread. How I can accomplish the delay in the function which is in a other thread than the UI thread?

Update:

Now I tried it with the timer:

   public void BackgroundThreadProc(Dispatcher uiDispatcher)
    {
        for (var i = 0; i < 100; i++)
        {
            var _delayTimer = new System.Timers.Timer();
            _delayTimer.Interval = 1000;
            //_delayTimer.Enabled = true;
            _delayTimer.Elapsed += delegate
            {
                var animal = new Animal { Name = "test" + i };
                uiDispatcher.Invoke(new Action(() => log(animal)));
                _delayTimer.Stop();
            };
            _delayTimer.Start();
        }
    }

解决方案

Use Task.Delay to introduce a delay asynchrnoously:

var task = Task.Delay(1000)
    .ContinueWith(t => BackgroundThreadProc());

这篇关于WPF线程延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆