交叉线程问题 - 从网络计算机工作 - 而不是从本地计算机工作 [英] Cross threading issue - works from network computers - not from local computer

查看:81
本文介绍了交叉线程问题 - 从网络计算机工作 - 而不是从本地计算机工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G'day



我的程序工作非常接近我想要的V1.0。



与谢尔盖讨论后,我坐下来试着弄清楚如何编写TCP(这里有什么地方我可以把它放到这里,以便人们可以看看它并给我改进它的想法?)



如果我通过网络计算机发送数据流,它会起作用,但如果我在本地发送它会发生什么(如果我选择环回地址或网络IP无关紧要)它会在表单更新时作为交叉线程出现。



我不会抛出整个负载在那里,但有谁可以看到为什么它可能在本地导致问题,但不是从联网的计算机?



updateform子是交叉线程的地方发生。



再一次,我还在学习。请有建设性。



如果我没有收录,我愿意加入。如果你想要完整的东西,我会把它提起来。



G'day

I've got my program working pretty darn close to how I want for a V1.0.

After discussing with Sergey, I've sat down and "tried" to work out how to write a TCP (Is there anywhere in here that I could put this up just so people could have a look at it and give me ideas on improving it?)

What happens is if I send through a stream of data via a networked computer it works, but if I send it locally (It doesn't matter if I choose the loopback address or the network I.P) it comes up as a cross thread on the form update.

I won't throw the whole load up there, but can anyone see why it may be causing an issue locally, but not from a networked computer?

The updateform sub is where the cross threading occurs.

Again, I'm still learning. Please be constructive.

If I haven't included something, I am willing to include it. If you want the whole thing up, I will put it up.

Private Sub txtboxcreate(ByVal datastring As System.Object, ByRef currenttb As RichTextBox)

        Dim count As Integer = 1
        Dim tb As New RichTextBox
        Name = ("TxtBox" & CStr(counttotal))
        With tb
            .Name = Name
            .Multiline = True
            .AutoSize = True
            .Width = 250
            .ReadOnly = True
            .ScrollBars = False
            .Tag = 20
            .WordWrap = True
            .BackColor = Color.Black
            .Height = linecount * 22

            AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
        End With
        textboxestime.Add(tb, 0)
        count = (count + counttotal)
        currenttb = tb
        updatefrm(tb)
        append(datastring)
    End Sub







Private Sub updatefrm(ByVal tb As RichTextBox)

                If InvokeRequired Then
            Invoke(Sub() FlowLayoutPanel1.Controls.Add(tb))
        Else : FlowLayoutPanel1.Controls.Add(tb)
        End If
    End Sub

推荐答案

您在函数 Private Sub txtboxcreate 中创建文本框 - 这可能是问题所在:如果在一个函数中调用该函数与UI线程不同的线程,Textbox是在该不同的线程中创建的,并且稍后会在加入时引起问题从UI线程中获取...

确保该功能已在UI线程中运行!
You create the Textbox in the function Private Sub txtboxcreate - and here is likely the problem: if the function is called in a different thread than the UI thread, that Textbox is created in that different thread, and will later cause problems when it is accessed from the UI thread...
Make sure that that function is already running in the UI thread!


这篇关于交叉线程问题 - 从网络计算机工作 - 而不是从本地计算机工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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