| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3099 人关注过本帖
标题:多线程读取PLC地址位数据导致主页面假死
取消只看楼主 加入收藏
无聊时想想你
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-13
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
多线程读取PLC地址位数据导致主页面假死
最近在学习多线程,所以DEMO了一个程式但主页面卡死,感觉3个现场都调用同一个PLC控件的原因导致的,求高手帮解
Imports ACTETHERLib
Imports System.Threading.Thread


Public Class Form1
    Private thread1, thread2, thread3 As Threading.Thread
    Public Delegate Sub MyDelegate()
    Public clsPlcComm As New ActQJ71E71TCP
    Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim PlcIp As String
            Dim PlcNetNo As String
            Dim PlcStatNo As String
            Dim PcNetNo As String
            Dim PcStatNo As String
            Dim TimeOut As String
            Dim dlen As String
            Dim str As String = New String(Chr(0), 1024)
            Dim lRet As Integer
            Dim rtnCode As String
            Dim inFilepath As String = Application.StartupPath & "\Plcsetting.ini"
            dlen = GetPrivateProfileString("PLC", "PLCIP", "", str, Len(str), inFilepath)
            PlcIp = Strings.Left(str, dlen)
            dlen = GetPrivateProfileString("PLC", "PlcNetNo", "", str, Len(str), inFilepath)
            PlcNetNo = Strings.Left(str, dlen)
            dlen = GetPrivateProfileString("PLC", "PlcStatNo", "", str, Len(str), inFilepath)
            PlcStatNo = Strings.Left(str, dlen)
            dlen = GetPrivateProfileString("PLC", "PcNetNo", "", str, Len(str), inFilepath)
            PcNetNo = Strings.Left(str, dlen)
            dlen = GetPrivateProfileString("PLC", "PcStatNo", "", str, Len(str), inFilepath)
            PcStatNo = Strings.Left(str, dlen)
            dlen = GetPrivateProfileString("PLC", "TimeOut", "", str, Len(str), inFilepath)
            TimeOut = Strings.Left(str, dlen)
            With clsPlcComm
                .ActHostAddress = PlcIp
                .ActNetworkNumber = PlcNetNo
                .ActStationNumber = PlcStatNo
                .ActSourceNetworkNumber = PcNetNo
                .ActSourceStationNumber = PcStatNo
                .ActTimeOut = TimeOut
            End With

            lRet = clsPlcComm.Open()
            If lRet = 0 Then
                lblPlcStatus.BackColor = Color.Green

            Else
                rtnCode = (String.Format("0x{0:x8}", lRet)).ToUpper

                MessageBox.Show("连接失败(出错代码 :" + rtnCode + ")")
                lblPlcStatus.BackColor = Color.Red

            End If
        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
    End Sub

    Public Sub handle1()
        Control.CheckForIllegalCrossThreadCalls = False
        Dim Devlist1 As String
        Dim lret As Integer
        Dim lData As Integer = 0
        Devlist1 = "ZR24024"
        Dim OldValue As Integer = 0

        While True
            Application.DoEvents()
            OldValue = lData
            lret = clsPlcComm.GetDevice(Devlist1, lData)
            If lData = OldValue Then

            Else
                ListBox1.Items.Add(lData)
                ListBox1.Refresh()
            End If
        End While
    End Sub

    'Me.BeginInvoke(New MyDelegate(AddressOf Getval))


    Public Sub handle2()
        Control.CheckForIllegalCrossThreadCalls = False
        Dim Devlist2 As String
        Dim lret As Integer
        Dim lData As Integer
        Devlist2 = "ZR2000"
        Dim OldValue As Integer = 0
        While True
            Application.DoEvents()
            OldValue = lData
            lret = clsPlcComm.GetDevice(Devlist2, lData)
            If lData = OldValue Then

            Else
                ListBox2.Items.Add(lData)
                ListBox2.Refresh()
            End If
        End While
    End Sub
    Public Sub handle3()
        Control.CheckForIllegalCrossThreadCalls = False
        Dim Devlist3 As String
        Dim lret As Integer
        Dim lData As Integer
        Devlist3 = "ZR1000"
        Dim OldValue As Integer = 0
        While True
            Application.DoEvents()
            OldValue = lData
            lret = clsPlcComm.GetDevice(Devlist3, lData)
            If lData = OldValue Then

            Else
                ListBox3.Items.Add(lData)
                ListBox3.Refresh()
            End If
        End While

    End Sub

    Private Sub btnMonitor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMonitor.Click
        btnMonitor.Enabled = False
        Application.DoEvents()
        thread1 = New Threading.Thread(AddressOf handle1)
        thread1.Start()
        thread2 = New Threading.Thread(AddressOf handle2)
        thread2.Start()
        thread3 = New Threading.Thread(AddressOf handle3)
        thread3.Start()
    End Sub

    'Public Sub Getval()
    '    Dim Devlist1 As String
    '    Dim lret As Integer
    '    Dim lData As Integer = 0
    '    Devlist1 = "ZR24024"
    '    Dim OldValue As Integer = 0

    '    While True
    '        Application.DoEvents()
    '        OldValue = lData
    '        lret = clsPlcComm.GetDevice(Devlist1, lData)
    '        If lData = OldValue Then

    '        Else
    '            ListBox1.Items.Add(lData)
    '            ListBox1.Refresh()
    '        End If
    '    End While
    'End Sub

End Class
搜索更多相关主题的帖子: String str Integer End Dim 
2017-07-14 14:59
无聊时想想你
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-13
收藏
得分:0 
回复 2楼 shsmhq
不行,那个我有试过,加的时间长,会好些,到也有点卡,而且我要实时监控Plc值加了意义不大
2017-07-18 09:05
快速回复:多线程读取PLC地址位数据导致主页面假死
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017633 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved