求教跨线程控制MCI播放音乐的问题
请问为何新开的线程里无法控制主线程实例化的mciPlayer呢,求高人指点,谢谢!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace threadingPlay
{
public partial class Form1 : Form
{
public clsMCI mciPlayer = new clsMCI();
public System.Windows.Forms.Timer winTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
winTimer.Tick += this.eventPlay;
winTimer.Interval = 500;
}
private void eventPlay(object sender, EventArgs e)
{
this.winTimer.Stop();
this.mciPlayer.Play("connect.mp3");
}
private void button1_Click(object sender, EventArgs e)
{
Action dm = () =>
{
this.winTimer.Start();
MessageBox.Show(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
};
dm.BeginInvoke(null, null);
}
private void button2_Click(object sender, EventArgs e)
{
mciPlayer.Stop();
MessageBox.Show(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
}
}
/// <summary>
/// clsMci 的摘要说明。
/// </summary>
public class clsMCI
{
public static uint SND_ASYNC = 0x0001;
public static uint SND_FILENAME = 0x00020000;
[DllImport("winmm.dll")]
public static extern uint mciSendString(string lpstrCommand,
string lpstrReturnString, uint uReturnLength, uint hWndCallback);
public void Play(string fileName)
{
mciSendString(@"close temp_alias", null, 0, 0);
mciSendString("open \"" + fileName + "\" alias temp_alias", null, 0, 0);
mciSendString("play temp_alias repeat", null, 0, 0);
}
public void Stop()
{
mciSendString("close all", null, 0, 0);
}
}
}