Solved

Repeat a command

Posted on 2004-10-06
16
194 Views
Last Modified: 2010-04-23
Lets say that I have button "Run" and if I click it then it writes something to "textbox1".

How can I do so that it would repeat this commad number of times defined in "textbox2", every time deleting previously writed information.
0
Comment
Question by:Aetia
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 19

Expert Comment

by:Desp
ID: 12235244
you can do this in a FOR loop with its upper bound set to the value in the textbox2
0
 
LVL 19

Expert Comment

by:Desp
ID: 12235262
something close to this:

Dim Limit As Int32 = Convert.ToInt32(TextBox2.Text)
Dim i As Int32

For i = 1 To Limit
            TextBox1.Text = "The Value now is " + Limit.ToString()
Next
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12235290
I guess you want to loop as long as the value in textbox2 isn't reached.
I tried this. In my textbox2 was "2" as value entered

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Short
        For i = 1 To CShort(TextBox2.Text)
            MsgBox("TextBox1.Text = " & i)
            TextBox1.Text = i
        Next i
    End Sub
0
 

Author Comment

by:Aetia
ID: 12235461
Ok! Almost that what I am looking for...

How can I do it without msgbox? I meand it would coun times on textbox1 or say atleast after every repeat completed etc?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12235498
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Short
        For i = 1 To CShort(TextBox2.Text)
            TextBox1.Text = i
        Next i
    End Sub
0
 
LVL 19

Expert Comment

by:Desp
ID: 12235533
did you try my code?
0
 

Author Comment

by:Aetia
ID: 12235561
dhaest, Dhaest I've tried it...

Cast from string "TextBox2" to type 'Short' is not valid.
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 12235580
What value do you have in textbox2 ?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Aetia
ID: 12235589
OK! Maybe I can make it more simple for you... Here is what I want to make loop. When I click on this button it downloads website and shows me source code. What I want to do is to loop this command so it would generate number of hits to my website.

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

        Dim hgf As Short
        For hgf = 1 To CShort(TextBox2.Text)
            MsgBox("Label13.Text = " & hgf)
            Label13.Text = hgf
        Next hgf

        wr = New clsWebRequest(txtRequest.Text)
        If Not wr.RequestObj Is Nothing Then
            '- Set The Status Callback Address
            wr.StatusCallBack = AddressOf ReqCallBack

            '- Use Smart Auto Redirection (Cookie Support)
            wr.UseSmartRedirection = True

            '- Set Up The Request Headers
            If txtReferer.Text <> "" Then wr.RequestObj.Referer = txtReferer.Text
            If txtUserAgent.Text <> "" Then wr.RequestObj.UserAgent = txtUserAgent.Text
            If txtAccept.Text <> "" Then wr.RequestObj.Accept = txtAccept.Text

            Select Case True
                Case rbConnectionOther.Checked
                    wr.RequestObj.KeepAlive = False
                    If txtConnection.Text <> "" Then wr.RequestObj.Connection = txtConnection.Text

                Case rbConnectionClose.Checked
                    wr.RequestObj.KeepAlive = False

                Case rbConnectionKeepAlive.Checked
                    wr.RequestObj.KeepAlive = True
            End Select

            '- Add The Custom Headers
            Dim i As Integer
            For i = 0 To lvHeaders.Items.Count - 1
                wr.RequestObj.Headers.Add(lvHeaders.Items(i).Text, lvHeaders.Items(i).SubItems(1).Text)
            Next

            '- Setup The Proxy Info
            If chkUseProxy.Checked Then
                wr.RequestObj.Proxy = New System.Net.WebProxy(txtProxyAddress.Text, CInt(txtProxyPort.Text))
                If txtProxyDomain.Text <> "" Then
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text, txtProxyDomain.Text)
                Else
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text)
                End If
            End If

            '- Add The Data To Post
            If chkPost.Checked Then
                For i = 0 To lvPost.Items.Count - 1
                    wr.AddPostData(lvPost.Items(i).Text, lvPost.Items(i).SubItems(1).Text)
                Next
            End If

            '- Setup The Proxy Info
            If chkAuth.Checked Then
                wr.RequestObj.PreAuthenticate = True
                wr.RequestObj.Credentials = New Net.NetworkCredential(txtAuthUsername.Text, txtAuthPassword.Text)
            End If

            '- Setup For Resuming
            wr.DoResume = chkResume.Checked
            If wr.DoResume Then wr.ResumeData = txtResumeData.Text

            '- Make The Request
            wr.ProcessRequest()
        End If
    End Sub
0
 
LVL 19

Expert Comment

by:Desp
ID: 12235606
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Limit As Int32 = Convert.ToInt32(TextBox2.Text)
Dim i As Int32

For i = 1 To Limit
            TextBox1.Text = Limit.ToString()
Next

end sub
0
 

Author Comment

by:Aetia
ID: 12237096
Desp, look my previus post, how can I make this wr.ProcessRequest() to loop?

As far as I've tried it says I cant run it if previous call isn't ended...

Any solution for that?
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12239361
clsWebRequest I presume is a custom class since I couldn't find any documentation on it.

If so is there an event in it to call back to your app when it is finished?

If there isn't an event and you created the class then add one.

If you didn't create the class do you have access to modify it.

Anyway if you catch the the (ProcessRequestFinished or whatever) event you can then run the next loop since you know the first ProcessRequest is finished.

Your module might look like this. Except you will need to replace ProcessRequestFinished with whatever the actual event is called.

Dim LoopsLeft as int
Dim WR as clsWebRequest

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                             Handles Button1.Click
  LoopsLeft = TextBox1.text
  StartProcessRequest ()

End Sub

PrivateSub StartProcessRequest ()
        Dim hgf As Short
        For hgf = 1 To CShort(TextBox2.Text)
            MsgBox("Label13.Text = " & hgf)
            Label13.Text = hgf
        Next hgf

        wr = New clsWebRequest(txtRequest.Text)
        If Not wr.RequestObj Is Nothing Then
            '- Set The Status Callback Address
            wr.StatusCallBack = AddressOf ReqCallBack

            '- Use Smart Auto Redirection (Cookie Support)
            wr.UseSmartRedirection = True

            '- Set Up The Request Headers
            If txtReferer.Text <> "" Then wr.RequestObj.Referer = txtReferer.Text
            If txtUserAgent.Text <> "" Then wr.RequestObj.UserAgent = txtUserAgent.Text
            If txtAccept.Text <> "" Then wr.RequestObj.Accept = txtAccept.Text

            Select Case True
                Case rbConnectionOther.Checked
                    wr.RequestObj.KeepAlive = False
                    If txtConnection.Text <> "" Then wr.RequestObj.Connection = txtConnection.Text

                Case rbConnectionClose.Checked
                    wr.RequestObj.KeepAlive = False

                Case rbConnectionKeepAlive.Checked
                    wr.RequestObj.KeepAlive = True
            End Select

            '- Add The Custom Headers
            Dim i As Integer
            For i = 0 To lvHeaders.Items.Count - 1
                wr.RequestObj.Headers.Add(lvHeaders.Items(i).Text, lvHeaders.Items(i).SubItems(1).Text)
            Next

            '- Setup The Proxy Info
            If chkUseProxy.Checked Then
                wr.RequestObj.Proxy = New System.Net.WebProxy(txtProxyAddress.Text, CInt(txtProxyPort.Text))
                If txtProxyDomain.Text <> "" Then
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text, txtProxyDomain.Text)
                Else
                    wr.RequestObj.Proxy.Credentials = New System.Net.NetworkCredential(txtProxyUserName.Text, txtProxyPassword.Text)
                End If
            End If

            '- Add The Data To Post
            If chkPost.Checked Then
                For i = 0 To lvPost.Items.Count - 1
                    wr.AddPostData(lvPost.Items(i).Text, lvPost.Items(i).SubItems(1).Text)
                Next
            End If

            '- Setup The Proxy Info
            If chkAuth.Checked Then
                wr.RequestObj.PreAuthenticate = True
                wr.RequestObj.Credentials = New Net.NetworkCredential(txtAuthUsername.Text, txtAuthPassword.Text)
            End If

            '- Setup For Resuming
            wr.DoResume = chkResume.Checked
            If wr.DoResume Then wr.ResumeData = txtResumeData.Text

            '- Make The Request
            wr.ProcessRequest()
        End If
End sub
Private Function WR_ProcessRequestFinished () as long _
                                            Handles WR.ProcessRequestFinished
If LoopsLeft > 0 then
   StartProcessRequest
end if
LoopsLeft - 1



End Function
0
 

Author Comment

by:Aetia
ID: 12240011
I think I've found this what you call "ProcessRequestFinished":

Private Shared Sub ResponseCallback(ByVal AsyncResult As IAsyncResult)
        Dim ReqState As RequestState = CType(AsyncResult.AsyncState, RequestState)

        If bCancel Then
            ReqState.Request.Abort()
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTABORTED, 0, 0, 0)
            ReqThread.Abort()
            Exit Sub
        End If

        Try
            ReqState.Response = ReqState.Request.EndGetResponse(AsyncResult)
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_HEADERRECEIVED, 0, IIf(bResume, ReqState.Response.ContentLength + ReqState.lResumeStart, ReqState.Response.ContentLength), 0)

            '- Smart Redirection
            If bUseSmartRedirection Then
                Select Case ReqState.Response.StatusCode
                    Case HttpStatusCode.Found, HttpStatusCode.Moved, HttpStatusCode.MovedPermanently

                        '- Invoke The Status Callback
                        If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_HEADERRECEIVED, 0, ReqState.Response.ContentLength, 0)

                        '- Change URL To Absolute If Relative
                        Dim i As Integer
                        Dim sNewUrl As String = ReqState.Response.Headers.Item("Location")
                        If Not IsValidUrl(sNewUrl) Then
                            sNewUrl = ReqState.Response.ResponseUri.Scheme & ReqState.Response.ResponseUri.SchemeDelimiter & ReqState.Response.ResponseUri.Host
                            sNewUrl = sNewUrl & ReqState.Request.RequestUri.LocalPath
                            sNewUrl = sNewUrl & ReqState.Response.Headers.Item("Location")
                        End If

                        '- Create New Request With The New Address
                        Dim NewReq As HttpWebRequest = HttpWebRequest.Create(sNewUrl)
                        NewReq.Proxy = ReqState.Request.Proxy
                        NewReq.UserAgent = ReqState.Request.UserAgent
                        NewReq.PreAuthenticate = ReqState.Request.PreAuthenticate
                        NewReq.Credentials = ReqState.Request.Credentials
                        NewReq.Referer = ReqState.Request.RequestUri.ToString

                        '- Include Any Cookies Received
                        Dim sCookie() As String
                        sCookie = ReqState.Response.Headers.GetValues("Set-Cookie")
                        If Not sCookie Is Nothing Then
                            For i = 0 To UBound(sCookie)
                                NewReq.Headers.Add("Cookie: " & sCookie(i))
                            Next i
                        End If
                        Erase sCookie
                        ReqState.Request = NewReq

                        '- Request The New Address
                        If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTINGHEADER, 0, 0, 0)
                        AsyncResult = CType(ReqState.Request.BeginGetResponse(AddressOf ResponseCallback, ReqState), IAsyncResult)
                        Exit Sub
                End Select
            End If

            '- Setup OutputFile If Needed
            If sOutputFile <> "" Then
                If bResume Then
                    ReqState.ByteLen = ReqState.lResumeStart
                    ReqState.OutputFileStream = New FileStream(sOutputFile, FileMode.Append, FileAccess.Write, FileShare.None)
                Else
                    ReqState.OutputFileStream = New FileStream(sOutputFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)
                End If
                ReqState.Data.Append(sOutputFile)
            Else
                ReqState.OutputFileStream = Nothing
                If bResume Then
                    ReqState.Data.Append(sResumeData)
                    ReqState.ByteLen = sResumeData.Length
                End If
            End If

            ReqState.ResponseStream = ReqState.Response.GetResponseStream()
            Dim aResult As IAsyncResult = ReqState.ResponseStream.BeginRead(ReqState.Buffer, 0, BUFFER_SIZE, AddressOf ReadCallBack, ReqState)

        Catch we As WebException
            '- Make The Response Available For Analizing Afterwards
            ReqState.Response = we.Response

            Dim iErrNum As Integer = CType(we.Response, HttpWebResponse).StatusCode

            '- Notify Owner Of Failed Request
            If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.Invoke(ReqEvent.SE_REQUESTFAILED, 0, 0, iErrNum)

            '- End This Thread
            ReqThread.Abort()
        End Try
    End Sub
0
 
LVL 13

Expert Comment

by:Corey2
ID: 12240499
It seems that you have the class so you can modify it.

I don't think that is quite what I was looking for.

Since you can modify the class directly, at the top of the class declare an event

    Public Event ProcessRequestFinished ()

Look for a sub in the clsWebRequest class named ProcessRequest
Once you find this sub go to the end of the sub and for the last line put

    RaiseEvent ProcessRequestFinished()

this will start running the code in your module that handles this event.

Private Function WR_ProcessRequestFinished () as long _
                                            Handles WR.ProcessRequestFinished
   If LoopsLeft > 0 then
     StartProcessRequest
   end if
   LoopsLeft - 1

End Function

Then you should be golden.

Corey
0
 

Author Comment

by:Aetia
ID: 12241982
I am newbie and started learning vb.net. Everything you said is currently mishmash for me... What I think that you could download my solution an see it yourself.

Download it here:
http://www.dosch3d.ee/HttpWebRequest.rar

Maybe you could give me full parts of codes so I could get this thing working and later examine it...

What I want to develop of this thing would be "traffic bump" that would generate numerous hits to some webpage. That's why I need it to loop it set number of times.
0
 
LVL 13

Accepted Solution

by:
Corey2 earned 500 total points
ID: 12242808

Find this line
 '- Start The Request Thread

Insert this directly after it.

if not ReqThread is nothing then
      While ReqThread.IsAlive
            Thread.Sleep(100)
            'wait 100 milleseconds and check if the
                                'thread has finished.
      wend
end if

If you use the "for...next" loop and insert the code above it should run and when it gets to this point on each subsequent attempt it will wait for the earlier attempt to finish before continuing.

That will get you going, but I think you should read up on multithreading in the MSDN It would be best if your secondary thread was run from it's own class then you could rais events as I mentioned earlier when the thread finishes it is a much better practice IMHO.

Corey
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now