I am a beginner in C# and I want to do the following:
Thread t = new Thread(new ThreadStart(this.play));
t.Start();
This is the method play
:
private void play()
{
playSong("path\\to\\song.mp3");
}
private static void playSong(String path)
{
mciSendString("open \"" + path + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
mciSendString("play MediaFile", null, 0, IntPtr.Zero);
}
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
When I execute the method just like this: play();
, without using threading, the song plays perfectly, but it doesn't play if I do like in the first code excerpt.
I suspect that it happens because I need to call the mciSendString
method in the main thread. If so, will someone tell me how can I do that? Or tell me how can I get it working using a thread?
I appreciate your help.
-- Edited from here --
I don't know if I should post the code, cause it is kind of large, but I will reduce it, to show what I want to do:
I have a windows forms application that starts a socket listening on a port when it begins, which works as a server. This server receives as requests paths to songs that it'll play. If I use the play
method as shown below, the form crashes, but the application still listens to my requests. I understand that I have to do it in the background, so that the form controls won't crash, I just don't know the better way to do it.
//Constructor
public Main()
{
InitializeComponent();
play();
}
play()
is something like this:
private void play()
{
//Does the socket initialization
do
{
//...
while (true)
{
//Reads text from client
//The text contains a string, indicating the path to the song.
byte[] bytesFrom = new byte[10025];
networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
string dataFromClient = System.Text.Encoding.Default.GetString(bytesFrom);
//Plays the song from the received path
playSong(dataFromClient);
//...Do other stuff
}
} while (_serverOn);
//Closes the socket connection
}
private static void playSong(String path)
{
mciSendString("open \"" + path + "\" type mpegvideo alias MediaFile", null, 0, IntPtr.Zero);
mciSendString("play MediaFile", null, 0, IntPtr.Zero);
}