2

我是线程新手,我正在尝试找出一种使用任务来完成此任务的方法:

private void botCore()
{
    if (CheckBox2.Checked == true)
    {
        Task Core =  Task.Factory.StartNew(() =>
        {
            MessageBox.Show("Proxy -> Next");
            Webbrowser1.Navigate("www.lalaland.com")

        });
        Core.ExecuteFor(2000)); // Executes the task above for only 2000 milliseconds then proceeds

        if  (Core.IsCompleted)              // Checks if the 2000 milliseconds are up then executes another statement.
        {
            MessageBox.Show("Proxy -> Next after next");
            Webbrowser1.Navigate("www.balalala.com")
            ExecuteFor(3000) //Execute this for 3000 milliseconds.
        }
        Core.Stop();
    }
}

基本上,我正在尝试将任务运行 x 时间,一旦任务完成,它将运行另一个任务 x 时间并循环执行。如果这可以以更好的方式完成,例如 UI 线程,请为我提供工作来源。我对 C# 很陌生。感谢任何支持我的人。

4

3 回答 3

1

I see two common mistaken assumptions in your code:

  • You are calling UI methods from a thread other than the UI thread. In Windows, there is (usually) only one UI thread, and all UI updates must be done on that thread.
  • You are attempting to control a thread "externally", telling it to execute for so long and to stop on command. In the general case, this isn't possible; a thread is completely independent and will not stop until it wants to.

If you just want to introduce some delays, threads aren't needed. You can do it with async:

private async Task botCore()
{
  if (CheckBox2.Checked == true)
  {
    MessageBox.Show("Proxy -> Next");
    Webbrowser1.Navigate("www.lalaland.com")

    await Task.Delay(2000);

    MessageBox.Show("Proxy -> Next after next");
    Webbrowser1.Navigate("www.balalala.com");

    await Task.Delay(3000);
  }
}
于 2013-06-12T12:35:38.067 回答
0

一个简单的解决方案是调用Core.Wait(_time_to_wait_)并检查返回值。如果为真,则任务完成。否则为假。如果任务超时并且您想“杀死”它,请参阅:http: //msdn.microsoft.com/en-us/library/dd537607.aspx

请注意,如果您Task.Wait在 之后立即调用Task.Factory.StartNew,则计时器甚至可能在任务执行之前就超时,因为任务首先被放入任务调度程序的队列中,并且任务初始化和执行之间可能存在明显的延迟。为了避免这种情况(如果你想在重负载下进行精确的时间控制,这是会发生延迟的主要场景),在任务中添加一个额外的信号,然后先等待它:

var started = new ManualResetEventSlim(false);
var t = Task.Factory.StartNew(() =>
    {
        started.Set();
        //...
    });
started.Wait();
t.Wait(2000);
于 2013-06-12T07:10:39.153 回答
0

如果你问我,我会说开始使用方法。这是一个开始。除此之外,看看这个例子..它并没有解释一切,但没关系。 http://msdn.microsoft.com/en-us/library/dd537610.aspx

注意:您可以等待任务。但它会在完成后完成。它不会在 2000 毫秒后自动关闭。您需要手动执行此操作

除此之外,试试这个例子,.. 没有测试它。

private void botCore()
{
    if (CheckBox2.Checked == true)
    {
       Task coreTask = Task.Factory.StartNew(() => printMessage("1", "www.google.nl"));
       await Task.Delay(2000);
       coreTask.Stop() // AS you asked that it will stop.

        if  (coreTask.IsCompleted) // Checks if the 2000 milliseconds are up then executes another statement.
        {
           Task coreTask2 = Task.Factory.StartNew(() => printMessage("2", "www.google.nl?p=2"));
           await Task.Delay(3000);
           coreTask2.Stop() // AS you asked that it will stop.
        }
        Core.Stop();
    }
}


public void printMessage(string text, string url){
    MessageBox.Show("Proxy -> " + text);
    Webbrowser1.Navigate(url)
}
于 2013-06-12T07:20:22.657 回答