I have the set of code writen in vb 2010 to reformat a text file of ip address and give me ipsec command to block the ipaddress to my server. It takes a long time when I'm doing 1000+ Ip Address to block a country. This might not be able to be fixed. It hangs the program tell its done. But still works good just take forerver.

Function Build() As String

        Dim TextLine As String
        Dim Display As String
        Dim ipaddress As String
        Dim subnet As String
        Static p1 As String = ("netsh ipsec static add filter filterlist=")
        Dim p2 As String = TextBox2.Text
        Static P3 As String = (" srcaddr=")
        Static p4 As String = (" srcmask=")
        Static p5 As String = (" dstaddr=me description=")
        Dim p6 As String = TextBox3.Text
        Static p7 As String = (" protocol=any srcport=0 dstport=0")
        Static n1 As String = ("#Run This Batch in Command Terimal For Windows 2003 Only")




        Try

            For Each TextLine In arraylist

                ' Dim tmp() As String = Split(TextLine)
                Dim split As String() = TextLine.Split(New [Char]() {"/"}) 
                ipaddress = split(0)
                subnet = split(1)
                If subnet.Contains("32") Then
                    subnet = subnet.Replace("32", "255.255.255.255")
                ElseIf subnet.Contains("31") Then
                    subnet = subnet.Replace("31", "255.255.255.254")
                ElseIf subnet.Contains("30") Then
                    subnet = subnet.Replace("30", "255.255.255.252")
                ElseIf subnet.Contains("29") Then
                    subnet = subnet.Replace("29", "255.255.255.248")
                ElseIf subnet.Contains("28") Then
                    subnet = subnet.Replace("28", "255.255.255.240")
                ElseIf subnet.Contains("27") Then
                    subnet = subnet.Replace("27", "255.255.255.224")
                ElseIf subnet.Contains("26") Then
                    subnet = subnet.Replace("26", "255.255.255.192")
                ElseIf subnet.Contains("25") Then
                    subnet = subnet.Replace("25", "255.255.255.0")
                ElseIf subnet.Contains("24") Then
                    subnet = subnet.Replace("24", "255.255.255.0")
                ElseIf subnet.Contains("23") Then
                    subnet = subnet.Replace("23", "255.255.254.0")
                ElseIf subnet.Contains("22") Then
                    subnet = subnet.Replace("22", "255.255.252.0")
                ElseIf subnet.Contains("21") Then
                    subnet = subnet.Replace("21", "255.255.248.0")
                ElseIf subnet.Contains("20") Then
                    subnet = subnet.Replace("20", "255.255.240.0")
                ElseIf subnet.Contains("19") Then
                    subnet = subnet.Replace("19", "255.255.224.0")
                ElseIf subnet.Contains("18") Then
                    subnet = subnet.Replace("18", "255.255.192.0")
                ElseIf subnet.Contains("17") Then
                    subnet = subnet.Replace("17", "255.255.128.0")
                ElseIf subnet.Contains("16") Then
                    subnet = subnet.Replace("16", "255.255.0.0")
                ElseIf subnet.Contains("15") Then
                    subnet = subnet.Replace("15", "255.254.0.0")
                ElseIf subnet.Contains("14") Then
                    subnet = subnet.Replace("14", "255.252.0.0")
                ElseIf subnet.Contains("13") Then
                    subnet = subnet.Replace("13", "255.248.0.0")
                ElseIf subnet.Contains("12") Then
                    subnet = subnet.Replace("12", "255.240.0.0")
                ElseIf subnet.Contains("11") Then
                    subnet = subnet.Replace("11", "255.224.0.0")
                ElseIf subnet.Contains("10") Then
                    subnet = subnet.Replace("10", "255.192.0.0")
                ElseIf subnet.Contains("9") Then
                    subnet = subnet.Replace("9", "255.128.0.0")
                ElseIf subnet.Contains("8") Then
                    subnet = subnet.Replace("8", "255.0.0.0")
                End If

                Display = Display & p1 & p2 & P3 & ipaddress & p4 & subnet & p5 & p6 & p7 & vbNewLine  ' formats the data to my liking for viewing
                TextBox1.Text = n1 & vbNewLine & Display                                                                ' puts data in text box
                btn2003.Enabled = False

            Next
            arraylist.Clear()
            arraylist.TrimToSize()
        Catch ex As Exception

            MessageBox.Show(ex.Message, "Invalid Format")
        End Try
    End Function

Recommended Answers

All 4 Replies

Two things standout as big performance hits:
1) string concatenation: This is very slow and gets worse as the length of the string grows. Your "Display" variable gets longer as the loop progresses.

2) unnecessary assignment statements in the loop: The big one is assigning TextBox1.Text. Since you have not issued a TextBox1.Refresh command after the assignment, I assume that you can wait until the loop finishes to view the results. The minor assigment is the "btn2003.Ennabled"

No guaranty of major performance improvement, but I reccommend that you use a System.Text.StringBuilder for "Display" and modify the logic as follows:

        Try

            Dim Display As New System.Text.StringBuilder(100) 'adjust this initial size (100) to what makes sense
            textbox1.Text = n1 & vbNewLine 'initialize the textbox's text
            btn2003.Enabled = False
            For Each TextLine In ArrayList

                ' Dim tmp() As String = Split(TextLine)
                Dim split As String() = TextLine.Split(New [Char]() {"/"c})
                ipaddress = split(0)
                subnet = split(1)
                If subnet.Contains("32") Then
                    subnet = subnet.Replace("32", "255.255.255.255")
                    .
                    . the rest of the "if-then" block
                    .
               Display.Length = 0 ' clears the stringbuilder
               With Display
                  .Append(p1)
                  .Append(p2)
                  .Append(P3)
                  .Append(ipaddress)
                  .Append(p4)
                  .Append(subnet)
                  .Append(p5)
                  .Append(p6)
                  .AppendLine(p7)

               End With

                'let the textbox append the text, this eliminates a couple to string copys that can be quite long
                textbox1.AppendText(Display.ToString)

            Next
            btn2003.Enabled = True  'If this is the correct spot        

Also, what does this function return?

The Function Returns -
netsh ipsec static add filter filterlist="Blocked IP List" srcaddr=217.114.221.0 srcmask=255.255.255.0 dstaddr=me description=" " protocol=any srcport=0 dstport=0

Thanks alot it was alot faster. Made a world of difference.

I just realized that I made mistake in my first response to you. The textbox is updating as you append text in your loop. These updates are very resource intensive. If you can live with updating every "X" lines, then this modifcation will give you even better performance. The larger "X" is (less repainting the textbox) the faster your code will proceed.

Dim counter as Int32 = 0
TextBox1.AllowRedraw(False)
For Each TextLine In ArrayList

... Your Code


If counter Mod 5 = 0 Then 'update every 5 lines process
   TextBox1.AllowRedraw()
   TextBox1.AllowRedraw(False)
End If

Next

TextBox1.AllowRedraw() ' make sure to re-enable redraw

Where the AllowRedraw Method is an extension method defined as:

Module MyExtensions
   Private Const WM_SETREDRAW As Int32 = &HB
   <System.Runtime.CompilerServices.Extension()> _
   Public Sub AllowRedraw(ByVal cntrl As Control, Optional ByVal allow As Boolean = True)

      If allow Then
         SendMessage(cntrl.Handle, WM_SETREDRAW, New IntPtr(1), IntPtr.Zero)
         cntrl.Refresh()
      Else
         SendMessage(cntrl.Handle, WM_SETREDRAW, IntPtr.Zero, IntPtr.Zero)
      End If
   End Sub

   <Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
   Private Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As UInt32
   End Function
End Module

Just add this module to your project. It's one of my favorite tricks to speed things up.

I tired you secound post worked slower. the first method was alot fast I also added a background worker that fixed the gui hanging. I learned alot form you and thanks for your time. When i took vb.net classes in college they never went over background worker. I really wished they would have went over them.

Global at the top of the form

Dim WithEvents worker As New BackgroundWorker

Use this to make the background worker working. Its lazy coding but it works for a person program.

Private Sub frmmain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim TempPath As String = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\Temp"
        If My.Computer.FileSystem.FileExists(TempPath & "\TempText.txt") Then
            My.Computer.FileSystem.DeleteFile(TempPath & "\TempText.txt")
        End If
        System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False


        btn2008.Enabled = False
        btn2003.Enabled = False
    End Sub

This is the worker with you First reply

 Private Sub worker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles worker.DoWork

        Dim TextLine As String

        Dim ipaddress As String
        Dim subnet As String
        Static p1 As String = ("netsh ipsec static add filter filterlist=")
        Dim p2 As String = TextBox2.Text
        Static P3 As String = (" srcaddr=")
        Static p4 As String = (" srcmask=")
        Static p5 As String = (" dstaddr=me description=")
        Dim p6 As String = TextBox3.Text
        Static p7 As String = (" protocol=any srcport=0 dstport=0")
        Static n1 As String = ("#Run This Batch in Command Terimal For Windows 2003 Only")
        Dim i As Integer




        Try

            TextBox1.Text = n1 & vbNewLine 'initialize the textbox's text
            btn2003.Enabled = False
            Dim Display As New StringBuilder(1000) 'adjust this initial size (100) to what makes sense
            For Each TextLine In arraylist


                Dim split As String() = TextLine.Split(New [Char]() {"/"})  ' Splits data my using "/" as a marker ignores the char.
                ipaddress = split(0)
                subnet = split(1)
                If subnet.Contains("32") Then
                    subnet = subnet.Replace("32", "255.255.255.255")
                ElseIf subnet.Contains("31") Then
                    subnet = subnet.Replace("31", "255.255.255.254")
                ElseIf subnet.Contains("30") Then
                    subnet = subnet.Replace("30", "255.255.255.252")
                ElseIf subnet.Contains("29") Then
                    subnet = subnet.Replace("29", "255.255.255.248")
                ElseIf subnet.Contains("28") Then
                    subnet = subnet.Replace("28", "255.255.255.240")
                ElseIf subnet.Contains("27") Then
                    subnet = subnet.Replace("27", "255.255.255.224")
                ElseIf subnet.Contains("26") Then
                    subnet = subnet.Replace("26", "255.255.255.192")
                ElseIf subnet.Contains("25") Then
                    subnet = subnet.Replace("25", "255.255.255.0")
                ElseIf subnet.Contains("24") Then
                    subnet = subnet.Replace("24", "255.255.255.0")
                ElseIf subnet.Contains("23") Then
                    subnet = subnet.Replace("23", "255.255.254.0")
                ElseIf subnet.Contains("22") Then
                    subnet = subnet.Replace("22", "255.255.252.0")
                ElseIf subnet.Contains("21") Then
                    subnet = subnet.Replace("21", "255.255.248.0")
                ElseIf subnet.Contains("20") Then
                    subnet = subnet.Replace("20", "255.255.240.0")
                ElseIf subnet.Contains("19") Then
                    subnet = subnet.Replace("19", "255.255.224.0")
                ElseIf subnet.Contains("18") Then
                    subnet = subnet.Replace("18", "255.255.192.0")
                ElseIf subnet.Contains("17") Then
                    subnet = subnet.Replace("17", "255.255.128.0")
                ElseIf subnet.Contains("16") Then
                    subnet = subnet.Replace("16", "255.255.0.0")
                ElseIf subnet.Contains("15") Then
                    subnet = subnet.Replace("15", "255.254.0.0")
                ElseIf subnet.Contains("14") Then
                    subnet = subnet.Replace("14", "255.252.0.0")
                ElseIf subnet.Contains("13") Then
                    subnet = subnet.Replace("13", "255.248.0.0")
                ElseIf subnet.Contains("12") Then
                    subnet = subnet.Replace("12", "255.240.0.0")
                ElseIf subnet.Contains("11") Then
                    subnet = subnet.Replace("11", "255.224.0.0")
                ElseIf subnet.Contains("10") Then
                    subnet = subnet.Replace("10", "255.192.0.0")
                ElseIf subnet.Contains("9") Then
                    subnet = subnet.Replace("9", "255.128.0.0")
                ElseIf subnet.Contains("8") Then
                    subnet = subnet.Replace("8", "255.0.0.0")
                End If


                'Threading.Thread.Sleep(10)

                Display.Length = 0 ' clears the stringbuilder
                With Display
                    .Append(p1)
                    .Append(p2)
                    .Append(P3)
                    .Append(ipaddress)
                    .Append(p4)
                    .Append(subnet)
                    .Append(p5)
                    .Append(p6)
                    .AppendLine(p7)

                End With




                TextBox1.AppendText(Display.ToString)
                ' ProgressBar1.PerformStep()



                'write1.WriteLine(Display.ToString)

                '  Display = Display & p1 & p2 & P3 & ipaddress & p4 & subnet & p5 & p6 & p7 & vbNewLine  ' formats the data to my liking for viewing
                'TextBox1.Text = n1 & vbNewLine & Display                                                                ' puts data in text box
                ' btn2003.Enabled = False

            Next
            arraylist.Clear()
            arraylist.TrimToSize()
        Catch ex As Exception

            MessageBox.Show(ex.Message, "Error")
        End Try

    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.