c#之線程講解

1.進程類

Process[] pro = Process.GetProcesses();//獲得當前打開的進程
Process.Start("calc");//打開calc進程
ProcessStartInfo pro = new ProcessStartInfo(Path)//打開路徑下的文件;
Process p = new Process();
p.StartInfo = pro;

2.線程類

Thread th = new Thread(Test);
//標記這個線程準備就緒了,可以隨時被執行,具體什麼時候執行這個線程由CPU決定
th.IsBackground = true;//設置後臺線程
th.Start();
//前臺行程:只有所有的前臺線程都關閉了才能完成程序關閉。//新創建的都爲前臺線程
//後臺線程:只有所有的前臺線程結束,後臺線程自動結束。
//新線程訪問主線程的資源(.NeT不允許跨線程訪問)
//取消跨線程的訪問
Control.CheckForIllegalCrossThreadCalls = false;

3.音樂播放器

在這裏插入圖片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _16天
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        List<string> pathlist = new List<string>();
        string path;
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Multiselect = true;
            openFile.Title = "音樂播放器";
            openFile.Filter = "音樂| *.mp3|所有文件|*.wav;*.mp4";
            openFile.ShowDialog();
            string[] path = openFile.FileNames;
            //foreach (var item in path)
            //{
            //    listBox1.Items.Add(System.IO.Path.GetFileName(item));
            //}
            for (int i = 0; i < path.Length; i++)
            {
                listBox1.Items.Add(System.IO.Path.GetFileName(path[i]));
                pathlist.Add(path[i]);
            }
            
        }
        /// <summary>
        /// 下一曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            int Index = listBox1.SelectedIndex;
            Index++;
            if(Index == listBox1.Items.Count)
            {
                Index = 0;
            }
            sp.SoundLocation = pathlist[Index];
            sp.Play();
            listBox1.SelectedIndex = Index;
        }
        SoundPlayer sp = new SoundPlayer();
        /// <summary>
        /// 雙擊播放音樂
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        public static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);
        
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //mciSendString("Open " + pathlist[listBox1.SelectedIndex] + " alias wavType", null, 0, 0);//path是聲音文件路徑
            //mciSendString("Play wavType", null, 0, 0);
            sp.SoundLocation = pathlist[listBox1.SelectedIndex];
            sp.Play();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int Index = listBox1.SelectedIndex;
            Index--;
            if (Index == -1)
            {
                Index = listBox1.Items.Count-1;
            }
            sp.SoundLocation = pathlist[Index];
            sp.Play();
            listBox1.SelectedIndex = Index;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
                ;

        }
    }
}

4.搖獎號

在這裏插入圖片描述

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

namespace 搖獎機
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool flag = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if(flag == false)
            {
                button1.Text = "暫停";
                flag = true;
                Thread th = new Thread(PlayGame);
                th.Start();
            }
            else
            {
                flag = false;
                button1.Text = "開始";
            }

        }
        Random r = new Random();
        private void PlayGame()
        {
            while(flag == true)
            {
                label1.Text = r.Next(0, 10).ToString();
                label2.Text = r.Next(0, 10).ToString();
                label3.Text = r.Next(0, 10).ToString();
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }
    }
}

5.帶參數的線程方法

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

namespace 線程執行帶參數的方法
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(Test);
            th.Start("123");
        }
        /// <summary>
        /// 線程方法執行的參數必須是Object類型,填寫參數的位置需要注意。
        /// </summary>
        /// <param name="s"></param>
        public void Test(Object s)
        {
            for (int i = 0; i < 10000; i++)
            {
                Console.WriteLine(i.ToString());
            }

        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章