long interval between StreamReader/StreamWriter processes

Hello, in my program I am using StreamReaders and StreamWriters,
and they work, but if I use the StreamReader or StreamWriter process,
it takes long before I can use them again, so now my question is:
can I make the process-intervals(sorry if this name doesn't exist) shorter
Or should I use an other way to read and write files, and who can be used in faster time-intervals

Grtz, Tom.




Answer this question

long interval between StreamReader/StreamWriter processes

  • Xzion

    @ Shakalama: Sorry, but I don't understand anything of what you wrote down there :s
    Mayby u could explain what it does

    Grtz, Tom.



  • Jeremy_AK

    I did that, but I still keep having the same problem :s

    Maybe I just should stop posting so fast, as my problems may stress u guys.

    Grtz, Tom.



  • tusAugusto

    hi,

    do you know the controls properties that you have in properties tab, you can write extra properties in your classes it will not appear in the properties tab because this tab for controls so when you make a property in your class(form) called level you can access it by code as you access text property in textbox control like (textbox1.text) in your case it will be (selectLevelForm.level) anyway to know about it more here its a part from a lesson http://www.programmersheaven.com/2/Les_VBNET_4_p3

    if you want to follow it up from the beginning here its the homepage

    http://www.programmersheaven.com/2/VB-NET-School

    best regards



  • FrenchiInLA

    What do you mean by "it takes long before I can use them again".

    Could it be that you are getting an exception like "Access Denied"/"Sharing Violation"/"File is in use" etc. In this case probably you don't "dispose" the streams.


  • RFU

    hi,

    first of all i don't think exposing form control to other forms is a good idea ,

    ' Send selected level to form1.
    form_main.lbllevel.Text = "1"

    i guess if you used properties will be better

    form level selection

    'field
    Private level As Integer = 1

    'property
    Public ReadOnly Property level As Integer
    Get
    Return
    _level
    End Get
    End Property

    in your mani form

    'field
    Private level As Integer = 1
    'property
    Public Property level As Integer
    Get
    Return
    _level
    End Get
    Set
    _level = value

    lbllevel.text = value
    End Set
    End Property

    in open form button

    dim level As levelselectorform = New levelselectorform
    dim result As DialogResult = level.showdialog

    if result = dialogresult.ok then

    level = levelselectorform.level

    'if you want to expose result to the user

    lbllevel.text = levelselectorform.level.tostring

    end if

    hope this helps



  • PAPutzback

    "New game"-button:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Call the level-selector form.
    Form3.Show()
    End Sub

    "Level-selector" form -> code:
    Public Class Form3

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' Send selected level to form1.
    form_main.lbllevel.Text = "1"
    Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' Send selected level to form1.
    form_main.lbllevel.Text = "2"
    Me.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    ' Send selected level to form1.
    form_main.lbllevel.Text = "3"
    Me.Close()
    End Sub

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.Text = "Level Selector v" & My.Application.Info.Version.ToString
    End Sub

    End Class

    lbllevel.textchanged:
    Private Sub lbllevel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbllevel.TextChanged

    ' Clear all fields
    For Each ctrl As Control In Me.Controls
    If ctrl.GetType Is GetType(TextBox) Then
    ctrl.Text = ""
    ctrl.BackColor = Color.White
    End If
    Next
    ' Fill in the data in the textboxes
    If lbllevel.Text = "1" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown1.Value - 1)
    Try
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "1" & "-" & intt & ".txt")lblall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    ElseIf lbllevel.Text = "2" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown2.Value - 1)
    Try
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "2" & "-" & intt & ".txt")lblall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    ElseIf lbllevel.Text = "3" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown3.Value - 1)
    Try
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "3" & "-" & intt & ".txt")lblall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    End If
    End Sub

    lblall.textchanged:
    ' The following code doesn't work as it should, but that is not the problem :p
    Private Sub lblall_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblall.TextChanged

    Dim intZ As Integer
    intZ = 1
    For Each ctrl As Control In Me.Controls
    If ctrl.GetType Is GetType(TextBox) Then
    If (Mid(lblall.Text, intZ, 1).ToString) = "g" Then
    intZ = intZ + 1
    ctrl.Text = (Mid(lblall.Text.ToString, intZ, 1))
    ctrl.BackColor = Color.BlueViolet
    ctrl.Enabled =
    False
    Else
    ctrl.Text = (Mid(lblall.Text.ToString, intZ, 1))
    End If
    intZ = intZ + 1
    End If
    Next
    ProgressBar1.Value = 90
    ' Look if gamesounds are allowed
    If form_settings.CheckBox1.CheckState = CheckState.Checked Then
    ' play 2.new.wav as newgame-sound
    Dim sp As New System.Media.SoundPlayer(Application.StartupPath & "\sound\2.new.wav")
    sp.Load()
    sp.Play()
    End If
    ProgressBar1.Value = 100
    End Sub

     

    Hope u don't get lost :)
    Grtz, Tom.



     

     



  • Cara2006

    I will chime in and say that I'm confused as well.

    Can you post the code for the 'New Game' button



  • Lih

    hi,

    i don't understand that too, would you clarify your question with short explanation and support it with code

    thx



  • rimmei

    No, I don't get any exceptions or errors, it's just that when I use the StreamReader or StreamWiter, I have to wait a couple of minutes before I can use them again, but i need to be able to use them faster than that.

    Grtz, Tom.



  • john.shu

    I still don't understand the problem. Why do you have to wait


  • Ryszard Kwiecinski - MSFT

    Thank you for that Mike, but now it doesn't work anymore after a second click because if I add your code, the text in lbllevel will be "" and not "1", "2" or "3", so nothing will happen, as i have programmed to work with "1", "2" or "3".

    Grtz, Tom.



  • byebyestar

    One thing I can spot is that if you choose the same level when you start a new game nothing will happen. The TextChanged event is not raised if the text does not actually changes. For example, if the label already contains "1" and you set the Text property to "1" again no TextChanged will be raised. One simple solution is to add

    form_main.lbllevel.Text = ""

    in Button_Click1, Button_Click2, Button_Click3 subs before setting the label Text.


  • DaveHinATL

    @ Mike: That's just the problem, I don't know how it comes I have to wait either.
    @ Shakalama: I shouldn't know how to clarify, as I have told u guys everything I know.
    It's just: I am building a 'game'. If the user presses the "new game" button, a StreamReader starts reading data from a textfile and fills this data in in a label.
    Now when I press the "new game" button, wait 5 secs. and then press it again, nothing happens.

    Here is the code I'm using:

    Private Sub lbllevel_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbllevel.TextChanged

    ' Clear all fields
    For Each ctrl As Control In Me.Controls
    If ctrl.GetType Is GetType(TextBox) Then
    ctrl.Text = ""
    'Set all textboes BackColor to White
    ctrl.BackColor = Color.White
    End If
    Next
    ' Fill in the textboxes that are set as given
    If lbllevel.Text = "1" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown1.Value - 1)
    Try
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "1" & "-" & intt & ".txt")lblall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    ElseIf lbllevel.Text = "2" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown2.Value - 1)
    T
    ry
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "2" & "-" & intt & ".txt")blall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    ElseIf lbllevel.Text = "3" Then
    Dim intt As Integer
    intt = (form_create.NumericUpDown3.Value - 1)
    Try
    Dim sr As New IO.StreamReader(Application.StartupPath & "\levels\" & "3" & "-" & intt & ".txt")lblall.Text = sr.ReadToEnd
    sr.Close()
    Catch a As System.IO.FileNotFoundException
    MsgBox(
    "Kan geen sudoku van dit level vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Geen sudoku gevonden")
    Catch b As System.IO.DirectoryNotFoundException
    MsgBox(
    "Kan de map met de levels niet vinden.", MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, "Map niet gevonden")
    End Try
    End If
    End Sub



  • selwonk

    I should have been a little more clear. Keep your code and add mine too:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    form_main.lbllevel.Text = ""


    ' Send selected level to form1.
    form_main.lbllevel.Text = "1"
    Me.Close()
    End Sub


  • long interval between StreamReader/StreamWriter processes