t_tongtong 2016-07-27 08:18 采纳率: 0%
浏览 985

求助,c#多线程问题。(初学者)

下面是我写的一段程序,我设置了11个命令,但是他每次只运行了6个,我一直不解!希望有人帮我解答?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace xiancheng7.25
{
class Program
{
static void Main(string[] args)
{
//Thread t1 = new Thread(new ThreadStart(Thread1));
//Thread t2 = new Thread(new ThreadStart(Thread2));
//Thread t3 = new Thread(new ThreadStart(Thread3));
//Thread t4 = new Thread(new ThreadStart(Thread4));
//Thread t5 = new Thread(new ThreadStart(Thread5));
Thread t6 = new Thread(new ThreadStart(Thread6));
Thread t7 = new Thread(new ThreadStart(Thread7));
Thread t8 = new Thread(new ThreadStart(Thread8));
Thread t9 = new Thread(new ThreadStart(Thread9));
Thread t10 = new Thread(new ThreadStart(Thread10));
Thread t11 = new Thread(new ThreadStart(Thread11));
//t1.Priority = ThreadPriority.Normal;
//t2.Priority = ThreadPriority.Normal;
//t3.Priority = ThreadPriority.Normal;
//t4.Priority = ThreadPriority.Normal;
//t5.Priority = ThreadPriority.Normal;
t6.Priority = ThreadPriority.Normal;
t7.Priority = ThreadPriority.Normal;
t8.Priority = ThreadPriority.Normal;
t9.Priority = ThreadPriority.Normal;
t10.Priority = ThreadPriority.Normal;
t11.Priority = ThreadPriority.Normal;
//t1.Start();
//t2.Start();
//t3.Start();
//t4.Start();
//t5.Start();
t6.Start();
t7.Start();
t8.Start();
t9.Start();
t10.Start();
t11.Start();
}
public static void Thread1()
{
//string str = Console.ReadLine();
dosth();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = false;//不显示程序窗口
p.Start();//启动程序

        //向cmd窗口发送输入信息
        p.StandardInput.WriteLine("d:");
        p.StandardInput.WriteLine("cd d:\\test\\a");
        // p.StandardInput.WriteLine("make clean");
        p.StandardInput.WriteLine("make ");
        //向cmd窗口发送输入信息
        p.StandardInput.WriteLine("exit");
        //获取cmd窗口的输出信息
        string output = p.StandardOutput.ReadToEnd();


        p.WaitForExit();//等待程序执行完退出进程
        p.Close();


        Console.WriteLine(output);
       // Console.ReadLine();

    }
    public static void Thread2()
    {
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.UseShellExecute = false;    //是否使用操作系统shell启动
        p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
        p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
        p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
        p.StartInfo.CreateNoWindow = false;//不显示程序窗口
        p.Start();//启动程序

        //向cmd窗口发送输入信息
        p.StandardInput.WriteLine("d:");
        p.StandardInput.WriteLine("cd d:\\test\\b");
        // p.StandardInput.WriteLine("make clean");
        p.StandardInput.WriteLine("make ");
        //向cmd窗口发送输入信息
        p.StandardInput.WriteLine("exit");
        //获取cmd窗口的输出信息
        string output = p.StandardOutput.ReadToEnd();


        p.WaitForExit();//等待程序执行完退出进程
        p.Close();


        Console.WriteLine(output);
        //Console.ReadLine();


    }

    …………………………………………
    public static void dosth()
    {//用来模拟复杂运算

        for (int j = 0; j < 10000000; j++)
        {
            int a = 15;
            a = a * a * a * a;
        }
    }
}

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-07-27 15:10
    关注

    t11.Start();
    后面加上
    Console.Read();
    让主线程保持住。

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办