Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod doesn't work when executed in a thread, but works otherwise - C#
    primarykey
    data
    text
    <p>I am a beginner in C# and I want to do the following:</p> <pre><code>Thread t = new Thread(new ThreadStart(this.play)); t.Start(); </code></pre> <p>This is the method <code>play</code>:</p> <pre><code> 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); </code></pre> <p>When I execute the method just like this: <code>play();</code>, without using threading, the song plays perfectly, but it doesn't play if I do like in the first code excerpt.</p> <p>I suspect that it happens because I need to call the <code>mciSendString</code> 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?</p> <p>I appreciate your help.</p> <p><strong>-- Edited from here --</strong></p> <p>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:</p> <p>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 <code>play</code> 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.</p> <pre><code> //Constructor public Main() { InitializeComponent(); play(); } </code></pre> <p><code>play()</code> is something like this:</p> <pre><code> 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload