S
H
A
R
E

Monday, May 02, 2011

VB.net Console with Form and Muti-threading


I using VS 2010, lets try it, Make your project with VB language, choose Console Application.
In Console Apllication, you can’t import the windows class (Imports System.Windows.Forms). Because, the references it not included automatically in Console Apllication project, but, it can be set, use this way:

·         On Solution Explorer, klik ‘My Project’ (Project properties and configurations):



·         Choose ‘Refferences’:

·         Click Add:

·         Select “System.Windows.Forms”:

“Click OK”, now you can create form in your console application :-), now you can use this import: Imports System.Windows.Forms.

NEXT: THE CODING

Import System.Windows.Forms in your project, and declaring this

    Dim T1 As New Threading.Thread(AddressOf Thread_one)
    Dim i As Integer = 0

Var I is use for counter, T1 is the thread class object, and Thread_One is the sub program that will run as thread T1.

Create the Main Sub:

    Sub Main()
        Console.WriteLine("Press enter and then the second thread will be run..")
        Console.ReadKey()
        T1.Start()
        Console.Write("Input User: ")
        Console.ReadLine()
        T1.Abort()
    End Sub

And this is the handler of Second Thread:

    Private Sub Thread_one()
        Dim Thread_one_form As New Form
        Dim Thread_one_label As New Label
        Dim i As Integer = 0
        Thread_one_form.Show()
        Thread_one_form.Top = 0
        Thread_one_form.Width = 200
        Thread_one_form.Height = 100
        Thread_one_label.Parent = Thread_one_form
        Thread_one_label.Dock = DockStyle.Top
        Thread_one_form.Text = "Waiting User Input"
        While True
            Thread_one_label.Text = i & " seconds."
            i += 1
            Threading.Thread.Sleep(1000)
            Thread_one_form.Left = (i + 1) * 10
            Thread_one_form.Update()
        End While
    End Sub

This subwill showing the form and wating for user input. U can edit this code for make your application run more better and escape from Not responding form process.

Download Tutorial (docx) and Project (VB2010)

0 comments:

Post a Comment