?
Solved

Repeat a command

Posted on 2004-10-06
16
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 19

Expert Comment

by:Fahad Mukhtar
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:Fahad Mukhtar
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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:Fahad Mukhtar
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
 

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:Fahad Mukhtar
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month14 days, 7 hours left to enroll

770 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