using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Audio a = new Audio();
Thread t1 = new Thread(new ThreadStart(a.Play));
t1.IsBackground = false;
t1.Start();
Thread t2 = new Thread(new ThreadStart(a.Stop));
t2.IsBackground = false;
t2.Start();
}
}
public class Audio
{
[DllImport("winmm.dll")]
private static extern int mciSendString
(
string lpstrCommand,
string lpstrReturnString,
int uReturnLength,
int hwndCallback
);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetShortPathName
(
[MarshalAs(UnmanagedType.LPTStr)] string path,
[MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
int shortPathLength
);
public Audio()
{
}
public void Play()
{
string FileName = @"C:\Users\Administrator\Desktop\back0.wav";
StringBuilder shortPathTemp = new StringBuilder(255);
int result = GetShortPathName(FileName, shortPathTemp, shortPathTemp.Capacity);
string ShortPath = shortPathTemp.ToString();
mciSendString("open " + ShortPath + " alias song", "", 0, 0);
mciSendString("play song", "", 0, 0);
}
public void Stop()
{
Thread.Sleep(3000);
mciSendString("stop song", "", 0, 0);
}
}
}
代码如上所示, 我想在 t2 线程中中止 t1 线程正在播放的音乐,该如何做到。求指导。