BackgroundWorker process for Windows Media Player - c#

I need some help setting up a BackgroundWorker process for a Windows Media Player audio. I need to run the audio (WMP) directly from the BackgroundWorker, not from the main Thread, and that background process needs to remain opened until the end of the audio file, but even though the audio starts playing normal on PLAY, the BackgroundWorker stops, and therefore I don't think the audio is actually playing on that second Thread or backgroundWorker as is already closed.
The question I have is, how I can play this audio file using Windows Media Player (WMPLib) from a backgroundWorker that will remain opened until the end of the song?
using WMPLib;
namespace mediaplayer
{
public partial class MainWindow : Window
{
BackgroundWorker m_audioProcessingWorker;
public MainWindow()
{
InitializeComponent();
}
string filename = #"C:\audio\song1.mp3"
private void button_play_Click(object sender, EventArgs e)
{
// Create the Audio Processing Worker (Thread)
m_audioProcessingWorker = new BackgroundWorker();
m_audioProcessingWorker.DoWork += new DoWorkEventHandler(audioProcessingWorker_DoWork);
m_audioProcessingWorker.RunWorkerAsync();
m_audioProcessingWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(audioProcessingWorker_Completed);
}
private void audioProcessingWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
axWindowsMediaPlayer1.URL = filename;
axWindowsMediaPlayer1.Ctlcontrols.play();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void audioProcessingWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Audio is finished");
}
private void button_stop_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
m_audioProcessingWorker.CancelAsync();
}
private void button_pause_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
}
}
Thanks.

The UI I have it's causing the audio playback to freeze up or stalls
when loading big chunks of data inside the same Thread UI, and I would
prefer to have that audio process running on a separate thread, that
why I though of BackgroundWorker.
You should rather use BackgroundWorker (or better yet, Task.Run or naturally async IO APIs) to load big chunks of data in the background and keep the main UI thread lag-free. Then create and use the WMP control on the UI thread.
If you can't refactor the code this way and want to use a background thread for WPM, keep in mind such thread has to pump Windows messages, otherwise the WPM control may not function properly. For this purpose, you could use my ThreadWithAffinityContext from here.

Related

C# timer backgroundworker user controls

I have a C# Windows Form Application that has a menu called Start Download Files and thi menu have then two instantiated user controls(submenu, tab menus).
For each tab (user control) i have a download button and a timer that runs it every 5 minutes.
I am using a private background worker which is created every time the control loads and runs a method to start download the files. That gives me a lot of troubles which i still can't find a solution for because:
- when i enable the timer for both controls they start the download multiple times for each and i get into concurrency accessing the files or
- cross thread exceptions
Does someone experienced something similar and maybe can give me a hint?
My code looks like this:
public partial class ucGeneralInfo : UserControl
{
private BackgroundWorker backgroundWorker;
private void TimerDownloadFrequency_Tick(object sender, ElapsedEventArgs e)
{
if (backgroundWorker.IsBusy != true)
{
backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
backgroundWorker.RunWorkerAsync(Branch);
}
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
Download_Process((string) e.Argument);
}
private void Download_Process(string Branch)
{
// copying files
// processing files
}
}
}
For concurrent access, use lock.
lock(fileLock)
{
wc.DownloadFileAsync (/*...*/);
}
For Cross-thread exception use Invoke .
Invoke(new MethodInvoker(delegate
{
timer.Enable = true;
}));

C# Threading still make UI unresponsive

Well , I have a form1 which has buttons and if you click one of its button
It would load the UserControl into panel in form1
That usercontrol1 contains a lot of data like Database,charts and picture boxes too. So it would definitely make the User Interface unresponsive while loading.
So I read some article and I found out that I need to run it through another thread so I tried it and it just increase the performance by a little bit.
The usercontrol1 still make the GUI unresponsive for about 3-5 sec and what if my data become larger.
I want to make it responsive and show to user that still loading by running the animated picturebox and stop if its finish loading
here is my code:
private void click_dashb_Click(object sender, EventArgs e)
{
ParameterizedThreadStart pts = new ParameterizedThreadStart(load_UserControl);
Thread t = new Thread(pts);
t.Start();
//Animated Picturebox to show user that UI is loading
pictureBox1.Enabled = true;
hover.Location = new Point(42, 130);
}
private void load_UserControl(object state)
{
Invoke(new Action(() =>
{
//load user control through another thread
while (panel1.Controls.Count > 0)
panel1.Controls[0].Dispose();
Home frm = new Home();
frm.AutoScroll = true;
panel1.Controls.Add(frm);
frm.Show();
}));
//Stop the animated GIF means the load is finish!
pictureBox1.Enabled = false;
}
If you help me about this problem. I might apply it to all of my works. because most of it contains large data.
Thanks stackoverflow community :)
EDIT:
After reading the comments suggesting to use Background worker . I tried to use it. but still getting a little bit unresponsiveness
Here's the new code:
private void click_dashb_Click(object sender, EventArgs e)
{
bgw.RunWorkerAsync();
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
BeginInvoke((MethodInvoker)delegate
{
while (panel1.Controls.Count > 0)
panel1.Controls[0].Dispose();
Home frm = new Home();
frm.AutoScroll = true;
panel1.Controls.Add(frm);
frm.Show();
});
}
catch (Exception x)
{
MessageBox.Show("An error occured while performing operation" + x);
}
}
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("Operation Cancelled");
}
else
{
MessageBox.Show("Operation Completed");
}
}
It's a little bit better but i still got a little unresponsiveness. Can you check out my code and what's wrong with this.?
thanks again
The problem with your code is that although you run the load_UserControl code in a new thread, that code calls Invoke which effectively makes al the code run on the UI thread again. I can image you did that because accessing Forms and PictureBoxes requires running on the UI thread.
The solution (in general) is to do non-UI work on a seperate thread and then switch back to the UI thread to update the visual controls.
To do this, you can conveniently use the BackgroundWorker class. In the DoWork event handler you do the heavy computation, in the RunWorkerCompleted event handler you update the controls. If you want, you can even update some controls (like a progressbar) during the work by means of the ProgressChanged event handler.
Well, just starting a new thread doesn't make the UI responsive by definition. You need to make the thread so that it actually does stuff in parallel.
Your thread does not, as it basically executes all code in this.Invoke.
That being said: Your code needs to be executed in this.Invoke, as almost everything you do needs to be done on the UI thread.
So in your case, there's really no point in parallelizing stuff, as there's no way to do what you want to do without blocking the UI thread and no technique I know of (Threads, Tasks, BackgroundWorker, etc.) will solve this problem.

How to play sounds asynchronuously, but themselves in a queue?

I just want to play 4 sounds after each other (sounds1->sound2->sound3), but without stopping the flow in my code during each play or without waiting for each sound to finish.
I have searched for this about everywhere, but every direction I read, gets stuck in some other problem.
My best bet so far was: using my already used SoundPlayer from System.Media and make my own queue function, but Soundplayer doesn't have a "finished playing" event so I have no idea of knowing when to start the next sound. (Really, Microsoft?)
Other solution and problems:
DirectSound seems complicated to get working in .NET (c#).
Win Playsound doesn't really help because it can't queue either.
You can try to use PlaySync on a thread outside UI, eg: Background thread, as some people have commented.
Here is a sample (untested) using a thread-safe* BlockingCollection for the queue
* which you can use in and outside the thread
You may want to make your own class or methods that rises an event every time the sounds ends. Or you can just loop the queue in the thread since PlaySync will just wait by itself.
using System.Threading;
using System.Collections.Concurrent;
namespace PlaySound
{
public partial class Form1 : Form
{
private Thread soundPlayThread;
private BlockingCollection<string> speakQueue = new BlockingCollection<string>();
private CancellationTokenSource cancelSoundPlay;
private int soundPlayCount = 0;
public Form1()
{
InitializeComponent();
cancelSoundPlay = new CancellationTokenSource();
}
private void btnStartSoundPlay_Click(object sender, EventArgs e)
{
StartSoundPlay();
}
private void btnStopSoundPlay_Click(object sender, EventArgs e)
{
cancelSoundPlay.Cancel();
Console.WriteLine("Sound play cancelled.");
}
private void btnAddToQueue_Click(object sender, EventArgs e)
{
speakQueue.Add("MyFile.wav");
}
private void queueAndPlay(string loc)
{
if (!File.Exists(loc=loc+".wav"))
loc=configPath+"soundnotfound.wav";
speakQueue.Add(loc);
StartSoundPlay();
}
private void StartSoundPlay()
{
//Sound Player Loop Thread
if (this.soundPlayThread == null || !this.soundPlayThread.IsAlive)
{
this.soundPlayThread = new Thread(SoundPlayerLoop);
this.soundPlayThread.Name = "SoundPlayerLoop";
this.soundPlayThread.IsBackground = true;
this.soundPlayThread.Start();
Console.WriteLine("Sound play started");
}
}
//Method that the outside thread will use outside the thread of this class
private void SoundPlayerLoop()
{
var sound = new SoundPlayer();
foreach (String soundToPlay in this.speakQueue.GetConsumingEnumerable(cancelSoundPlay.Token))
{
//http://msdn.microsoft.com/en-us/library/system.media.soundplayer.playsync.aspx
speaker.SoundLocation=soundToPlay;
//Here the outside thread waits for the following play to end before continuing.
sound.PlaySync();
soundPlayCount++;
Console.WriteLine("Sound play end. Count: " + soundPlayCount);
}
}
}
}

Backgroundworker, not running the progressbar

How can I fix this issue ?
I am expecting the progressbar to load during process untill process it is done
Here is my code:
private void btnProcess_Click(object sender, EventArgs e)
{
backgroundWorker.WorkerReportsProgress = true;
backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
backgroundWorker.DoWork += backgroundWorker_DoWork;
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
//start transaction
DoTransaction();
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
My transaction function:
private void DoTransaction()
{
string pathIdentifier;
pathIdentifier = func.checkthePathFile();
if (pathIdentifier == null)
{
MessageBox.Show("Path has no yet been specified!");
}
else
{
//create xml base from user inputs
XElement transactXML = new XElement("Transaction",
new XElement("CardNumber", txtCardNum.Text.Trim()),
new XElement("ExpireDate", txtExpDate.Text.Trim()),
new XElement("Cardtype", txtCardType.Text.Trim())
);
//save xml to a file
transactXML.Save(pathIdentifier + "/sample.xml");
}
}
How is the runtime supposed to know how far along your process is?
You need to tell it by calling backgroundWorker.ReportProgress from the background operation. No magic here.
MSDN: http://msdn.microsoft.com/en-us/library/ka89zff4.aspx
Break down your process into meaningful chunks and ReportProgress whenever it makes sense to do so.
public void DoTransaction()
{
part1();
backgroundWorker.ReportProgress(25);
part2();
backgroundWorker.ReportProgress(50);
part3();
backgroundWorker.ReportProgress(75);
part4();
backgroundWorker.ReportProgress(100);
}
Edit Based on Posting of Transaction() function
If you are not confident in writing multithreaded programs, then do not attempt to write multithreaded programs, even with the help of a BackgroundWorker which tries to abstract some of those details away from you.
A few issues:
Your provided Transaction() method attempts to launch a MessageBox and read the Text property of various controls from the background thread. This is going to cause problems as the runtime typically throws an Exception when UI elements are accessed from a thread other than the one which created them.
If you really want to do the XML saving in the BackgroundWorker, you should validate the filename and directory, and save the Text properties to an intermediate object before setting up the BackgroundWorker and calling RunWorkerAsync.
Furthermore, in my opinion, your Transaction method is not going to be time intensive enough to truly warrant a background thread. Even a relatively old PC will be able to create and save a 15 element XML file faster than you can blink. The runtime will probably waste more time marshalling data between the threads than it would to simply write the file out to disk. Just do your work in the button click event handler.
needs some reference to the BackgroundWorker instance.pass the reference to the class when instantiating it.
instantiate like this
BackgroundWorker worker = sender as BackgroundWorker;
then call like this
`worker.ReportProgress(...)`

C# Wav File Playing freezes GUI

Problem: The wav files plays, but GUI does not respond at all. The lbl_PhonoString.Text is not updated every loop. User cannot pause or cancel until all the files are played completely.
I know I have to use a seaparate thread to play the Wav file. I am not at all familiar with threading. Can someone suggest what I should do here?
My Code:
//====================================================
bool bPause = false, bCancel = false;
private void btn_DicStart_Click(object sender,EventArgs e)
{
PlayWave("StartProgram.WAV");
for(int i=0;i<10;i++)
{
lbl_PhonoString.Text=i.ToString();
PlayWave("NextSound.WAV");
PlayWave("Clue"+i.ToString()+".WAV");
//Check if user pressed pause/cancel
while(bPause);
if(bCancel)
break;
}
}
private void btn_Pause_Click(object sender,EventArgs e)
{
bPause=!bPause;
if(!bPause)
btn_Pause.Text ="PAUSE";
else
btn_Pause.Text ="CONTINUE";
}
private void btn_Cancel_Click(object sender,EventArgs e)
{
bCancel=true;
}
//Play the Wave File
private void PlayWave(string WaveFile)
{
System.Media.SoundPlayer myPlayer=new System.Media.SoundPlayer();
myPlayer.SoundLocation=WaveFile;
myPlayer.PlaySync();
}
//====================================================
Look here essentially the same question and the accepted answer is your answer. In a nutshell, yes, you should be doing all long running processes in a seperate thread or they will block the UI, or use BackgroundWorker of course.

Categories