none
close form immediately RRS feed

  • Question

  • hi all

    when i use close to close the program it's not close immediately

    example

    Close() Using webClient = New Net.WebClient webClient.DownloadString("http://www.google.com/") End Using

    should i use : (exit sub) after close

    Friday, July 14, 2017 2:22 PM

Answers

  • when i use exit sub after close

    it will not downloading and close immediately

    should i use it?

    I don't know what you mean but look again at the code that I posted and follow the logic:

    There's a class-scoped boolean named "_stringDownloaded" which is initially set to false.

    When the form is closing (not closed yet) the code checks to see the state of that and if it's false then it will run the part of the code that downloads that string and saves it.

    It will then call "Close" in that branch of the conditional:

                If Not _stringDownloaded Then
    e.Cancel = True
    Const url As String = _ "http://www.fls-online.net/VBNet_Forum/ExampleText.txt" Dim s As String = GetTextFromURL(url) If Not String.IsNullOrWhiteSpace(s) Then Dim desktop As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim fi As New FileInfo(Path.Combine(desktop, "MyString.txt")) If fi.Exists Then fi.Delete() End If File.WriteAllText(fi.FullName, s) _stringDownloaded = True Close() End If

    So now it's been instructed to close but notice that that line just above it sets that class-scoped boolean to True first.

    This time through it will see that it's true and skip the download - and just close the form.

    Make sense?


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, July 14, 2017 4:43 PM ...forgot to set e.Cancel!
    • Marked as answer by zacVB Friday, July 14, 2017 4:57 PM
    Friday, July 14, 2017 4:02 PM

All replies

  • Hi

    Try     'End'

    NOTE: anything after End (or Close()) will not be reached.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, July 14, 2017 2:32 PM
    Friday, July 14, 2017 2:31 PM
  • hi all

    when i use close to close the program it's not close immediately

    example

    Close() Using webClient = New Net.WebClient webClient.DownloadString("http://www.google.com/") End Using

    should i use : (exit sub) after close


    DownloadString is a blocking call - that's why. You  can use the async method (.DownloadStringAsync), but I think you're better to verify that the string downloaded THEN close the form.

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, July 14, 2017 2:33 PM
  • Yes or Return

    Be aware that your code is in everything wrong. You uses the class name for the object while the webclient.Downloadstring downloads but that is it, it is stored nowhere.


    Success
    Cor

    Friday, July 14, 2017 2:36 PM
  • zac,

    A suggestion (this will work -- it's a small text file on my website):

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Imports System.Net
    Imports System.IO
    
    Public Class Form1
        Private _stringDownloaded As Boolean = False
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
            ' Initialization here (if applicable)
    
        End Sub
    
    
    
        Private Sub _
            Form1_FormClosing(sender As Object, _
                              e As System.Windows.Forms.FormClosingEventArgs) _
                              Handles Me.FormClosing
    
            If e.CloseReason = CloseReason.UserClosing Then
                If Not _stringDownloaded Then
    e.Cancel = True
    Const url As String = _ "http://www.fls-online.net/VBNet_Forum/ExampleText.txt" Dim s As String = GetTextFromURL(url) If Not String.IsNullOrWhiteSpace(s) Then Dim desktop As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim fi As New FileInfo(Path.Combine(desktop, "MyString.txt")) If fi.Exists Then fi.Delete() End If File.WriteAllText(fi.FullName, s) _stringDownloaded = True
    Close() End If End If End If End Sub Private Function _ GetTextFromURL(ByVal url As String, _ Optional ByVal timeoutMS As Integer = 500) As String Dim retVal As String = Nothing Try Dim request As WebRequest = WebRequest.Create(url) request.Timeout = timeoutMS Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() retVal = responseFromServer End Using End Using End Using Catch ex As System.Net.WebException ' Intentionally empty: Be sure to test ' the return value. Catch ex As Exception MessageBox.Show(String.Format("An error occurred:{0}{0}{1}", _ vbCrLf, ex.Message), _ "Exception Thrown", _ MessageBoxButtons.OK, _ MessageBoxIcon.Warning) End Try Return retVal End Function End Class


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Friday, July 14, 2017 4:42 PM ...forgot to set e.Cancel!
    Friday, July 14, 2017 2:46 PM
  • thank you guys so much

    the problem not just in webclient

    this code will thrown exception that file not exist

    but what i need is not perform codes after close if some condition is true

            Close()
            Dim s As New IO.FileStream("C:\NotExistFile.exe", IO.FileMode.Open, IO.FileAccess.Read)
            Dim a As New IO.BinaryReader(s)
            MsgBox(s.Length)
            s.Close()
            a.Close()

    without using if and else

    Friday, July 14, 2017 3:16 PM
  • thank you guys so much

    the problem not just in webclient

    this code will thrown exception that file not exist

    but what i need is not perform codes after close if some condition is true

            Close()
            Dim s As New IO.FileStream("C:\NotExistFile.exe", IO.FileMode.Open, IO.FileAccess.Read)
            Dim a As New IO.BinaryReader(s)
            MsgBox(s.Length)
            s.Close()
            a.Close()

    without using if and else

    No offense but none of that makes sense.

    Explain what your goal is and let's see if we can come up with a reasonable (and safe) solution.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, July 14, 2017 3:19 PM
  • example from your code

    part download

     Dim somecondition As Boolean = True
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If somecondition = True Then
                Close()
            End If
            If Not _stringDownloaded Then
                Const url As String = _
                    "http://www.fls-online.net/VBNet_Forum/ExampleText.txt"
    
                Dim s As String = GetTextFromURL(url)
    
                If Not String.IsNullOrWhiteSpace(s) Then
                    Dim desktop As String = _
                        Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    
                    Dim fi As New FileInfo(Path.Combine(desktop, "MyString.txt"))
    
                    If fi.Exists Then
                        fi.Delete()
                    End If
    
                    File.WriteAllText(fi.FullName, s)
                    _stringDownloaded = True
    
                End If
            End If
        End Sub

    whan click button and condition is true it's not close the form immediately


    Friday, July 14, 2017 3:34 PM
  • whan click button and condition is true it's not close the form immediately

    It can't close immediately; it's downloading the string.

    I'm making use of the fact that it's a blocking call which is what I suggested to start with.

    You can make the program look like it closed (use Hide()) but I think you're better to show your user that it's downloading the string and will close once that's that's done.

    *****

    If you look on your desktop, you'll see the text file that was downloaded and saved there ("MyString.txt").


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Friday, July 14, 2017 3:39 PM
  • when i use exit sub after close

    it will not downloading and close immediately

    should i use it?

    Friday, July 14, 2017 3:54 PM
  • when i use exit sub after close

    it will not downloading and close immediately

    should i use it?

    I don't know what you mean but look again at the code that I posted and follow the logic:

    There's a class-scoped boolean named "_stringDownloaded" which is initially set to false.

    When the form is closing (not closed yet) the code checks to see the state of that and if it's false then it will run the part of the code that downloads that string and saves it.

    It will then call "Close" in that branch of the conditional:

                If Not _stringDownloaded Then
    e.Cancel = True
    Const url As String = _ "http://www.fls-online.net/VBNet_Forum/ExampleText.txt" Dim s As String = GetTextFromURL(url) If Not String.IsNullOrWhiteSpace(s) Then Dim desktop As String = _ Environment.GetFolderPath(Environment.SpecialFolder.Desktop) Dim fi As New FileInfo(Path.Combine(desktop, "MyString.txt")) If fi.Exists Then fi.Delete() End If File.WriteAllText(fi.FullName, s) _stringDownloaded = True Close() End If

    So now it's been instructed to close but notice that that line just above it sets that class-scoped boolean to True first.

    This time through it will see that it's true and skip the download - and just close the form.

    Make sense?


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Friday, July 14, 2017 4:43 PM ...forgot to set e.Cancel!
    • Marked as answer by zacVB Friday, July 14, 2017 4:57 PM
    Friday, July 14, 2017 4:02 PM
  • thank you so much bro

    understand the problem :)


    • Edited by zacVB Friday, July 14, 2017 4:58 PM
    Friday, July 14, 2017 4:56 PM