司机与售票员的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProcComu
{
    public partial class MainForm : Form
    {
        //用整型来标识进程状态
        //0表示还没创建
        //1表示正在运行
        //2表示挂起
        //3表示终止

        public int driverState = 0;//0表示进程没有被创建
        public int clerkState = 0;
        public System.Threading.Thread driverThread;
        public System.Threading.Thread clerkThread;
        public System.Threading.Semaphore run;
        public System.Threading.Semaphore stop;
        private int driverIndex = 0;
        private int clerkIndex = 0;

        public MainForm()
        {
            InitializeComponent();
            DriverFunc.Text = "";
            ClerkFunc.Text = "";
            run = new System.Threading.Semaphore(0,1);
            stop = new System.Threading.Semaphore(0,1);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1://启动司机进程
                    {
                        if (driverState==0 || driverState==3)
                        {
                            driverThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.DriverThread));
                            driverThread.Start();
                            driverState = 1;//1表示线程在运行
                            Driver.Text = "运行";
                            DriverFunc.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("司机进程已经在运行");
                        }
                        break;
                    }
                case Keys.F2://挂起司机进程
                    {
                        if (driverState == 1)
                        {
                            driverThread.Suspend();
                            driverState = 2;
                            Driver.Text = "挂起";
                        }
                        else if (driverState == 0)
                        {
                            MessageBox.Show("进程还没启动呢");
                        }
                        else if (driverState == 3)
                        {
                            MessageBox.Show("进程已终止");
                        }
                        break;
                    }
                case Keys.F3://创建售票员进程
                    {
                        if (clerkState==0 || clerkState==3)
                        {
                            clerkThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.ClerkThread));
                            clerkThread.Start();
                            clerkState = 1;
                            Clerk.Text = "运行";
                            ClerkFunc.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("售票员进程已经在运行");
                        }
                        break;
                    }
                case Keys.F4://挂起售票员进程
                    {
                        if (clerkState == 1)
                        {
                            clerkThread.Suspend();
                            clerkState = 2;
                            Clerk.Text = "挂起";
                        }
                        else if (clerkState == 0)
                        {
                            MessageBox.Show("进程还没启动");
                        }
                        else if (clerkState == 3)
                        {
                            MessageBox.Show("进程已终止");
                        }
                        break;
                    }
                case Keys.F5://查看行车轨迹
                    {
                        DriverFunc.Enabled = true;
                        ClerkFunc.Enabled = true;
                        break;
                    }
                case Keys.F6://恢复功能键
                    {
                        DriverFunc.Enabled = false;
                        ClerkFunc.Enabled = false;
                        break;
                    }
                case Keys.F7://恢复司机进程
                    {
                        if (driverState == 0)
                            MessageBox.Show("进程还没创建呢");
                        else if (driverState == 3)
                            MessageBox.Show("进程已终止");
                        else if (driverState == 2)
                        {
                            driverThread.Resume();
                            Driver.Text = "运行";
                            driverState = 1;
                        }
                        break;
                    }
                case Keys.F8://恢复售票员进程
                    {
                        if (clerkState == 0)
                            MessageBox.Show("进程还没创建呢");
                        else if (clerkState == 3)
                            MessageBox.Show("进程已终止");
                        else if (clerkState == 2)
                        {
                            clerkThread.Resume();
                            Clerk.Text = "运行";
                            clerkState = 1;
                        }
                        break;
                    }
                case Keys.F9://杀死两个进程
                    {
                        if (driverState != 0)
                        {
                            driverThread.Abort();
                            driverState = 3;
                            Driver.Text = "终止";
                        }
                        else
                        {
                            MessageBox.Show("还没启动司机进程");
                        }
                        if (clerkState != 0)
                        {
                            clerkThread.Abort();
                            clerkState = 3;
                            Clerk.Text = "终止";
                        }
                        else
                        {
                            MessageBox.Show("还没启动售票员进程");
                        }
                        break;
                    }
            }
        }

        public void DriverThread()
        {
            while (true)
            {
                run.WaitOne();
                driverIndex = 0;
                DriverFunc.Invoke(new ShowData(DriverFunShow));
                System.Threading.Thread.Sleep(1000);
                driverIndex = 1;
                DriverFunc.Invoke(new ShowData(DriverFunShow));
                System.Threading.Thread.Sleep(1300);
                driverIndex = 2;
                DriverFunc.Invoke(new ShowData(DriverFunShow));
                System.Threading.Thread.Sleep(1500);
                stop.Release();
            }
        }

        public void ClerkThread()
        {
            while (true)
            {
                clerkIndex = 0;
                ClerkFunc.Invoke(new ShowData(ClerkFunShow));
                System.Threading.Thread.Sleep(900);
                clerkIndex = 1;
                ClerkFunc.Invoke(new ShowData(ClerkFunShow));
                System.Threading.Thread.Sleep(1400);
                run.Release();
                clerkIndex = 2;
                ClerkFunc.Invoke(new ShowData(ClerkFunShow));
                System.Threading.Thread.Sleep(2000);
                stop.WaitOne();
                clerkIndex = 3;
                ClerkFunc.Invoke(new ShowData(ClerkFunShow));
                System.Threading.Thread.Sleep(1800);
                clerkIndex = 4;
                ClerkFunc.Invoke(new ShowData(ClerkFunShow));
                System.Threading.Thread.Sleep(800);
            }
        }

        public delegate void ShowData();

        public void DriverFunShow()
        {
            switch (driverIndex)
            {
                case 0:
                    {
                        DriverFunc.Text += System.DateTime.Now.ToLongTimeString();
                        DriverFunc.Text += "  开车.../r/n";
                        break;
                    }
                case 1:
                    {
                        DriverFunc.Text += System.DateTime.Now.ToLongTimeString();
                        DriverFunc.Text += "  正常行驶.../r/n";
                        break;
                    }
                case 2:
                    {
                        DriverFunc.Text += System.DateTime.Now.ToLongTimeString();
                        DriverFunc.Text += "  到站停车.../r/n";
                        break;
                    }
            }
        }

       public void ClerkFunShow()
        {
            switch (clerkIndex)
            {
                case 0:
                    {
                        ClerkFunc.Text += System.DateTime.Now.ToLongTimeString();
                        ClerkFunc.Text += "  乘客正在上车.../r/n";
                        break;
                    }
                case 1:
                    {
                        ClerkFunc.Text += System.DateTime.Now.ToLongTimeString();
                        ClerkFunc.Text += "  关车门.../r/n";
                        break;
                    }
                case 2:
                    {
                        ClerkFunc.Text += System.DateTime.Now.ToLongTimeString();
                        ClerkFunc.Text += "  售票.../r/n";
                        break;
                    }
                case 3:
                    {
                        ClerkFunc.Text += System.DateTime.Now.ToLongTimeString();
                        ClerkFunc.Text += "  开车门.../r/n";
                        break;
                    }
                case 4:
                    {
                        ClerkFunc.Text += System.DateTime.Now.ToLongTimeString();
                        ClerkFunc.Text += "  乘客正在下车.../r/n";
                        break;
                    }
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}

阅读更多
文章标签: object class
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭