• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

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

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
Nitharsan
Asked:
Nitharsan
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
NitharsanAuthor Commented:
Hai Mind
it is working well.i was spending two days for this problem.
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now