[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use Multithreading to enable/Disble Buttons while Program running in Vb.Net

Posted on 2009-12-20
2
Medium Priority
?
533 Views
Last Modified: 2013-12-26
Hai

I am creating small application to calculate the stresses on pavement.for that,I need to ask user to input the required value.Later if he click the button to run the program to calculate stresses , first it will check whether anything wrong with input , if there are not then it will disable some button and continue the calculation. i have problem with disabling buttons by using thread.I cannot attach the code here since that is very long.therefor i have simplified the code and attached here.

refering attached code,and image of form , first user need to input three numbers . and if Run button is clicked , first it will check whether those inputs are numbers by using fun01.check function .i have used thread to run this function .because when program runs, those two button should be accessible.if there are no error in input , then program should disble run button and at the same time Cancel should appear and it should accessible anytime while calculation is in process.I have used thread to run the caculation function.

After finishing all the calculation , Run button display name should be changed to "Modify" and all those 3 textbox should be disbled.If modify button is clicked , it should enable all 3 textbox.

I tried using thread but it does not work.
Please guide me to overcome this problem.

Thx
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Run.Visible = True
        Run.Enabled = True
        cancel.Visible = False

    End Sub

    Private Sub Run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Run.Click
        If Run.Text = "Run" Then
            MyThread.t_Checkinput.Start()
            MyThread.WaitAnyEvents_checkInput.WaitOne()

            If Fun01.aa = 3 Then
                Run.Enabled = False
                cancel.Visible = True
                cancel.Enabled = True

                TextBox1.Enabled = False
                TextBox2.Enabled = False
                TextBox3.Enabled = False


                MyThread.t_Calc.Start()
                MyThread.WaitAnyEvents_Calc.WaitOne()

                Run.Enabled = True
                Run.Text = "Modify"
                cancel.Visible = False
                cancel.Enabled = False
            End If
        ElseIf Run.Text = "Modify" Then
            Run.Text = "Run"

            TextBox1.Enabled = True
            TextBox2.Enabled = True
            TextBox3.Enabled = True

        End If

    End Sub

    Shared Sub WaitAll(ByVal waitHandles() As WaitHandle)

        If Thread.CurrentThread.GetApartmentState() = ApartmentState.STA Then

            For Each waitHandle As WaitHandle In waitHandles
                waitHandle.WaitAny(New WaitHandle() {waitHandle})
            Next
        Else
            WaitHandle.WaitAll(waitHandles)
        End If
    End Sub
End Class




'/// fun01


Public Class Fun01
    Friend Shared aa As Integer = 0
    Shared Sub Check()

        If IsNumeric(Form1.TextBox1.Text) Then
            aa = aa + 1
        End If
        If IsNumeric(Form1.TextBox2.Text) Then
            aa = aa + 1
        End If
        If IsNumeric(Form1.TextBox3.Text) Then
            aa = aa + 1
        End If
        MyThread.WaitAnyEvents_checkInput.Set()
    End Sub
    Shared Sub CalcFric()
        Dim a As Double = 0
        Dim b As Double = 0
        For i = 0 To 30000
            a = a + 2 * i * CDbl(Form1.TextBox1.Text) * CDbl(Form1.TextBox2.Text)
            b = b + 2 * i + CDbl(Form1.TextBox1.Text) + CDbl(Form1.TextBox2.Text)
        Next
        Form1.Label4.Text = CStr(a)
        Form1.Label5.Text = CStr(b)
        MyThread.WaitAnyEvents_Calc.Set()
    End Sub

End Class


'/// mY thread

Imports System.Threading
Public Class MyThread
    Friend Shared t_Checkinput As Thread = New Thread(AddressOf Fun01.Check)
    Friend Shared t_Calc As Thread = New Thread(AddressOf Fun01.CalcFric)

    Friend Shared WaitAnyEvents_checkInput As AutoResetEvent = New AutoResetEvent(False)
    Friend Shared WaitAnyEvents_Calc As AutoResetEvent = New AutoResetEvent(False)

    Shared Sub WaitAll(ByVal waitHandles() As WaitHandle)

        If Thread.CurrentThread.GetApartmentState() = ApartmentState.STA Then

            For Each waitHandle As WaitHandle In waitHandles
                waitHandle.WaitAny(New WaitHandle() {waitHandle})
            Next
        Else
            WaitHandle.WaitAll(waitHandles)
        End If
    End Sub
End Class

Open in new window

Page-1.JPG
0
Comment
Question by:Nitharsan
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 200 total points
ID: 26093331
You can simplify things a TON by using the BackgroundWorker() control:
http://msdn.microsoft.com/en-us/library/8xs8549b.aspx
Public Class Form1

    Private a, b As Double
    Private dblA, dblB As Double
    Private WithEvents bgw As New System.ComponentModel.BackgroundWorker

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        bgw.WorkerSupportsCancellation = True

        Run.Text = "Run"
        cancel.Visible = False
    End Sub

    Private Sub Run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Run.Click
        If Run.Text = "Run" Then
            If Double.TryParse(TextBox1.Text, dblA) Then
                If Double.TryParse(TextBox2.Text, dblB) Then
                    Run.Enabled = False
                    cancel.Visible = True
                    TextBox1.Enabled = False
                    TextBox2.Enabled = False
                    bgw.RunWorkerAsync()
                Else
                    MessageBox.Show("Second: " & TextBox2.Text, "Invalid Second Number")
                End If
            Else
                MessageBox.Show("First: " & TextBox1.Text, "Invalid First Number")
            End If
        ElseIf Run.Text = "Modify" Then
            Run.Text = "Run"
            TextBox1.Enabled = True
            TextBox2.Enabled = True
        End If
    End Sub

    Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        cancel.Visible = False
        bgw.CancelAsync()
    End Sub

    Private Sub bgw_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
        a = 0
        b = 0
        For i As Integer = 0 To 30000
            a = a + 2 * i * dblA * dblB
            b = b + 2 * i + dblA + dblB

            If bgw.CancellationPending Then
                Exit For
            End If
        Next
        e.Result = Not bgw.CancellationPending
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
        If e.Result Then
            Label4.Text = a.ToString
            Label5.Text = b.ToString

            Run.Text = "Modify"
            Run.Enabled = True
            cancel.Visible = False
        Else
            MessageBox.Show("Operation Cancelled")
        End If
    End Sub

End Class

Open in new window

0
 

Author Comment

by:Nitharsan
ID: 26093993
Hai Mind
it is working well.i was spending two days for this problem.
Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 13 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question