CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2010
    Posts
    23

    Thread doesn't work

    // CLASS: BackGroundMusicGP
    Collapse

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.IO;

    namespace BackGroundMusicGP
    {
    class BkGrndMusic
    {
    private string _command;
    private bool isOpen;
    [DllImport("winmm.dll")]

    private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);

    public void Close()
    {
    _command = "close MediaFile";
    mciSendString(_command, null, 0, IntPtr.Zero);
    isOpen = false;
    }

    public void Open(string sFileName)
    {
    _command = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
    mciSendString(_command, null, 0, IntPtr.Zero);
    isOpen = true;
    }

    public void Play(bool loop)
    {
    if(isOpen)
    {
    _command = "play MediaFile";
    if (loop)
    _command += " REPEAT";
    mciSendString(_command, null, 0, IntPtr.Zero);
    }
    }
    public void playMusic()
    {
    string path = "E:\\abc\\Habib.mp3";
    string filename = Path.GetFileName(path);
    Open(filename);
    Play(false);
    }
    }
    }

    ///////////////////////////
    Collapse

    public partial class Form1 : Form
    {
    BkGrndMusic objBkMusic;
    int i = 0;
    public Form1()
    {
    InitializeComponent();
    objBkMusic = new BkGrndMusic();
    }
    private void button1_Click(object sender, EventArgs e)

    {
    Thread t = new Thread(new ThreadStart(objBkMusic.playMusic));
    t.Start(); // it doesn't work
    //objBkMusic.playMusic(); // it works
    }
    How can i play background music using thread? when i call directly using object of the class then it works. But when i call using thread it doesn't work.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Thread doesn't work

    Have you stepped through the code in a debugger and checked the return values for errors?

  3. #3
    Join Date
    Oct 2010
    Posts
    23

    Re: Thread doesn't work

    Yes.There is no error. But no music heard. any statements after t.start() executes properly.
    Last edited by sah_forum; October 19th, 2010 at 12:27 AM.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Thread doesn't work

    Quote Originally Posted by sah_forum View Post
    Yes.There is no error. But no music heard. any statements after t.start() executes properly.
    I don't see you catching the return value of mciSendString. How do you know there isn't any errors?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured