Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me.

why when me run a thread inside my code Then Are locked another controls my form

For example when i click on button1 then run thread1 Thereafter are locked button2.

please Please correct my code.

_______________all code________________________
VB
Imports System.Threading
Imports System.IO
Imports System.Diagnostics.Process
Imports System.Windows
Public Class Form1
     Public t1 As System.Threading.Thread
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Opacity = 2
        t1 = New System.Threading.Thread(New ThreadStart(AddressOf p1))
        t1.IsBackground = True
        t1.Start()
    End Sub
    Private Sub p1()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf p1))

        Else
            While ProgressBar1.Value < 100
                ProgressBar1.Value += 1
                Label1.Text = Str(ProgressBar1.Value) & "%"
                Label1.Update()
                Thread.Sleep(200)
            End While
        End If
        ProgressBar1.Value = 0
        t1.Abort()
    End Sub

      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        t1.Abort()
        Label2.Text = "taner Riffat"
    End Sub
End Class


[email removed to protect you from spammers]
Posted
Updated 12-Apr-10 8:03am
v2

1 solution

OK, Few problems that i see with the concept of threading in your code is, your thread isn't actually availing benefits of threading concept.

What it means ?
Your thread is just calling caller i.e main thread in which form is loaded, and caller is actually calling while loops and Thread.Sleep(200).
Thats the reason why you are facing the problem. Its just as good as running without any thread.

I think someone has shared good article here on Threading with Traffic signal example. see that it will help you.

Solution
Actually this aint a perfect solution, just a quick change that might work...

_value is global shared integer = 0
Private Sub p1()
    While _value < 100
        Thread.Sleep(200)
        p2()
    End While
    t1.Abort()
End Sub

Private Sub p2()
    If Me.InvokeRequired Then
        Me.Invoke(New MethodInvoker(AddressOf p2))
    Else
        _value = ProgressBar1.Value+1
        if _value < 100
            ProgressBar1.Value = _value
            Label1.Text = Str(ProgressBar1.Value) & "%"
            Label1.Update()
        End If
    End If
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900