Task.Factory.StartNew()使用TPL操作计时和调试多任务执行

时间:2012-11-20 20:24:01

标签: debugging c#-4.0 .net-4.0 task-parallel-library

我将了解如何并行执行一系列函数并能够调试每个函数。

我正在尝试创建一个控制台应用程序(在编译之后), 在将通过其任务调度程序执行应用程序的服务器上运行。

    static void Main(string[] args)
    {
        Task.Factory.StartNew(
            // first b. point here (will not "step into")
            () =>
                doFirstOne().ContinueWith(task => doSecondOne())
                ); 
    }

    public static IWebDriver AllDayWbDrvr;
    static string extrctdStr = "";

    public static void doFirstOne()
    { 
           // <<<<<<<----- brakePoint here  nothing steps in
        SeparatedClass.IeEnginGenerator IeNgn = new SeparatedClass.IeEnginGenerator();
         AllDayWbDrvr = IeNgn.ExtractPageContent(firstUrl....);

        var a = AllDayWbDrvr.FindElements(By.Id("anyID"));
        IWebElement IwbElm = IWebDrvRdonlyColl.ElementAt(1).FindElements(By.TagName("td"))[0];
        extrctedStr = IwbElm.Text;
        // plan is to append extracted text to one shared file that will log all results
        string fNm = @"C:\Inetpub\wwwroot\dolarRate.asp";
        File.WriteAllText(fNm, extrctdStr);

        AllDWbDrvr.Close();
        IeNgn.service.Dispose();

    }


    public static void doSecondOne()
    {
        SeparatedClass.IeEnginGenerator IeNgn = new SeparatedClass.IeEnginGenerator();
         AllDayWbDrvr = IeNgn.ExtractPageContent("secondURL....");

        var a = AllDayWbDrvr.FindElements(By.ClassName("WpBody"));
        IWebElement IwbElm = IeNgn.IWebDrvRdonlyColl.ElementAt(5).FindElements(By.TagName("td"))[1];
        extrctedStr = IwbElm.Text;
        string fNm = @"C:\Inetpub\wwwroot\anyOtherFile.asp";
        File.WriteAllText(fNm, extrctdStr);

        AllDWbDrvr.Close();
        IeNgn.service.Dispose();

    }
  1. 是Thread / concatenate函数的方法吗?
  2. 我怎样才能打破doFirst()&amp; doScond()代码?

1 个答案:

答案 0 :(得分:1)

您需要等待任务完成,然后退出main。

结束您的应用程序

尝试类似: -

Task.Factory.StartNew(doFirstOne).ContinueWith(task => doSecondOne()).Wait();