Aetia
asked on
Repeat a command
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.
How can I do so that it would repeat this commad number of times defined in "textbox2", every time deleting previously writed information.
you can do this in a FOR loop with its upper bound set to the value in the textbox2
something close to this:
Dim Limit As Int32 = Convert.ToInt32(TextBox2.T ext)
Dim i As Int32
For i = 1 To Limit
TextBox1.Text = "The Value now is " + Limit.ToString()
Next
Dim Limit As Int32 = Convert.ToInt32(TextBox2.T
Dim i As Int32
For i = 1 To Limit
TextBox1.Text = "The Value now is " + Limit.ToString()
Next
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
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
ASKER
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?
How can I do it without msgbox? I meand it would coun times on textbox1 or say atleast after every repeat completed etc?
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
Dim i As Short
For i = 1 To CShort(TextBox2.Text)
TextBox1.Text = i
Next i
End Sub
did you try my code?
ASKER
dhaest, Dhaest I've tried it...
Cast from string "TextBox2" to type 'Short' is not valid.
Cast from string "TextBox2" to type 'Short' is not valid.
What value do you have in textbox2 ?
ASKER
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.T ext)
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.Chec ked
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).T ext, lvHeaders.Items(i).SubItem s(1).Text)
Next
'- Setup The Proxy Info
If chkUseProxy.Checked Then
wr.RequestObj.Proxy = New System.Net.WebProxy(txtPro xyAddress. Text, CInt(txtProxyPort.Text))
If txtProxyDomain.Text <> "" Then
wr.RequestObj.Proxy.Creden tials = New System.Net.NetworkCredenti al(txtProx yUserName. Text, txtProxyPassword.Text, txtProxyDomain.Text)
Else
wr.RequestObj.Proxy.Creden tials = New System.Net.NetworkCredenti al(txtProx yUserName. 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.Item s(i).Text, lvPost.Items(i).SubItems(1 ).Text)
Next
End If
'- Setup The Proxy Info
If chkAuth.Checked Then
wr.RequestObj.PreAuthentic ate = True
wr.RequestObj.Credentials = New Net.NetworkCredential(txtA uthUsernam e.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 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.T
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.Chec
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(
Next
'- Setup The Proxy Info
If chkUseProxy.Checked Then
wr.RequestObj.Proxy = New System.Net.WebProxy(txtPro
If txtProxyDomain.Text <> "" Then
wr.RequestObj.Proxy.Creden
Else
wr.RequestObj.Proxy.Creden
End If
End If
'- Add The Data To Post
If chkPost.Checked Then
For i = 0 To lvPost.Items.Count - 1
wr.AddPostData(lvPost.Item
Next
End If
'- Setup The Proxy Info
If chkAuth.Checked Then
wr.RequestObj.PreAuthentic
wr.RequestObj.Credentials = New Net.NetworkCredential(txtA
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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Limit As Int32 = Convert.ToInt32(TextBox2.T ext)
Dim i As Int32
For i = 1 To Limit
TextBox1.Text = Limit.ToString()
Next
end sub
Dim Limit As Int32 = Convert.ToInt32(TextBox2.T
Dim i As Int32
For i = 1 To Limit
TextBox1.Text = Limit.ToString()
Next
end sub
ASKER
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?
As far as I've tried it says I cant run it if previous call isn't ended...
Any solution for that?
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.T ext)
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.Chec ked
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).T ext, lvHeaders.Items(i).SubItem s(1).Text)
Next
'- Setup The Proxy Info
If chkUseProxy.Checked Then
wr.RequestObj.Proxy = New System.Net.WebProxy(txtPro xyAddress. Text, CInt(txtProxyPort.Text))
If txtProxyDomain.Text <> "" Then
wr.RequestObj.Proxy.Creden tials = New System.Net.NetworkCredenti al(txtProx yUserName. Text, txtProxyPassword.Text, txtProxyDomain.Text)
Else
wr.RequestObj.Proxy.Creden tials = New System.Net.NetworkCredenti al(txtProx yUserName. 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.Item s(i).Text, lvPost.Items(i).SubItems(1 ).Text)
Next
End If
'- Setup The Proxy Info
If chkAuth.Checked Then
wr.RequestObj.PreAuthentic ate = True
wr.RequestObj.Credentials = New Net.NetworkCredential(txtA uthUsernam e.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
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.T
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.Chec
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(
Next
'- Setup The Proxy Info
If chkUseProxy.Checked Then
wr.RequestObj.Proxy = New System.Net.WebProxy(txtPro
If txtProxyDomain.Text <> "" Then
wr.RequestObj.Proxy.Creden
Else
wr.RequestObj.Proxy.Creden
End If
End If
'- Add The Data To Post
If chkPost.Checked Then
For i = 0 To lvPost.Items.Count - 1
wr.AddPostData(lvPost.Item
Next
End If
'- Setup The Proxy Info
If chkAuth.Checked Then
wr.RequestObj.PreAuthentic
wr.RequestObj.Credentials = New Net.NetworkCredential(txtA
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
ASKER
I think I've found this what you call "ProcessRequestFinished":
Private Shared Sub ResponseCallback(ByVal AsyncResult As IAsyncResult)
Dim ReqState As RequestState = CType(AsyncResult.AsyncSta te, RequestState)
If bCancel Then
ReqState.Request.Abort()
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In voke(ReqEv ent.SE_REQ UESTABORTE D, 0, 0, 0)
ReqThread.Abort()
Exit Sub
End If
Try
ReqState.Response = ReqState.Request.EndGetRes ponse(Asyn cResult)
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In voke(ReqEv ent.SE_HEA DERRECEIVE D, 0, IIf(bResume, ReqState.Response.ContentL ength + ReqState.lResumeStart, ReqState.Response.ContentL ength), 0)
'- Smart Redirection
If bUseSmartRedirection Then
Select Case ReqState.Response.StatusCo de
Case HttpStatusCode.Found, HttpStatusCode.Moved, HttpStatusCode.MovedPerman ently
'- Invoke The Status Callback
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In voke(ReqEv ent.SE_HEA DERRECEIVE D, 0, ReqState.Response.ContentL ength, 0)
'- Change URL To Absolute If Relative
Dim i As Integer
Dim sNewUrl As String = ReqState.Response.Headers. Item("Loca tion")
If Not IsValidUrl(sNewUrl) Then
sNewUrl = ReqState.Response.Response Uri.Scheme & ReqState.Response.Response Uri.Scheme Delimiter & ReqState.Response.Response Uri.Host
sNewUrl = sNewUrl & ReqState.Request.RequestUr i.LocalPat h
sNewUrl = sNewUrl & ReqState.Response.Headers. Item("Loca tion")
End If
'- Create New Request With The New Address
Dim NewReq As HttpWebRequest = HttpWebRequest.Create(sNew Url)
NewReq.Proxy = ReqState.Request.Proxy
NewReq.UserAgent = ReqState.Request.UserAgent
NewReq.PreAuthenticate = ReqState.Request.PreAuthen ticate
NewReq.Credentials = ReqState.Request.Credentia ls
NewReq.Referer = ReqState.Request.RequestUr i.ToString
'- Include Any Cookies Received
Dim sCookie() As String
sCookie = ReqState.Response.Headers. GetValues( "Set-Cooki e")
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.In voke(ReqEv ent.SE_REQ UESTINGHEA DER, 0, 0, 0)
AsyncResult = CType(ReqState.Request.Beg inGetRespo nse(Addres sOf 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(sOutp utFile)
Else
ReqState.OutputFileStream = Nothing
If bResume Then
ReqState.Data.Append(sResu meData)
ReqState.ByteLen = sResumeData.Length
End If
End If
ReqState.ResponseStream = ReqState.Response.GetRespo nseStream( )
Dim aResult As IAsyncResult = ReqState.ResponseStream.Be ginRead(Re qState.Buf fer, 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).StatusCod e
'- Notify Owner Of Failed Request
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In voke(ReqEv ent.SE_REQ UESTFAILED , 0, 0, iErrNum)
'- End This Thread
ReqThread.Abort()
End Try
End Sub
Private Shared Sub ResponseCallback(ByVal AsyncResult As IAsyncResult)
Dim ReqState As RequestState = CType(AsyncResult.AsyncSta
If bCancel Then
ReqState.Request.Abort()
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In
ReqThread.Abort()
Exit Sub
End If
Try
ReqState.Response = ReqState.Request.EndGetRes
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In
'- Smart Redirection
If bUseSmartRedirection Then
Select Case ReqState.Response.StatusCo
Case HttpStatusCode.Found, HttpStatusCode.Moved, HttpStatusCode.MovedPerman
'- Invoke The Status Callback
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In
'- Change URL To Absolute If Relative
Dim i As Integer
Dim sNewUrl As String = ReqState.Response.Headers.
If Not IsValidUrl(sNewUrl) Then
sNewUrl = ReqState.Response.Response
sNewUrl = sNewUrl & ReqState.Request.RequestUr
sNewUrl = sNewUrl & ReqState.Response.Headers.
End If
'- Create New Request With The New Address
Dim NewReq As HttpWebRequest = HttpWebRequest.Create(sNew
NewReq.Proxy = ReqState.Request.Proxy
NewReq.UserAgent = ReqState.Request.UserAgent
NewReq.PreAuthenticate = ReqState.Request.PreAuthen
NewReq.Credentials = ReqState.Request.Credentia
NewReq.Referer = ReqState.Request.RequestUr
'- Include Any Cookies Received
Dim sCookie() As String
sCookie = ReqState.Response.Headers.
If Not sCookie Is Nothing Then
For i = 0 To UBound(sCookie)
NewReq.Headers.Add("Cookie
Next i
End If
Erase sCookie
ReqState.Request = NewReq
'- Request The New Address
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In
AsyncResult = CType(ReqState.Request.Beg
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(sOutp
Else
ReqState.OutputFileStream = Nothing
If bResume Then
ReqState.Data.Append(sResu
ReqState.ByteLen = sResumeData.Length
End If
End If
ReqState.ResponseStream = ReqState.Response.GetRespo
Dim aResult As IAsyncResult = ReqState.ResponseStream.Be
Catch we As WebException
'- Make The Response Available For Analizing Afterwards
ReqState.Response = we.Response
Dim iErrNum As Integer = CType(we.Response, HttpWebResponse).StatusCod
'- Notify Owner Of Failed Request
If Not ReqState.StatusCallback Is Nothing Then ReqState.StatusCallback.In
'- End This Thread
ReqThread.Abort()
End Try
End Sub
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
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
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.