none
socket打开线程 RRS feed

  • 问题

  • 用socket打开线程以后为什么控制台应用程序工作起来很好,但是用vb.net窗体打开server端线程就说“没有响应”,窗体上面的控件就不显示。但是客户端运行正常,客户端和服务器端运行正常。只是服务器端窗体上的控件不显示仁厚标题上面提示“没有响应”。不会是我电脑不行吧。。
    muradil
    2009年5月18日 4:40

答案

  • Hi 穆拉迪力,

    因为Winform窗体的UI线程被你的while语句打断,所以窗体不能显示出来。如果你想用Winform窗体但不被“冻结”住,你需要开另一个线程来执行while语句里的内容。
    Imports System.Threading

    Public Class Form1

        Public Sub New()
            InitializeComponent()
            Dim thread As Thread = New Thread(AddressOf Run)
            thread.Start()
        End Sub

        Public Sub Run()
            While (True)
                ' Do what you need
            End While
        End Sub
    End Class
    这样你的窗体可以正常显示、拖动、操作等不受影响。

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    2009年5月19日 8:27

全部回复

  • 用socket打开线程  是怎么做的?循环listener?  最好能给段代码看看


    控制台程序是 多线程模型   winform 是单线程模型  的确不一样的



    有效回复过700 撒花
    2009年5月18日 4:47
  • Private Sub listening()
            Dim ipAddr As IPAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList(0)  '获取服务器ip
            Try
                serverSocket = New TcpListener(ipAddr, 12345)        '生成侦听服务端
                serverSocket.Start()
            Catch ex As Exception
                MsgBox(ex.Message)
                End
            End Try
            While (True)
                Try
                    Dim acceptSocket As TcpClient = serverSocket.AcceptTcpClient() '唤醒挂起Client,返回服务套接字
                    numOfClient += 1
                    'Console.Write("第 " + CStr(numOfClient) + " 号用户来了!")
                    stream = acceptSocket.GetStream()
                    Dim buffer(1024) As Byte '缓冲区
                    stream.Read(buffer, 0, 1024)
                    mContent = System.Text.Encoding.GetEncoding("UTF-8").GetString(buffer)
                    read = New XmlTextReader(New IO.StringReader(mContent))
                    Dim user As UserInf = New UserInf
                    While (read.Read())
                        If (read.NodeType = XmlNodeType.Element And read.Name = "userAccount") Then
                            user.userAccount = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userPsw") Then
                            user.userPsw = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userName") Then '获取用户昵称
                            user.userName = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userHead") Then '获取头像地址
                            user.userHead = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userWord") Then '获取个性签名
                            user.userWord = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userSex") Then '获取性别
                            user.userSex = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userAge") Then '获取年龄
                            user.userAge = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userEmail") Then '获取Email 
                            user.userEmail = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userTel") Then '获取电话
                            user.userTel = read.ReadElementContentAsString
                        ElseIf (read.NodeType = XmlNodeType.Element And read.Name = "userState") Then '获取状态
                            user.userState = read.ReadElementContentAsString
                        End If
                    End While
                    'Console.WriteLine(" 欢迎" + user.userAccount + " 进入本系统!")
                    Dim newThread As ThreadClass
                    Dim certainThread As Thread
                    newThread = New ThreadClass(user, acceptSocket)
                    certainThread = New Thread(AddressOf newThread.newComerHandle)
                    Try
                        certainThread.Start()
                    Catch ex As Exception
                        If Not certainThread Is Nothing Then
                            certainThread.Abort()
                            certainThread = Nothing
                        End If
    
                    End Try
    
                Catch ex As Exception
                    If Not stream Is Nothing Then
                        stream.Close()
                    End If
                    If Not read Is Nothing Then
                        read.Close()
                    End If
                    'Console.WriteLine("新用户登录失败,服务器断开连接!")
                End Try
            End While
        End Sub
    这是核心代码。怎样可以窗体和线程是同步运行啊。本来以为我的电脑速度不行,但是别的电脑还是那样。
    muradil
    2009年5月18日 9:57
  • 1  您可不能把  accept socket 和 读取client  放在一个线程阿

    2  winform 显示线程被您这么一 while   就完全没时间paint 了 自然会出错咯

    建议您采用.net 异步调用   请您参考我的这篇blog

    http://www.cnblogs.com/waynebaby/archive/2009/01/14/1375234.html


    有效回复过700 撒花
    2009年5月19日 7:22
  • Hi 穆拉迪力,

    因为Winform窗体的UI线程被你的while语句打断,所以窗体不能显示出来。如果你想用Winform窗体但不被“冻结”住,你需要开另一个线程来执行while语句里的内容。
    Imports System.Threading

    Public Class Form1

        Public Sub New()
            InitializeComponent()
            Dim thread As Thread = New Thread(AddressOf Run)
            thread.Start()
        End Sub

        Public Sub Run()
            While (True)
                ' Do what you need
            End While
        End Sub
    End Class
    这样你的窗体可以正常显示、拖动、操作等不受影响。

    Sincerely,
    Kira Qian


    Please mark the replies as answers if they help and unmark if they don't.
    2009年5月19日 8:27
  • 呵呵。。。太爽了。。。谢谢。。。


    muradil
    2009年5月20日 5:05