Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1046839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:12:30+00:00 2026-05-16T16:12:30+00:00

I am a beginner in C# and I want to do the following: Thread

  • 0

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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:12:31+00:00Added an answer on May 16, 2026 at 4:12 pm

    I had similar problem a while back… Pain in the butt.

    You are doing cross thread operations, that is not safe. That is the reason why it crashes. You are absolutely right about where you problem is.

    Here is msdn documentation on this thread operations http://msdn.microsoft.com/en-us/library/ms171728.aspx

    You need to add delegate definition for mciSendString => mciSendStringCallBack

    delegate void mciSendStringCallBack(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);

    In private static void playSong(String path) you need to check if InvokeRequired, if it is you need to instantiate callback and invoke it.

    mciSendStringCallBack method = new mciSendStringCallBack(....);
    this.Invoke(method , new object[] { .....});

    Look through the example on the msdn site, they do a good job demonstrating who it works.

    —

    You might want to try using background worker, it gives you multi threaded way of doing things, easier to work with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is more of a beginner's question. Say I have the following code: library(multicore)
I want to start learning Android but have the following doubts : (1) What
I am beginner.I have the following div structure in zend layout.phtml <div id='outer'>//start outer
As a MacRuby beginner I am working through this tutorial , and want to
I am a PHP beginner and I want to do the following: I want
(VB Express Level: Beginner) I want to do following, A column from Workbook 1
whats is wrong with following? (Level:Beginner) I want to add the answers obtained after
I'm sorry if this question sounds kinda newbie, but I'm a beginner and want
Could someone please help me in here, I'm just a beginner who want to
Im a Drupal beginner. I want to create a simple website, which has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.