locked
datastream.write hangs for long time RRS feed

  • Question

  • I am sending notifications to smartphones, using code as below:

    -------------------------------

    Sub sendNotification(S as string)

                Dim request As WebRequest = WebRequest.Create("https://xx.yyy.io")
                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(S)
                request.Method = "POST"
                request.ContentType = "application/json"
                request.ContentLength = byteArray.Length
                Dim dataStream As Stream = request.GetRequestStream()
                dataStream.Write(byteArray, 0, byteArray.Length)

                dataStream.Close()
                dataStream.Dispose()
                byteArray = Nothing
                request = Nothing
        

    end sub

    ---------------------------

    S is a json-formatted string

    It works fine - notification reaches the smartphone.

    But when I call the sub several times eg 4 times in a minute, the third or the forth call hangs in the datastream.write for almost a minute.

    Can anyone please explain why - and maybe even what to do ?

    mvh  ravnø

    Tuesday, July 21, 2020 4:28 PM

All replies

  • dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Flush()
    dataStream.Close()
    Flush() should force data dump/write-out to the stream whether its buffer is full or not.  Otherwise it can stall waiting for more data, engineered with the idea in mind that it's more efficient to push as much data as possible all at once rather than just put constant traffic on the line.

    Before you can learn anything new you have to learn that there's stuff you don't know.

    Tuesday, July 21, 2020 4:59 PM
  • Sorry to say no difference - it still hangs at no. 3 notification.

    mvh  ravnø

    Tuesday, July 21, 2020 5:40 PM
  • Does it hang if you send the third and forth calls after restarting your program?

    Tuesday, July 21, 2020 6:24 PM
  • I have measured:

    notification #    time in seconds

    1.  1
    2.  1
    3.  75
    4.  1
    5.  1
    6.  60
    7.  1
    8.  6
    9.  60
    10.  1
    11.  3
    12.  65
    13.  1
    14.  1

    The same shows after restart or 15 minutes of pause

    Wednesday, July 22, 2020 6:09 PM
  • I have measured:

    notification #    time in seconds

    1.  1
    2.  1
    3.  75
    4.  1
    5.  1
    6.  60
    7.  1
    8.  6
    9.  60
    10.  1
    11.  3
    12.  65
    13.  1
    14.  1

    The same shows after restart or 15 minutes of pause

    Is that 3rd item always the same web address?  If so then the lag may just be that server.  You might try moving it to 1st place and seeing if it's still a lag monster - that would confirm that it's not your computer/code/network.

    Before you can learn anything new you have to learn that there's stuff you don't know.

    Wednesday, July 22, 2020 7:01 PM
  • They are all to the same server - and it has been tested by spamming it with 50 calls without problems.
    Thursday, July 23, 2020 6:43 AM
  • Hi Ravnø Data,

    Did you consider using Async in your code?

    Here are two documents you can refer to:

    1. Using Async for File Access (Visual Basic)
    2. Making Asynchronous Requests

    Hope them could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, July 23, 2020 9:15 AM
  • Yes - I already have a BackgroundWorker, that sends the notifications.

    I just wonder why the delay - and would like it to be fixed.

    mvh  ravnø

    Thursday, July 23, 2020 1:18 PM
  • Hi Ravnø Data,

    Thanks for your feedback.

    Since WebRequest is not recommended for new development, you can consider using HttpClient to post the request.

    HttpWebRequest to HttpClient post Request

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 24, 2020 2:27 AM
  • VS2017 Community looks like not having httpClient

    It suggests smtpClient or tcpClient - neither having eg .baseAddress ??

    Monday, July 27, 2020 4:49 PM
  • Hi Ravnø Data,

    >>VS2017 Community looks like not having httpClient

    After importing 'System.Net.Http', you can use 'HttpClient' in vs2017:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 28, 2020 6:06 AM
  • Imports System.Net.Http is marked as unnecessary - HttpClient is undefined ??

    Sunday, August 2, 2020 1:16 PM
  • Hi Ravnø Data,

    You may need to install following nuget package:

    Microsoft.Net.Http

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 3, 2020 5:57 AM
  • Hi Xingyu Zhao

    Now it works as wanted - one small detail:

    Private Async Function PostAsync(ByVal jsonString As String) As Task

            Dim content As New Net.Http.StringContent(jsonString, System.Text.Encoding.UTF8, "application/json")
            Dim response As Net.Http.HttpResponseMessage = Await client.PostAsync("", content)
         End Function

    I call the function:
    PostAsync(expoData)

    Here there is a warning:
    Warning BC42358 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the Await operator to the result of the call.

    How do I get rid of it ?

    mvh  ravnø


    • Edited by Ravnø Data Saturday, September 26, 2020 9:29 AM
    Thursday, September 24, 2020 9:13 AM