播放器02:顺序播放,用到了InvokeRequired 属性和委托(Delegate)的BeginInvoke方法

初学C#记录历程,记录心情。

UI:

View Code
  1                     using System;
  2                     using System.Collections.Generic;
  3                     using System.ComponentModel;
  4                     using System.Data;
  5                     using System.Drawing;
  6                     using System.Linq;
  7                     using System.Text;
  8                     using System.Windows.Forms;
  9                     using System.Threading;
 10 
 11                     namespace OrderPlay
 12                     {
 13                         public partial class myAudioPlayer : Form
 14                         {
 15 
 16                             private bool close = false;// 关闭窗口标志位
 17                             private bool running = false;//线程结束标志位
 18                             protected List<string> playList = new List<string>();  //定义一个list来保存加进到listbox里面的文件
 19 
 20                              private void SaveFiles()
 21                             {
 22                                 foreach (var fileName in openFileDialog.FileNames)
 23                                 {
 24                                     if (playList.Contains(fileName) == true)          //不加重复的   
 25                                     {
 26                                         continue;
 27                                     }
 28                                     else
 29                                     {
 30                                         playList.Add(fileName);          //保存原始文件到playList
 31                                         lstPlayer.Items.Add(DeletePath(fileName));      //添加文件到listbox
 32                                     }
 33                                 }
 34                             }
 35 
 36 
 37               
 38                             public myAudioPlayer()
 39                             {
 40                                 InitializeComponent();
 41                                 mediaPlayer.uiMode = "Full";
 42                             }
 43                                                 
 44        
 45                             //双击播放列表里面的文件,播放
 46                             private void lstPlayer_MouseDoubleClick(object sender, MouseEventArgs e)
 47                             {
 48                               mediaPlayer.URL = playList[lstPlayer.SelectedIndex];
 49                               
 50                             }
 51 
 52                             private void orderPlayToolStripMenuItem_Click(object sender, EventArgs e)
 53                             {
 54                                 if (!running)
 55                                 {
 56                                     // 启动另一线程检测 mediaPlayer 的播放状态,循环播放列表里的歌曲
 57                                     running = true;
 58                                     Thread thread = new Thread(new ThreadStart(this.CheckStatus));
 59                                     thread.IsBackground = false;   //设置后台线程为false
 60                                     thread.Start();
 61                                 }
 62                                 if (lstPlayer.Items.Count == 0)
 63                                 {
 64                                     MessageBox.Show("No file in List");
 65                                 }
 66                             }
 67 
 68                             //检查线程状态,顺序播放
 69                             private void CheckStatus()
 70                             {
 71                                 while (running && !close)
 72                                 {
 73                                     try
 74                                     {
 75                                         if (mediaPlayer.playState == WMPLib.WMPPlayState.wmppsStopped && lstPlayer.Items.Count > 0)  //是否播放停止且列表有文件
 76                                         {
 77                                             if (lstPlayer.InvokeRequired)           //是否跨线程
 78                                             {
 79                                                 lstPlayer.BeginInvoke(new MethodInvoker(() =>//BeginInvoke方法可以使用线程异步地执行委托所指向的方法
 80                                                 {
 81                                                     SetSelectedIndex();
 82                                                 }), null);
 83                                             }
 84                                             else
 85                                             {
 86                                                 SetSelectedIndex();
 87                                             }
 88 
 89                                         }
 90                                                                          
 91                                     }
 92                                     catch (Exception ex)
 93                                     {
 94                                         MessageBox.Show("出错" + ex.ToString());
 95                                     }
 96                                     System.Threading.Thread.Sleep(1000);//状态检测延时1秒,加快打开歌曲的速度
 97                                 }
 98                                 running = false;
 99                                                             
100 
101                             }
102 
103                             private void SetSelectedIndex()
104                             {
105                                 if (lstPlayer.SelectedIndex + 1 > lstPlayer.Items.Count - 1)  //是否到最下面
106                                 {
107                                     lstPlayer.SelectedIndex = 0;      //回到第一个
108                                 }
109                                 else
110                                 {
111                                     lstPlayer.SelectedIndex = lstPlayer.SelectedIndex + 1;   //下移
112                                 }
113                                 mediaPlayer.URL = playList[lstPlayer.SelectedIndex];   //播放
114             
115                             }
116 
117                             private void threadOrderPlay_FormClosing(object sender, FormClosingEventArgs e)
118                             {
119                                 this.close = true;
120                             }
121 
122                        
123                        
124 
125 
126 
127 
128                         }
129                     }
原文地址:https://www.cnblogs.com/bloomalone/p/2823660.html