Sign up to the jQuery Grid Subscription list.

How to use Debugger

Original solution posted by ReneeC at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1141328&SiteID=1

Sometimes the idea of using the debugger, can be intimdating to those unfamiliar with them.
This is a tutorial based in a standard piece a source code that we will supply for the purposes of this tutorial. We're going to make a cheap MP3 player. I hope this tutorial is highly interactive and constructive.
Needed Ingredients:
1.) A test directory with about 4 mp3 files. Make them as short as possible.
2.) Create a new Windows project. In the form Designer, add:
a.) A progressbar called progressbar b.) Two buttons, One named btPlay and the other called btStop. In their respective text property boxes in the designer, insert Play and Stop. c.) A windows Media Player control. This probably is not in your toolbox. Go to Tools Choose Toolbox Items COM Windows Media Player. It will now appear at the bottom of your toolbox. Add a media player to your form and name it wmp in it's properties window in the forms designer.
3.) Close the Project to write it to a directory and re-open it
4.) Go to the source editor and depress Control-A and then depress the delete Key.
5.) Then select, copy and paste the following source code:
Imports AxWMPLib
Imports WMPLib

Imports System.Runtime.InteropServices
Public Class Form1
Protected Friend WithEvents tmr As New Timer
Private Duration As Single
Const SongDirectory As String = "InsertYourTestDirectoryPathHere"

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
tmr.Interval = 1000 : tmr.Stop()
dim a as integer = 0 'Demo not relevant to the code
a = 1 'Demo not relevant to the code
End Sub

Private Sub btPlay_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btPlay.Click
NewSong()
End Sub

Private Sub NewSong()
Dim Song as String = ""
ProgressBar.Value = 0
Song = ChooseRandomSong()
if Song <> "" then
wmp.URL = song
wmp.Ctlcontrols.play()
else
btPlay.enabled = false
beep()
end if
End Sub

Private Sub tmr_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tmr.Tick

If wmp.playState = WMPPlayState.wmppsPlaying Then
ProgressBar.Value = _
(wmp.Ctlcontrols.currentPosition / Duration) * 100
Else
tmr.Stop()
btPlay.PerformClick()
End If
End Sub

Private Sub Player_PlayStateChange(ByVal sender As System.Object, _
ByVal e As _ AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) _
Handles wmp.PlayStateChange

Dim NewState As WMPPlayState = e.newState

Select Case e.newState

Case WMPPlayState.wmppsBuffering
Case WMPPlayState.wmppsMediaEnded
ProgressBar.Value = 0
Case WMPPlayState.wmppsPaused
Case WMPPlayState.wmppsPlaying
Duration = wmp.currentMedia.duration
tmr.Start()
Case WMPPlayState.wmppsReady
Case WMPPlayState.wmppsReconnecting
Case WMPPlayState.wmppsScanForward
Case WMPPlayState.wmppsScanReverse
Case WMPPlayState.wmppsStopped
Case WMPPlayState.wmppsTransitioning
Case WMPPlayState.wmppsWaiting
Case Else
End Select
End Sub

Private Function ChooseRandomSong() As String
Dim DI As New System.IO.DirectoryInfo(SongDirectory)
Dim fi() As System.IO.FileInfo = DI.GetFiles()
Static Song As String = ""
Static LastSong As String = ""
Static SongList As New List(Of String) : SongList.Clear()
For Each file As System.IO.FileInfo In fi
If file.Extension.ToLower = ".mp3" Then
SongList.Add(file.FullName)
End If
Next

If SongList.Count = 0 Then Return ""
Static rnd As New Random
Static num As Int32
Song = LastSong
While Song = LastSong
num = rnd.Next(SongList.Count)
Song = SongList(num)
End While
LastSong = Song
SongList.Clear()
Return Song
End Function
End Class

Please note the board editor introduces spurious line wraps and may introduce syntax errors around continuations. if you see squiggles after adding everything, make all lines with the continuation character "_" one line, deleting the "_" and the space in front of it.
Be sure to insert your mp3 directory here:
Const SongDirectory As String = "InsertYourTestDirectoryPathHere"
Depress the save all files button above.
Go to View Toolbars and make sure that the debug toolbar is checked.

1.) Go to the source editor
2.) Find this line in the load event tmr.Interval = 1000 : tmr.Stop() (These are actually two lines of code separated by a ":".
3.) On exactly the same horizontal level as that line, to the left of the editor, do a single left click. A brown dot should appear. click until a brown dot does apear. [see Dave's comments below]
4.) In the debug toolbar, make sure the locals windows is depressed (activated).
5.) Depress the Green right triangle up above.
6.) What happened?
7.) Make sure you can see the Locals window.
8.) In the debugger toolbar, you will see the Green Arrow, and a [] square. To the right you will see a button with the a line and arrow pointing into the middle of lines. This is the "Step Into" control. On its right you will see the the "Step Over" control.
9.) Watching the Locals window, depress the Step Into button and look for a
10.) Depress the Step Into Button and look at a now.
11.) Finally depress the Green arrow.
What Happened?

1.) Make sure the debugger is not running. [ ] Button
2.) Go to your single existing break point by putting your cursor on the Brown dot representing the break point and left click on it. The Break point is deleted.
Now let's see if your project is working.
3.) Making sure your speakers are on, Depress the Green arrow, and your project should appear on the screen.
4.) Depress the Play button and you should hear one of your mp3s startup. Allow it to play through.
5.) While listening, put a new break point on this line:
Private Sub NewSong()
ProgressBar.Value = 0 ' Set a break here and wait for the ' song to finish.

6.) Depress the Step Over Button and observe the editor.

7.) Stop the project [ ].

8. Repeat step 4 and then 9.

9.) This time, depress the step into button oberving what happens, depress it again and again and again.

What was different?

1.) Stop your project [ ] and delete all of your previous break points.
2.) Insert a break point at this Next Statement
For Each file As System.IO.FileInfo In fi
If file.Extension.ToLower = ".mp3" Then
SongList.Add(file.FullName)
End If
Next
3.) Start your project with the Green Arrow. Depress the Play Button.
4.) When your breakpoint is reached, place your cursor here:
Static SongList As New List(Of String) : SongList.Clear()
on Songlist
5.) A window will appear. Put your mouse on the + boxes and new windows will appear
6.) Follow the + signs to the end.
7.) Depress the green arrow so you execute the loop again. Repeat steps 4 -6
8.) Repeat 7 again and again.
9.) Stop your Project
10.) Delete all Breakpoints
11. Place a breakpoint Here:
While Song = LastSong
num = rnd.Next(SongList.Count)
Song = SongList(num)
End While
LastSong = Song
SongList.Clear()Return Song ‘ Set your breakpoint here.
12.) Start your project
13.) Depress Play
What is the contents of Song?
1.) Stop your project.
2.) Delete your breakpoints.
3.) Place a point here:
Private Sub btPlay_Click(ByVal sender As Object, _
4.) Start your project
5.) Press Play......
6.) After observing that. Stop your project and delete all break points
7.) Place a break point here:

Private Sub tmr_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles tmr.Tick

If wmp.playState = WMPPlayState.wmppsPlaying Then
ProgressBar.Value = _
(wmp.Ctlcontrols.currentPosition / Duration) * 100
Else
tmr.Stop()
btPlay.PerformClick()
End If
End Sub
At tmr.stop
8.) Start your prject with the green arrow
9.) Depress Play
10.) Listen to the music until the end and watch the screen......

0 comments:

Related Ads