3

我在 C# 中的 Windows 窗体应用程序有问题在这个应用程序里面有一个选项卡式浏览器。现在我想在一个循环中(我做一些操作来计算下一个 url)我可以在 AddTab 之间添加一个延迟。

IE

 foreach(Urls url in Links){

        // Do something with url
         if(url.Host == "google"){ //if host is google until start the 
                                   // next AddTab i would wait 5 SEC
             addTab(url);
           //Here i Tried to use Sleep Thread , While(TimeCur-TimePre) {} but i always 
           // get Freezing and the other Tabs dont continue the loading
             Wait 5 Seconds in somehow
         }
}

正如我所写的,我不想使用线程睡眠,因为它会冻结我的表单应用程序。我已经使用了 TimeCur-TimeSaved+(x Second) 的时间技巧,但这也会冻结表单。

我看到很多人们说要使用计时器的话题,所以我尝试使用它们(没有任何结果,也许我错了)

我可以在这个循环中添加什么来延迟 AddTab 之间的打开而不冻结任何东西?

4

2 回答 2

6

使用 Windows 窗体计时器(来自工具箱)。在属性中,设置间隔设置。然后在表单设计器上双击它。这将创建一个事件处理程序,当间隔时间过去时,它将被触发(在一个单独的线程中)。您可以在此处完成您的工作,并且该表单将保持活动状态。

于 2011-11-23T00:06:43.330 回答
4
    var thread = new System.Threading.Thread(p =>
    {
        lock (YourTabControl)
        {
            Action action = () =>
            {
                addTab(url);
            };
            this.Invoke(action);
            if(url.Host == "google")
                System.Threading.Thread.Sleep(5000);
        }
    });
    thread.Start();
于 2011-11-23T00:47:49.137 回答