帮我解决线程问题 [英] help me for thread problem

查看:72
本文介绍了帮我解决线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.

为什么当我在代码中运行线程然后又被锁定另一个控件时?

例如,当我单击button1然后运行线程1之后,将其锁定为button2.

请更正我的代码.

_______________所有代码________________________

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________________________

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]

推荐答案

好,我发现在代码中使用线程概念的一些问题是,您的线程实际上不是线程概念的好处.

这是什么意思 ?
您的线程只是在调用调用程序,即加载表单的主线程,而调用程序实际上是在while循环和Thread.Sleep(200)上调用的.
这就是您面对问题的原因.它就像没有任何线程运行一样好.

我认为有人在这里分享了有关使用交通信号进行线程处理"示例的出色文章.看到它将对您有帮助.

解决方案
实际上,这不是一个完美的解决方案,只是一个可能有效的快速更改...

_value是全局共享整数= 0
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


这篇关于帮我解决线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆