Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am creating an open source video game called "WubStar"(Like guitar hero for electronic music). I got a good portion through and then encountered an error. The Write sub-routine isn't running, AT ALL! Here's the code for the form I encountered the error on, please take a look, I'm hoping that if someone looks at it with a fresh set of eyes they'll be able to catch a (probably small) error that I wasn't able to catch, thanks in advance,

Jordan

VB.net code(Somewhat annotated):
VB
Imports System.Drawing
Imports System.IO

Public Class Form2

    'Game Palet
    Dim pWidth As Integer = 750
    Dim pHeight As Integer = 550
    Dim tSize As Integer = 32

    'Graphics Vars
    Dim G As Graphics
    Dim BBG As Graphics
    Dim BBI As Bitmap
    Dim pTile As Bitmap
    Dim S1 As Bitmap
    Dim S2 As Bitmap
    Dim sRect As Rectangle
    Dim dRect As Rectangle

    'Framerate Vars
    Dim tSec As Integer = TimeOfDay.Second
    Dim Tick As Integer = 0
    Dim Max As Integer = 0

    'Game State
    Dim isRunning As Boolean = True
    Public isWaiting As Boolean = False

    'Mouse Mapping
    Dim mouseX As Integer = 15
    Dim mouseY As Integer = 9

    'Mouse Button Handler Vars
    Dim lMouse As Boolean = False
    Dim rMouse As Boolean = False

    'Key Handler Vars
    Dim Key1 As Boolean = False
    Dim Key2 As Boolean = False
    Dim Key3 As Boolean = False
    Dim Key4 As Boolean = False
    Dim Key5 As Boolean = False
    Dim Key6 As Boolean = False
    Dim Key7 As Boolean = False
    Dim Key8 As Boolean = False
    Dim Up As Boolean = False
    Dim Down As Boolean = False

    'Note Mapping
    Dim mLength(tSize * 4, 0) As Integer
    Dim nMap(11, 0) As Integer
    Dim nMapSave(11, 0) As Integer
    Dim nMax As Integer = 0
    Dim Count As Integer = 0
    Dim inc As Integer = 8
    Dim gMax As Integer
    Dim dCount As Integer = 0
    Dim nMapSize As Integer = tSize / 4

    'Song Vars
    Public Song As String
    Dim Path As String
    Dim Browse As OpenFileDialog
    Public LoadSong As Boolean = False

    Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MsgBox("nMap 0,0: " & nMap(0, 0))
        MainMenu.Show()
    End Sub

    Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Me.Show()
        Me.Focus()

        'Initialize Graphics
        G = Me.CreateGraphics
        BBI = New Bitmap(pWidth, pHeight)
        pTile = New Bitmap(Sprites.BG.Image)
        S1 = New Bitmap(Sprites.S1.Image)
        S2 = New Bitmap(Sprites.S2.Image)

        'Initialize nMapping Vars
        If LoadSong = False Then
            For i As Integer = 0 To 11
                nMap(i, 0) = 0
            Next
        End If

        'Initialize Game Vars
        If LoadSong = False Then
            Browse = New OpenFileDialog
            Browse.Filter = "Audio Wave Files|*.wav"
            Browse.Title = "Open Song"
            If Browse.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
                Path = Browse.FileName()
            Else
                MsgBox("Couldn't open file", MsgBoxStyle.Critical, "Error")
                End
            End If
            Song = InputBox("Song Name:", "Name")
            If Not Song = "" Then
                Try
                    MkDir(CurDir() & "\Data\" & Song)
                Catch ex As Exception
                    End
                    MsgBox(ex, MsgBoxStyle.Critical, "Error")
                End Try
                Try
                    'Save song in songs database
                    If My.Computer.FileSystem.FileExists(CurDir() & "\Data\Songs.slst") Then
                        Using Write As StreamWriter = New StreamWriter(CurDir() & "\Data\Songs.slst", True)
                            Write.WriteLine(Song)
                            Write.Close()
                            Write.Dispose()
                        End Using
                    Else
                        Using Write As StreamWriter = New StreamWriter(CurDir() & "\Data\Songs.slst")
                            Write.Write(Song & vbNewLine)
                        End Using
                    End If

                    'Create .wub file to store notes
                        Using Write As StreamWriter = New StreamWriter(CurDir() & "\Data\" & Song & "\" & Song & ".wub")
                            Write.WriteLine("#Generated: " & DateAndTime.Now)
                            Write.Close()
                            Write.Dispose()
                        End Using
                Catch ex As Exception
                    MsgBox(ex, MsgBoxStyle.Critical, "Error")
                    End
                End Try
                Try
                    FileCopy(Path, CurDir() & "\Data\" & Song & "\" & Song & ".wav")
                Catch ex As Exception
                    MsgBox(ex, MsgBoxStyle.Critical, "Error")
                End Try
            End If
        ElseIf LoadSong = True Then
            Me.Hide()
            LoadForm.Show()
            isWaiting = True
            Do While isWaiting = True
                Application.DoEvents()
            Loop
            LoadForm.Hide()
            Me.Show()
        End If
        Designer(Song)
        End
    End Sub

    Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.A
                Key1 = True
            Case Keys.S
                Key2 = True
            Case Keys.D
                Key3 = True
            Case Keys.F
                Key4 = True
            Case Keys.H
                Key5 = True
            Case Keys.J
                Key6 = True
            Case Keys.K
                Key7 = True
            Case Keys.L
                Key8 = True
            Case Keys.Escape
                isRunning = False
        End Select
    End Sub

    Private Sub Form1_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyCode
            Case Keys.A
                Key1 = False
            Case Keys.S
                Key2 = False
            Case Keys.D
                Key3 = False
            Case Keys.F
                Key4 = False
            Case Keys.H
                Key5 = False
            Case Keys.J
                Key6 = False
            Case Keys.K
                Key7 = False
            Case Keys.L
                Key8 = False
        End Select
    End Sub

    Private Sub Designer(ByVal Name As String)
        'On Error GoTo A
        G.DrawImage(pTile, 0, 0)
        MsgBox("Song load/creation successfull, going to designer", MsgBoxStyle.Information, "Success")
        Do While isRunning = True
            Application.DoEvents()
            Write()

            Draw()

            Save()
            'Check for Up/Down arrow events
            If Up = True & Count = 0 Then  Else If Up = True Then Count -= 1
            If Down = True & Count = nMax Then
                nMapSave = nMap
                nMax += 1
                ReDim nMap(11, nMax)
                nMap = nMapSave
                ReDim nMapSave(11, nMax)
                Count += 1
            ElseIf Down = True Then
                Count += 1
            End If
        Loop
    End Sub

    Private Sub Write()
        If Key1 = True & nMap(0, Count) = 0 Then
            nMap(0, Count) = 1
        ElseIf Key1 = True Then
            nMap(0, Count) = 0
        End If
    End Sub

    Private Sub Draw()

    End Sub

    Private Sub Save()

    End Sub
End Class
Posted
Comments
joshrduncan2012 7-Dec-12 17:59pm    
How do you know that particular sub is not running? Can you place a MessageBox in places around it to see where the breakdown happens?
Akbar Ali Hussain 7-Dec-12 18:01pm    
Which portion is not working?
Sergey Alexandrovich Kryukov 7-Dec-12 18:32pm    
This code is complete mess; I don't think it worth considering. First of all, you are trying to use DoEvents in order to simulate something resembling thread functionality. Don't do it. Explain what do you want to achieve by that, chances are, you need just real threads.

A number of key variables, long case which does nothing but assign True -- all this is totally pointless. The code should be written in civilized manner first, and then we can discuss what to fix...
--SA
Sicppy 11-Dec-12 20:54pm    
First of all, this code isn't supposed to be very pretty, its a free software it doesn't have to be super professional, second of all, by putting the Application.DoEvents() it keeps the program responsive, if I don't have that when the while loop is running it, (1) Won't register key press events (2) Wont't register when the form is closed, minimized, or maximized.
Sergey Alexandrovich Kryukov 11-Dec-12 22:11pm    
I read: "First of all, this code isn't supposed to be very pretty, its a free software it doesn't have to be super professional".

OK, you see, this closes the question. With such your attitude, there is no way I would agree to waste time on it. Thank you for asking your question. Maybe someone else will answer, but don't expect "super professional" help.

(And of course your use of DoEvents is perfectly wrong...)

--SA

My best guess is that the application.doevents line is taking long time to process and write sub is never called as stated at msdn
Quote:
Calling this method causes the current thread to be suspended while all waiting window messages are processed. If a message causes an event to be triggered, then other areas of your application code may execute. This can cause your application to exhibit unexpected behaviors that are difficult to debug. If you perform operations or computations that take a long time, it is often preferable to perform those operations on a new thread. For more information about asynchronous programming, see Asynchronous Programming Model (APM).


Read it here
 
Share this answer
 
Jordan wrote:
Also, what would you suggest I do to keep my program responsive and still continue the rest of my code running instead of constantly waiting for something to happen…
First of all, as System.Drawing is used and UI is involved, it looks like the UI library is System.Windows.Forms (please, always tag it when asking questions, as well as application type, in this case, tag "Forms"). My advice is also applicable to WPF UI. You certainly need to use threads. This is not a matter of parallelism in the sense of performance or CPU utilization, but a logic and a workflow of the application. The event-oriented UI should be executed by a thread which is kept in the wait state when the user does not do any input, while other threads can do their job as soon as some task is provided.

There is one problem specific to the UI. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
One of the problems in your code I mentioned can be easily resolved and save a lot of time and enable some maintainability of the application, which is now pretty much killed. I mean repeated code "Dim Key1", "Dim Key2", etc. Instead, simply define an array of case. What you do also provokes you to write long switch statement, which is always hard to support. Instead, you should have a dictionary of keys: index type is the key codes or a character, value types is whatever is associated with the key; some class or structure.

A very universal extension of this idea is shown in my CodeProject article: Dynamic Method Dispatcher[^]. (Full name "Dynamic Method Dispatcher", No more long switch statements!; the "short description" part of the title now disappeared from the article pages; I'll have to ask to restore it, because lack of it may mangle the meaning of the title.)

This approach is very easy to use and completely eliminate a need of long switch or if statements. The key can be anything, even a type. And a value can be anything, including, most importantly, a delegate instance. This delegate instance provides the most of flexibility: it replaced any arbitrary block of code in switch case or if statement. Indeed, no more long switch statements!

Please understand that no one here tries to brain-wash you with technological stuff and force you into extra work, just the opposite, such works attempt to make your life better. We human are not designed to do a lot of boring tedious work, but most of us are pretty good with abstractions and using elegant techniques.

Good luck,
—SA
 
Share this answer
 
Comments
Sicppy 12-Dec-12 17:21pm    
Thanks, I never thought about doing an array but that would probably be helpful
Sergey Alexandrovich Kryukov 12-Dec-12 17:26pm    
Certainly. Well, now think about it (array or collection which you can even persist and load if and when you see a need in it). Hope you can accept this advice, too -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900