Code works whenever network connection ended!

Herry Markowitz

Well-known member
Joined
Oct 22, 2015
Messages
46
Programming Experience
1-3
Hi,
I got following code from here;
https://msdn.microsoft.com/library/...workchange(v=vs.80)?cs-save-lang=1&cs-lang=vb
Following code works like a charm.
VB.NET:
Imports System
Imports System.Net
Imports System.Net.NetworkInformation
Public Class NetworkingExample
    Public Shared Sub Main()
        AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
[COLOR=#ff0000]        MsgBox("Listening for address changes. Press OK to exit")[/COLOR]
    End Sub


    Shared Sub AddressChangedCallback(sender As Object, e As EventArgs)
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim n As NetworkInterface
        For Each n In adapters
            MsgBox("Internet connection is ended!")
            End
        Next n
    End Sub
End Class

Question: How to eliminate red line?
 
Last edited:
By selecting it in the code editor and pressing the Delete key. I'm guessing that there's more to your question than meets the eye but we only know what you tell us so I suggest that you tell us everything that's relevant. Are you maybe saying, without actually saying, that this is a Console app and, without that line, the application simply exits without ever doing anything useful?
 
1- I want Form Application solution.
So, again, are you saying, without actually saying, that this is a Console app? Please don't make us guess. Provide a FULL and CLEAR explanation of the problem. If you have a Console app and you want to make a Windows Forms app then say EXACTLY that.
2- I need to replace MsgBox line with something else because I dont want to see MsgBox.
So get rid of that line then. What's the problem?
 
1- I have a Console app and I want to make a Windows Forms app.
2- If I get rid of that line code doesnt work!
Any support?
 
2- If I get rid of that line code doesnt work!

Can you be more specific than "doesn't work"? That line is there in the Console app because, without it, the Main method would complete and the app would exit. If you've created a Windows Forms app then you're not touching the Main method and the app won't exit until you close the startup form, so there's no reason to have that message box. Please tell me that you're not touching the Main method.
 
1- I run following code.
2- Then MsgBox appear "Listening for address changes. Press OK to exit"
3- Then I ended internet connection manually.
4- Then MsgBox appear "Internet connection is ended!"
So everything is okey with this code.
VB.NET:
[COLOR=#333333]Imports System[/COLOR]Imports System.Net
Imports System.Net.NetworkInformation
Public Class NetworkingExample
    Public Shared Sub Main()
        AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback
[COLOR=#ff0000]        MsgBox("Listening for address changes. Press OK to exit")[/COLOR]
    End Sub


    Shared Sub AddressChangedCallback(sender As Object, e As EventArgs)
        Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        Dim n As NetworkInterface
        For Each n In adapters
            MsgBox("Internet connection is ended!")
            End
        Next n
    End Sub [COLOR=#333333]End Class[/COLOR]

My question: Is there any other way without using following line?
MsgBox("Listening for address changes. Press OK to exit")
 
If you want a Windows Forms application then DON'T run that code. Create a Windows Forms application project and then put the code in the correct place for that type of application. DO NOT touch the Main method unless you have a specific reason to do so, which you don't. Where would you normally put code to run at startup in a Windows Forms app? Either the Load event handler of the startup form or, probably better, the Startup event handler of the application. That's exactly what you should be doing here too. If you really want that code in a separate class then by all means put it in a separate class but DO NOT put a Main method in that class. Put the relevant code into a regular method and call that method for one of the usual places.
 
Plenty of ways, assuming that youre trying to find a way to stop your program from exiting immediately after it starts

Make it a windows forms app, paste this code into the class file for the main form:

VB.NET:
Imports System
Imports System.Net
Imports System.Net.NetworkInformation

Public Class MainForm

    Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
        AddHandler NetworkChange.NetworkAddressChanged, AddressOf AddressChangedCallback

    End Sub

    Shared Sub AddressChangedCallback(sender As Object, e As EventArgs)
        'you don't need to get all the adapters unless youre going to do something with them. showing a message and quitting the app is not "doing something with them" 
        'Dim adapters As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
        'there is no need to declare this variable
        'Dim n As NetworkInterface
        'there is no need to repeat this message once for every network adapter installed in the machine
        'For Each n In adapters
            MsgBox("Internet connection is ended!")
            'do not use End
            'End
        'Next n
    End Sub
End Class

If you want to keep it a console app, call Console.In.ReadLine() instead of msgbox.show
 
Hi cjard,
Thanks a lot. Solved.

I have another question regarding AddHandler Statement.

My vbnet application runs 30 minutes to complete.
I need a code which close notepad.exe if user try to open notepad.exe during 30 minutes (during my application is running.).

So, I am trying to find a way to prevent opening notepad.exe
during my application is running.

Any idea?



Here is a link for AddHandler Statement.
https://msdn.microsoft.com/tr-tr/library/7taxzxka(v=vs.100).aspx
 
Last edited:
Back
Top