c#在任务1完成后启动任务2

c#在任务1完成后启动任务2,c#,asynchronous,cmd,task,C#,Asynchronous,Cmd,Task,因此,如果task1 cmd cookie删除在运行task2之后完成。我想删除2or3 cookie,但不在sam中,但仍在同一时间运行 Task task1 = new Task(() => { string cDelete; cDelete = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTrack

因此,如果task1 cmd cookie删除在运行task2之后完成。我想删除2or3 cookie,但不在sam中,但仍在同一时间运行

            Task task1 = new Task(() =>
            {
                string cDelete;
                cDelete = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 & echo off | clip ";
                System.Diagnostics.Process.Start("CMD.exe", cDelete);

            });
            Task task2 = new Task(() =>
            {
                string cDelete;
                cDelete = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 & echo off | clip ";
                System.Diagnostics.Process.Start("CMD.exe", cDelete);

            });
            task1.Start();
            if (task1.IsCompleted)
            {
                task2.Start();
            }
直接使用流程

public static void Main()
{
    try
    {
        using (Process myProcess = new Process())
        {
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.FileName = "cmd.exe";
            myProcess.StartInfo.Arguments  = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 & echo off | clip ";
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.Start();
            // This code assumes the process you are starting will terminate itself. 
            // Given that is is started without a window so you cannot terminate it 
            // on the desktop, it must terminate itself or you can do it programmatically
            // from this application using the Kill method.
            myProcess.WaitForExit();
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}
您需要调用task.wait()以确保在启动task2之前任务已完成

            Task task1 = new Task(() =>
            {
                string cDelete;
                cDelete = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 & echo off | clip ";
                System.Diagnostics.Process.Start("CMD.exe", cDelete);

            });
            Task task2 = new Task(() =>
            {
                string cDelete;
                cDelete = "/c Javaws -uninstall & RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 & echo off | clip ";
                System.Diagnostics.Process.Start("CMD.exe", cDelete);

            });
            task1.Start();
            task.Wait(); //add this line
            if (task1.IsCompleted)
            {
                task2.Start();
            }

使用任务执行此操作将只等待流程执行/启动。相反,您应该使用该流程并调用
WaitUntilExit