• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

vb.net code transferred to a separate class..

Joined
Jun 5, 2007
Messages
2,010 (0.52/day)
Likes
279
Location
127.0.0.1
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F7b
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 - 16 - 16 - 30 1.45v
Video Card(s) Gigabyte Aorus GTX 1080
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 3tb WD Blue
Display(s) Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633 Gaming Headset
Power Supply Corsair HX750i
Mouse Logitech Proteus Core G502 | Logitech G602 Wireless Gaming
Keyboard Corsair K70 Cherry Red
Software Windows 10 Pro x64
#1
Code:
Sub Go()
        Dim p As New Process
        p.StartInfo.FileName = "ping.exe"
        p.StartInfo.Arguments = "www.bbc.co.uk -t"
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.CreateNoWindow = True
        AddHandler p.OutputDataReceived, AddressOf bleh
        p.Start()
        p.BeginOutputReadLine()
    End Sub

    Sub bleh(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        UpdateTextBox(e.Data)
    End Sub
    Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
    Private Sub UpdateTextBox(ByVal Tex As String)
        If Me.InvokeRequired Then
            Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
            Dim args As Object() = {Tex}
            Me.Invoke(del, args)
        Else
            Form1.tb.Text &= Tex & Environment.NewLine
        End If
    End Sub
This code works well when it's in the forms main code, but when I transfer it to a separate class file it doesn't work. Specifically this set of code The update/output.
Code:
 Private Sub UpdateTextBox(ByVal Tex As String)
        If Me.InvokeRequired Then
            Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
            Dim args As Object() = {Tex}
            Me.Invoke(del, args)
        Else
            Form1.tb.Text &= Tex & Environment.NewLine
        End If
    End Sub
Now when I change it to Me into form1 it compiles ok but the displayed text from ping does not show. And if I used "Me" I get this errors:

Error 1 'InvokeRequired' is not a member of 'txtbox_click_pinger.Class1'.
Error 2 'Invoke' is not a member of 'txtbox_click_pinger.Class1'.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,024 (6.21/day)
Likes
10,132
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#2
Code:
Public Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
Public Class Form1
    Private _Process As Process
    Public Sub New()
        InitializeComponent()

        _Process = New Process
        _Process.StartInfo.FileName = "ping.exe"
        _Process.StartInfo.Arguments = "www.bbc.co.uk -t"
        _Process.StartInfo.UseShellExecute = False
        _Process.StartInfo.RedirectStandardOutput = True
        _Process.StartInfo.CreateNoWindow = True
        AddHandler _Process.OutputDataReceived, AddressOf UpdateTextBox
        _Process.Start()
        _Process.BeginOutputReadLine()
    End Sub
    Private Sub UpdateTextBox(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Try
            If Me.InvokeRequired() Then
                ' Prevent cross-thread reference error.
                Me.Invoke(New DataReceivedEventHandler(AddressOf UpdateTextBox), New Object() {sender, e})
            Else
                tb.Text &= e.Data & Environment.NewLine
            End If
        Catch ex As Exception
            ' This will catch any errors generated during termination.
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        ' Make sure to stop ping.exe or else it will run indefinitely.
        If (Not _Process Is Nothing) Then _Process.Kill()
    End Sub
End Class
You are basically overcomplicated things by making your own delegate instead of using the delegate that already exists for the event.
 
Joined
Jun 5, 2007
Messages
2,010 (0.52/day)
Likes
279
Location
127.0.0.1
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F7b
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 - 16 - 16 - 30 1.45v
Video Card(s) Gigabyte Aorus GTX 1080
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 3tb WD Blue
Display(s) Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633 Gaming Headset
Power Supply Corsair HX750i
Mouse Logitech Proteus Core G502 | Logitech G602 Wireless Gaming
Keyboard Corsair K70 Cherry Red
Software Windows 10 Pro x64
#3
That seems to have done the trick... I've made it as form2.. So form1 only acts as a multifunction launcher of sorts. Btw is it possible to make a protection code so that it would Only allow 1 instance of form2 running?

Thanks man! You've really guided me alot in the process of vb learning.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,024 (6.21/day)
Likes
10,132
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#4
If you had only one place holder for Form2 and always used that placeholder, only once instance of Form2 could exist.

Dim child As Form2

You could also just use .Show and .Hide on that form so you only create a new instance of it once.
 
Joined
Jun 5, 2007
Messages
2,010 (0.52/day)
Likes
279
Location
127.0.0.1
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F7b
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 - 16 - 16 - 30 1.45v
Video Card(s) Gigabyte Aorus GTX 1080
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 3tb WD Blue
Display(s) Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633 Gaming Headset
Power Supply Corsair HX750i
Mouse Logitech Proteus Core G502 | Logitech G602 Wireless Gaming
Keyboard Corsair K70 Cherry Red
Software Windows 10 Pro x64
#5
If you had only one place holder for Form2 and always used that placeholder, only once instance of Form2 could exist.

Dim child As Form2

You could also just use .Show and .Hide on that form so you only create a new instance of it once.
Ok here's what is happening now, just want to make sure I understand what you've said..

When I click ping button on form1... It shows form2 and initiates it. I've put a button on form2 with "Hide" (form2.hide). So it's still running in the background right? Now form1 has the "ping" and "show form2" buttons...

"Show Form2" button is = form2.show()

uhm I've confused my self lol... That too is the command for "Ping" button.
Is there another way to code this so THE ping command wouldn't actually start off the bat when form2 is shown?

Sorry a bit confused.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,024 (6.21/day)
Likes
10,132
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#6
Yes, the only way the Form gets killed for good is if you call .Close() or reallocate the memory (e.g. child = Nothing).

Just remember that if you are going to run an infinite loop on ping.exe you should start/stop ping.exe when the VisbleChanged event is raised.
 
Joined
Jun 5, 2007
Messages
2,010 (0.52/day)
Likes
279
Location
127.0.0.1
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F7b
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 - 16 - 16 - 30 1.45v
Video Card(s) Gigabyte Aorus GTX 1080
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 3tb WD Blue
Display(s) Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633 Gaming Headset
Power Supply Corsair HX750i
Mouse Logitech Proteus Core G502 | Logitech G602 Wireless Gaming
Keyboard Corsair K70 Cherry Red
Software Windows 10 Pro x64
#7
Have made it work to make use a single instance by doing this:
Code:
 Public Sub New()
        [B][COLOR="Red"]MyBase.New()[/COLOR][/B]
        InitializeComponent()
        _p = New Process
        _p.StartInfo.FileName = "ping.exe"
        _p.StartInfo.Arguments = "www.bbc.co.uk -t"
        _p.StartInfo.UseShellExecute = False
        _p.StartInfo.RedirectStandardOutput = True
        _p.StartInfo.CreateNoWindow = True
        AddHandler _p.OutputDataReceived, AddressOf bleh
        _p.Start()
        _p.BeginOutputReadLine()
    End Sub
        InitializeComponent()
        _p = New Process
        _p.StartInfo.FileName = "ping.exe"
        _p.StartInfo.Arguments = "www.bbc.co.uk -t"
        _p.StartInfo.UseShellExecute = False
        _p.StartInfo.RedirectStandardOutput = True
        _p.StartInfo.CreateNoWindow = True
        AddHandler _p.OutputDataReceived, AddressOf bleh
        _p.Start()
        _p.BeginOutputReadLine()
    End Sub
And then added this to form 2 along with the code above:
Code:
  Private Shared _Instance As Form2 = Nothing
    Public Shared Function Instance() As Form2
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New Form2
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
Code for button initiate ping on form1 is:
Code:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MyForm2 = Form2.Instance
        MyForm2.Show()
    End Sub
^still gonna edit this further to be a 1 click start/stop button with error catching (try/catch)

Seems working perfectly now.. Also have made a show/hide check box with a try/catch combination so when there is no presence of the form2 it would not crash..

Thank you!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,024 (6.21/day)
Likes
10,132
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#8
MyBase.New() is a repetition of Public Sub New(). You'd only have to use MyBase.New if you were using a constructor other than New().


MyBase.New(), in a Form, is the same as calling New Form().
 
Joined
Jun 5, 2007
Messages
2,010 (0.52/day)
Likes
279
Location
127.0.0.1
System Name Zangief (Reborn)
Processor AMD Ryzen R7 1700X @ 3.825ghz , 1.35v
Motherboard Gigabyte GA-AX370 Gaming K7 Rev 1.0 BIOS F7b
Cooling Noctua NH-D15 Push / Push Config | 2x ML120 | 2x Phanteks 120 mm
Memory 2x8GB G.Skill Trident Z @ 3200mhz cl 16 - 16 - 16 - 30 1.45v
Video Card(s) Gigabyte Aorus GTX 1080
Storage 250 GB Samsung Evo 850 / 1tb WD Black / 3tb WD Blue
Display(s) Asus VX239H 23" AH-IPS Led
Case Phanteks Enthoo Pro M TG
Audio Device(s) On Board Realtek HD / Logitech G633 Gaming Headset
Power Supply Corsair HX750i
Mouse Logitech Proteus Core G502 | Logitech G602 Wireless Gaming
Keyboard Corsair K70 Cherry Red
Software Windows 10 Pro x64
#9
MyBase.New() is a repetition of Public Sub New(). You'd only have to use MyBase.New if you were using a constructor other than New().


MyBase.New(), in a Form, is the same as calling New Form().
Corrected and noted, works whichever is used. Thanks!
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
21,024 (6.21/day)
Likes
10,132
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#10
Ehm, my point is it doesn't need it--it is implied. It is only required if you are doing inheritance with a non-default base constructor. For example, if you have two classes:

Code:
Public Class Vehicle
  Public Name As String

  Public Sub New()
  End Sub
  Public Sub New(ByVal vehiclename As String)
  End Sub
End Class

Public Class Car Extends Vehicle
  Public Sub New()
  End Sub
End Class
As is, New Car would use New Vehicle(); however, if we changed Car to...
Code:
Public Class Car Extends Vehicle
  Public Sub New()
    MyBase.New("Mustang")
  End Sub
End Class
It would call New Vehicle("Mustang"). That's not the best example but the point is that MyBase is really only useful if you are dealing with inheritance.
 
Last edited: