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());
            }

        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值