错误:跨线程操作无效:控制'listView1'从其创建的线程以外的线程访问。我希望在12 ...之后手动拆分连续的charcet字符串 [英] ERROR : Cross-thread operation not valid: Control 'listView1' accessed from a thread other than the thread it was created on. and i want a split a continuous charcet string into manually after 12...

查看:76
本文介绍了错误:跨线程操作无效:控制'listView1'从其创建的线程以外的线程访问。我希望在12 ...之后手动拆分连续的charcet字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 私有  Sub  SMSPort_DataReceived( ByVal 发​​件人作为 对象 ByVal  e  As  System.IO.Ports.SerialDataReceivedEventArgs)句柄 comPort.DataReceived 

Dim str As String =
Dim temp 作为 字符串 =

objCon.GetConnectionString()
如果 e.EventType = SerialData .Chars 然后

SMSPort.DtrEnable = True
System.Threading.Thread.Sleep( 100
tempp = SMSPort.ReadExisting()。Replace( )。修剪()

//这里我希望 添加分裂字符
' For i As Integer = 1 to tempp.Length Step 12
' Debug.Write(Mid(tempp,i,12))

如果 tempp<> 然后
objCon .GetConnectionString()
objCon.GetConnection( 插入到tblAttended(Id,Status,Date,Time)值(' + tempp.ToString()+ ','in',' + System.DateTime.Today.ToShortDateString()+ ',' + DateTime.Now。 ToShortTimeString()+ ')

ListView1.Items.Add (tempp)<跨度类= 代码注释>' <跨度类= 代码注释><大> ERRORR< /大>
txtUniqueNumber.Text = tempp .Trim()' '< big> ERRORR< / big>
ListView1.AutoResizeCo lumns(ColumnHeaderAutoResizeStyle.ColumnContent)
结束 如果
' Next i
Control.CheckForIllegalCrossThreadCalls = False
结束 Sub

解决方案

HI,

您必须从委托调用此方法,该方法将其发送到不同的线程,并且从不同的线程您无法访问主线程。你应该做的只是更新变量并从主线程访问它们。



我想它可以帮到你。



RKS


您好! Amit,



首先,您必须为要在应用程序级别更改的每个属性声明一个变量。这样你就可以在主线程和辅助线程中访问这些变量了。



现在,你必须从二级更新属性,而不是直接更新属性线程并访问主线程中的变量以更新所需的属性。





希望它有所帮助,



如果有帮助则标记为答案。 .........

Private Sub SMSPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles comPort.DataReceived

        Dim str As String = ""
        Dim temp As String = ""

        objCon.GetConnectionString()
        If e.EventType = SerialData.Chars Then

            SMSPort.DtrEnable = True
            System.Threading.Thread.Sleep(100)
            tempp = SMSPort.ReadExisting().Replace("", "").Trim()

// here i want to add th split charcters
        '   For i As Integer = 1 To tempp.Length Step 12
            '  Debug.Write(Mid(tempp, i, 12))

            If tempp <> "" Then
                objCon.GetConnectionString()
                objCon.GetConnection("Insert into tblAttended(Id,Status,Date,Time)values('" + tempp.ToString() + "','In','" + System.DateTime.Today.ToShortDateString() + "','" + DateTime.Now.ToShortTimeString() + "')")

                ListView1.Items.Add(tempp)  '<big>ERRORR</big>
                txtUniqueNumber.Text = tempp.Trim() ''<big>ERRORR</big>
                ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
            End If
            'Next i
            Control.CheckForIllegalCrossThreadCalls = False
End Sub

解决方案

HI,
You must be calling this method from delegate which send it to different thread and from different thread you cannot access the primary thread. what you should do is just update variables and access them from Primary thread.

I guess it may help you.

RKS


Hello! Amit,

First, You have to declare one variable for every property your want to change at application level. so that u can access these variables in your primary thread as well as secondary thread.

Now, instead of updating the property directly, you have to update the property from secondary thread and access that variable in primary thread to update the desired property.


hope it helps,

mark as answer if helped..........


这篇关于错误:跨线程操作无效:控制'listView1'从其创建的线程以外的线程访问。我希望在12 ...之后手动拆分连续的charcet字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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