VB.net sub没有运行HELP [英] VB.net sub not running HELP
问题描述
我正在创建一个名为WubStar的开源视频游戏(就像电子音乐的吉他英雄一样)。我得到了很好的部分然后遇到了错误。写子程序没有运行,全部!这里是我遇到错误的表格的代码,请看一下,我希望如果有人用一双新鲜的眼睛看着它们,它们将能够捕捉到一个(可能很小)错误,我无法捕捉,提前谢谢,
乔尼
VB。净代码(有点注释):
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):
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
推荐答案
我最好的猜测是,application.doevents行需要很长时间才能处理并且写入sub永远不会被调用,如msdn
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
调用此方法会导致当前线程在处理完所有等待窗口消息时被挂起。如果消息导致触发事件,则可以执行应用程序代码的其他区域。这可能会导致您的应用程序出现难以调试的意外行为。如果执行需要很长时间的操作或计算,通常最好在新线程上执行这些操作。有关异步编程的更多信息,请参阅异步编程模型(APM)。
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).
读取它其他
另外,你建议我做些什么来保持我的程序响应并继续运行我的其余代码,而不是经常等待发生的事情......
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总之,因为使用 System.Drawing
并涉及UI,看起来UI库是 System.Windows.Forms
(请在提问时始终标记,以及申请类型,在这种情况下,标记表格)。我的建议也适用于WPF UI。你当然需要使用线程。这不是性能或CPU利用率方面的并行性问题,而是应用程序的逻辑和工作流程。面向事件的UI应该由一个线程执行,该线程在用户不做任何输入时保持等待状态,而其他线程可以在提供某个任务时立即完成工作。
UI有一个特定的问题。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke
或
System.Windows.Threading的方法
。 Dispatcher
(对于Forms或WPF)或 System.Windows.Forms.Control
(仅限表单)。
您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
使用Treeview扫描仪和MD5的问题 [ ^ ]。
另请参阅有关线程的更多参考资料:
如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],
在启用禁用+多线程后控制事件未触发 [ ^ ]。
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[^].
我提到的代码中的一个问题很容易解决,节省了大量时间并使应用程序具有一定的可维护性,现在几乎被杀死了。我的意思是重复代码Dim Key1
,Dim Key2
等。相反,只需定义一个案例数组。你做的还会激发你写长开关
语句,这总是难以支持。相反,你应该有一个键字典:索引类型是键码或字符,值类型是与键相关的任何东西;一些类或结构。
我的CodeProject文章中显示了这个想法的一个非常普遍的扩展:动态方法调度程序 [ ^ ]。 (全名动态方法调度程序,没有更长的转换语句!;标题的简短描述部分现在从文章页面中消失了;我将不得不要求恢复它,因为缺少它可能会破坏标题的含义。)
这种方法非常易于使用,完全不需要长切换
或if
语句。钥匙可以是任何东西,甚至是一种类型。值可以是任何值,包括最重要的是委托实例。此委托实例提供了最大的灵活性:它替换了 switch case 中的任意代码块或if
语句。确实,没有更长的转换声明了!
请理解,这里没有人试图用技术来冲洗你并强迫你进行额外的工作,恰恰相反,这些作品试图让你的生活更美好。我们人类的设计并不是为了做很多乏味乏味的工作,但是我们大多数人都非常善于抽象和使用优雅的技巧。
祝你好运,>
-SA
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 longswitch
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 longswitch
orif
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 orif
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
这篇关于VB.net sub没有运行HELP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!