线程无法正常工作 [英] Thread is not working
本文介绍了线程无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
//CLASS:BackGroundMusicGP
// CLASS: BackGroundMusicGP
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);
}
}
}
////////////////////////
///////////////////////////
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.
推荐答案
new Thread(new ThreadStart(objBkMusic.playMusic)).Start();
通常应该可以正常工作,但是有一个缓存.但是现在有消息队列中的消息被您的线程忽略了. UI线程默认情况下会处理这些消息,但对于非UI线程则不会.您可以在下面的链接中找到出租车:
http://www.pinvoke.net/default.aspx/winmm.mcisendstring [ ^ ]
您可能需要将表单的句柄传递给它,如下所示:
mciSendString(_command, null, 0, Form1.Handle);
http://msdn.microsoft.com/en-us/library/dd757161%28VS. 85%29 [ ^ ]
您当然会在BkGrndMusic类中需要一个可以容纳此句柄的属性,但您可能已经知道了;-)
祝你好运!
new Thread(new ThreadStart(objBkMusic.playMusic)).Start();
should normally work fine, but there is a cache. But there are now messages from the message queue ignored by your thread. The UI thread handles these messages by default but for a non UI thread it is not. You cab look at the link below:
http://www.pinvoke.net/default.aspx/winmm.mcisendstring[^]
You probably need to pass it the handle of your form, like this:
mciSendString(_command, null, 0, Form1.Handle);
http://msdn.microsoft.com/en-us/library/dd757161%28VS.85%29[^]
You would of course need a property in your BkGrndMusic class that can hold this handle, but you probably already got that ;-)
Good luck!
没有错误消息.但是听不到音乐.
@ E.F.奈博尔
您当然会在BkGrndMusic类中需要一个可以容纳此句柄的属性,但您可能已经知道了"
我对此没有足够的知识.能否请您提供代码.我应该更改我的 代码的什么地方?
No error messages. But no music heard.
@E.F. Nijboer
"You would of course need a property in your BkGrndMusic class that can hold this handle, but you probably already got that"
i don''t have sufficient knowledge on that.could you please provide code. where of my code should i change?
这篇关于线程无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文