主线程中:
private void button2_Click(object sender, EventArgs e)
        {
            this.IsStop = false;
            Thread jiankong = new Thread(new ThreadStart(ganhuo));//启动工作线程
            jiankong.Start();            ListViewItem item = new ListViewItem("开始工作");
            this.listView1.Items.Add(item);
            string logs = this.gd.errorinfo;
            while (this.gd.errorinfo != null)//这里不好,因为阻塞了主线程
            {
                while (this.gd.errorinfo != logs)
                {
                    item = new ListViewItem(this.gd.errorinfo);//获取工作线程中errorinfo的值,这里肯定是不对的
                    this.listView1.Items.Add(item);
                    logs = this.gd.errorinfo;
                }
               
                Thread.Sleep(1000);
            }
           
        }工作线程:用的是VB,关键是记录errorinfo的值
    Public Class gdunion
        Public total As Integer
        Public pram As String
        Public IsStop As Boolean
        Public price As String
        Public num1 As Integer
        Public num2 As Integer
        Public errorinfo As String
        Public mphdevice As String
        Public password As String
        Public code As String
        Public artist As String
        Public title As String
        Public ringtype As String
        Public copyRight As String
        Public cp As String
        Public dateStr As String = String.Format("{0:yyyy-MM-dd}", Now()) '或者是now.tostring("yyyy-MM-dd")
        Dim bianhao As String = ""      
        Public Sub gdunion()
                       Dim i As Int32
            i = 0
            Dim bianhao As String = ""
            Dim pram1 As String
            Dim pram2 As String
            Dim sindex, sindex2, eindex, mindex As Int32
            Dim sessionid As String = ""
            Dim url As String = "http://www.101568.com/customize-1.jsp?id=" + Me.code
            Dim refer As String = "http://www.101568.com/login.jsp"
            For i = 1 To Me.total
                If Me.IsStop = True Then
                    Exit Sub
                End If
                Try
                    Dim result As String = Postweb("http://www.101568.com/login.jsp", "USER_MDN=" & mphdevice & "&PASSWORD=" & password, "gb2312", , refer)
                    sessionid = result
                    Me.errorinfo = " 登录成功," & sessionid //记录登录成功到日志即listview
                    Dim result2 As String = GetWeb(url, sessionid, "http://www.101568.com/login.jsp")
                    refer = url                    sindex = result2.IndexOf("<input type=hidden name=""")
                    If sindex = -1 Then
                        errorinfo = "登录失败"////记录登录失败到日志即listview
                        Exit Sub                    End If
                    pram = ""
                    While (sindex <> -1)
                        sindex = sindex + "<input type=hidden name=""".Length
                        mindex = result2.IndexOf(""" value=""", sindex)
                        pram1 = result2.Substring(sindex, mindex - sindex) + "="
                        mindex = mindex + """ value=""".Length
                        eindex = result2.IndexOf(""" />", mindex)
                        pram2 = StrConv(result2.Substring(mindex, eindex - mindex))
                        pram += pram1 + pram2 + "&"
                        sindex = result2.IndexOf("<input type=hidden name=""", eindex) 
                    End While
                    Dim len2 As Integer = Encoding.GetEncoding("GB2312").GetBytes(pram.Trim).Length
                    pram = pram + "pm1=&pm2=&x=26&y=11"
                    Dim len1 As Integer = Encoding.GetEncoding("GB2312").GetBytes(pram.Trim).Length
                    
                    Dim result3 As String = Postweb("http://www.101568.com/customizePro.jsp", pram, "gb2312", sessionid, refer)
                    If result3.IndexOf("操作成功") <> -1 Then
                        num1 += 1
                        Me.errorinfo = mphdevice & "本次订购成功!已成功" & num1 & "次,已失败" & num2 & "次"//记示定购成功到日志即listview
                    ElseIf result3.IndexOf("音乐盒已满") Then
                        Dim myring As String = GetWeb("http://www.101568.com/myring.jsp", sessionid)
                        sindex2 = myring.IndexOf("delRing.jsp?code=")
                        Do While sindex2 > -1
                            eindex = myring.IndexOf("&type=1", sindex2)
                            bianhao = myring.Substring(sindex2, eindex - sindex2)
                            bianhao = "http://www.101568.com/" & bianhao & "&type=1"
                            Dim delring As String = GetWeb(bianhao, sessionid, "http://www.101568.com/myring.jsp")
                            Me.errorinfo = "成功删除一条炫铃"
                            sindex2 += 5
                            sindex2 = myring.IndexOf("delRing.jsp?code=", sindex2)
                        Loop                    Else
                        num2 += 1
                        Me.errorinfo = mphdevice & "本次订购失败!已成功" & num1 & "次,已失败" & num2 & "次"
                    End If
                    If Right(num1.ToString, 1) = 0 And num1 > 0 Then
                        Dim myring As String = GetWeb("http://www.101568.com/myring.jsp", sessionid)
                        sindex2 = myring.IndexOf("delRing.jsp?code=")
                        Do While sindex2 > -1
                            eindex = myring.IndexOf("&type=1", sindex2)
                            bianhao = myring.Substring(sindex2, eindex - sindex2)
                            bianhao = "http://www.101568.com/" & bianhao & "&type=1"
                            Dim delring As String = GetWeb(bianhao, sessionid, "http://www.101568.com/myring.jsp")
                            Me.errorinfo = "成功删除一条炫铃"
                            sindex2 += 5
                            sindex2 = myring.IndexOf("delRing.jsp?code=", sindex2)
                        Loop
                    End If                Catch ex As Exception                    Me.errorinfo = ex.ToString
                    Exit Sub 
                End Try            Next
        End Sub
end class大家可能会说事件或者委托,我也知道,我希望大家能写出来,否则我还是不知道怎么做,还有一个就是线程不能操作组件,如果用事件,不知用不用面临这个问题
在线等。。

解决方案 »

  1.   

    不能直接在线程中操作UI,需要用到Invoke或者beginInvoke
    参看
    http://blog.csdn.net/Knight94/archive/2006/08/24/1111267.aspx
      

  2.   

    而且,如何判断Errorinfo的值是否变化了也是一个问题啊
      

  3.   

    其实都是一样的,只不过现在把自线程改成vb.net做了而已
    大致思路,首先需要定义委托函数类型,然后把主线程的函数与其绑定。private void ShowMessage( string ValueString )
    {
        yourListView.Items.Add( ValueString );//Add a new item
    }//In sub-thread file
    public delegate void ShowMessageHandle( string ValueString );//In sub-thread class
    private Control ctrlParent;//For invoke method in sub-thread
    private ShowMessageHandle pHandle;//In sub-thread function
    ctrlParent.Invoke( pHandle, new object[]{ "yourValue"} );那么在主线程初始化子线程的时候,只要把ctrlParent和pHandle进行初始化,这样你就可以在自线程中来操作主线程的控件。ps:上面给的文章已经说明了这一点,你只要变通即可实现。
      

  4.   

    K兄,能告诉我你的QQ吗?我想把整个解决方案发给你,帮我解决一下!今天俺就交稿了,急ING
      

  5.   

    In sub-thread
    这是主线程还是子线程啊
      

  6.   

    其实,你只要参看我的这篇文章即可《如何弹出一个模式窗口来显示进度条》
    http://blog.csdn.net/knight94/archive/2006/05/27/757351.aspx