3

I'm only starting learning c# (and programming in general). I'm doing a little college project that plays music based on strings. I'm using windows forms. When you press the "play" button a it starts a method with a for loop that goes through each character in the string one by one and plays the appropriate note. Works fine, but I've been trying to add a "stop" button that will stop the music as its playing, but so far when the music is playing the form is frozen and I cant hit any other buttons... Just wondering is the only way I can achieve what I want by having different threads?? (We havent really looked at threads yet!)

edit: Hey thanks all for answers!! that was very very fast! :)

bryanmac
  • 38,941
  • 11
  • 91
  • 99
Guye Incognito
  • 2,726
  • 6
  • 38
  • 72

4 Answers4

4

Look at the BackgroundWorker class to play the music in the background. It makes it much simpler than manipulating threads yourself.

Here's an overview:

http://www.albahari.com/threading/part3.aspx#%5FBackgroundWorker

bryanmac
  • 38,941
  • 11
  • 91
  • 99
3

Just wondering is the only way I can achieve what I want by having different threads??

Typically, this is the most common method for this to happen. I'd recommend taking a look at the BackgroundWorker component. It allows you to run the work ("playing the note") on a background thread, but also provides cancellation support.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • However, it's a "thread be nice" cancellation support... hopefully the code running in the BGW can be written in a "be nice" manner... (as forcibly trying to kill/stop a thread is an entirely different can of worms) –  Jun 14 '12 at 01:33
  • @pst True - but all "good" cancellation mechanisms in .NET are cooperative, including the new .NET 4 cancellation support. Killing threads is really never a good idea. – Reed Copsey Jun 14 '12 at 01:34
  • It should be easy to check and stop cleanly after each "note" is played, though, in this case. (At least, I'd think so) – Reed Copsey Jun 14 '12 at 01:35
  • Oh, I don't disagree :) I was just throwing it in. There are different views of what "stop[ping] a [method,] thread[s]" entails... –  Jun 14 '12 at 01:36
1

The simple answer to your question is YES.

You are playing your music in a loop on the main thread, which means while the code is executing in the loop you can't receive any events from the UI.

To fix this you need to run your music loop on a background thread.

BlackSpy
  • 5,563
  • 5
  • 29
  • 38
0

Use Application.DoEvents(); after each iteration if you don't want to use threads just yet. Theads are best solution but as I understood your question, you are waiting when you learn threads first :) Calling DoEvents() allows processing window messages when you call it, so if you call it often, windows shouldn't get blocked. But please consider this as a workaround, not the best solution. Best soultion will be using threads.

Community
  • 1
  • 1
Pol
  • 5,064
  • 4
  • 32
  • 51