Philippe Renaud
asked on
VB.NET HttpWebRequest
Hello EE,
In my vb.net page, I do something like this after creating my HttpWebRequest :
My question is, by doing that, I saw that this code slows down ALOT my script. If I remove it just for fun and run, it's extremely fast.
So, in other words, is there a way to use "getRequestStream" or similar and yet still have the speed or its the only way ?
In my vb.net page, I do something like this after creating my HttpWebRequest :
Using dataStream = requestLoginToPage1.GetRequestStream()
dataStream.Write(bytes1, 0, bytes1.Length)
dataStream.Close()
End Using
My question is, by doing that, I saw that this code slows down ALOT my script. If I remove it just for fun and run, it's extremely fast.
So, in other words, is there a way to use "getRequestStream" or similar and yet still have the speed or its the only way ?
you should try the async version: https://msdn.microsoft.com/en-us/library/system.net.webrequest.getrequeststreamasync(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
ASKER
im trying to do with Await and lambda has i saw an example over the internet.
but what am i missing, not sure to fix the error saying :
Lambda expression cannot be convert to IO.Stream,not a delegate type
how would you write it ?
Using requestStream = New BinaryWriter(Function()
Await requestLoginToPage1.GetRequestStreamAsync()
End Function)
requestStream.Write(bytes1, 0, bytes1.Length)
End Using
but what am i missing, not sure to fix the error saying :
Lambda expression cannot be convert to IO.Stream,not a delegate type
how would you write it ?
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The error is because this is what you're passing to BinaryWriter:
...not this:
You're passing the labmda itself, not it's reasult. Just await the GetRequestStreamAsync directly:
GetRequestStreamAsync is already awaitable (because it returns Task(n)); you don't have to wrap it in a lambda.
Function()
...
End Function
...
End Function
...not this:
Await requestLoginToPage1.GetReq uestStream Async()
You're passing the labmda itself, not it's reasult. Just await the GetRequestStreamAsync directly:
Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
requestStream.Write(bytes1, 0, bytes1.Length)
End Using
GetRequestStreamAsync is already awaitable (because it returns Task(n)); you don't have to wrap it in a lambda.
ASKER
Kaufmed, hold on so you mean, I only have to write what you wrote and should be fine ?
if I only do this :
I have an error saying :
Await can only be used within an async lambda blablabla...
if I only do this :
Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
requestStream.Write(bytes1, 0, bytes1.Length)
End Using
I have an error saying :
Await can only be used within an async lambda blablabla...
OK, my mistake--kind of. You'd have to mark your outer method as Async (because you're using Await).
e.g.
But the implication of doing that is that you'd probably have to continue marking methods Async and Awaiting the results of such methods all the way up the chain. That's probably more work than you want for this.
What kind of project is this: Console? Library? Win Forms? ASP.NET?
e.g.
Async Sub YourMethod()
Using requestStream = New BinaryWriter(Await requestLoginToPage1.GetRequestStreamAsync())
requestStream.Write(bytes1, 0, bytes1.Length)
End Using
End Sub
But the implication of doing that is that you'd probably have to continue marking methods Async and Awaiting the results of such methods all the way up the chain. That's probably more work than you want for this.
What kind of project is this: Console? Library? Win Forms? ASP.NET?
ASKER
its vb.net win forms ....
i tried liek you said and i have no errors now... but my only problem is that... This USING block is inside a list.Forach(Async Sub(z) ...
and for some reason, it does not loop, it stops after only 1 value. (I have 36 values in my list) after index 0 it does not loop
but there are no error poping
you think its because of those Asyn calls ?
i tried liek you said and i have no errors now... but my only problem is that... This USING block is inside a list.Forach(Async Sub(z) ...
and for some reason, it does not loop, it stops after only 1 value. (I have 36 values in my list) after index 0 it does not loop
but there are no error poping
you think its because of those Asyn calls ?
ASKER
ok i found my problem
ASKER
im going to give points but, i did not really see any speed up by using Async ...
its really slow I find....
like almost a second per item in my list.
i dont have enough knowledge to know if its normal
its really slow I find....
like almost a second per item in my list.
i dont have enough knowledge to know if its normal
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
For you Kaufmed and Eric in case you know an answer :
https://www.experts-exchan ge.com/que stions/289 85931/Spee ding-up-a- Loop.html
https://www.experts-exchan