none
Not wait for process to finish. RRS feed

  • Question

  • Im my windows forms program I send notification to smartphones.

    Heres a bit of the code:

                Dim byteArray As Byte() = Encoding.UTF8.GetBytes(expoData)
                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

    Sometimes the server has quite a long response-time - up to one minute. This causes the program to 'Wait'

    Is there a way to 'not wait for the server to respond' but let the program continue ?

    And when the server responds the close, dispose etc. is executed

    Friday, January 31, 2020 4:49 PM

Answers

  • Hi

    You could use a BackGroundWorker which would execute code and would keep the UI responsive.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, January 31, 2020 4:58 PM
    • Proposed as answer by Castorix31 Friday, January 31, 2020 5:03 PM
    • Marked as answer by Ravnø Data Monday, October 12, 2020 8:13 AM
    Friday, January 31, 2020 4:58 PM
  • The backgroundWorker solved the problem

    Thanks

    mvh  ravnø

    • Marked as answer by Ravnø Data Tuesday, July 21, 2020 4:31 PM
    Tuesday, July 21, 2020 4:31 PM

All replies

  • Hi

    You could use a BackGroundWorker which would execute code and would keep the UI responsive.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, January 31, 2020 4:58 PM
    • Proposed as answer by Castorix31 Friday, January 31, 2020 5:03 PM
    • Marked as answer by Ravnø Data Monday, October 12, 2020 8:13 AM
    Friday, January 31, 2020 4:58 PM
  • You can place your code into the "while loop" below and keep the Await Task.Delay(1000), not 1000 means one second, make it say a half second would be 500 and you could go down to a smaller delay too.

    In this case to do a mock up the loop runs until a CheckBox.Check = True, in your case you would test for instance  when "request" is Nothing.

    Private Async Function RunAndRun() As Task(Of Boolean)
    
        While CheckBox1.Checked <> True
            Await Task.Delay(1000)
        End While
    
        Return True
    
    End Function

    Calling it for demo purposes

    Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim result = Await RunAndRun()
    End Sub
    The app remains responsive while the code runs to completion. If there is a chance the code needs to be cancelled when not in a mockup a CancellationToken can be used.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, January 31, 2020 5:24 PM
    Moderator
  • The backgroundWorker solved the problem

    Thanks

    mvh  ravnø

    • Marked as answer by Ravnø Data Tuesday, July 21, 2020 4:31 PM
    Tuesday, July 21, 2020 4:31 PM
  • The backgroundWorker solved the problem

    Thanks

    mvh  ravnø

    Since Les's reply is what pointed you in the right direction it seems prudent for that reply to be marked as an answer too.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 21, 2020 5:34 PM
    Moderator