如何在Silverlight中编写程序,每10秒做一次?

时间:2011-10-20 23:28:02

标签: c# silverlight

如何在Silverlight中编写可以每10秒执行一次的程序,例如,处理Twitter搜索结果?我最接近的是:

        // Get and process new twitter search results evey 10 seconds
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += (sender, args) =>
        {
            while (true)
            {
                //Get results of Twitter Search and process them in the above code
                client.DownloadStringAsync(new Uri("http://search.twitter.com/search.json?q=" + keywords + "&show-user=true", UriKind.Absolute));

                // wait 10 seconds beofre getting new twits
                System.Threading.Thread.Sleep(10000);
            }
        };
        worker.RunWorkerAsync();

但它不起作用。请帮助。

2 个答案:

答案 0 :(得分:2)

您想使用System.Threading.Timer。所有官方文档底部都有示例代码:http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

答案 1 :(得分:2)

如果您希望在UI线程上执行代码,则需要DispatcherTimer。请参阅此post