Skip to main content

 none
How to convert C# to VB.NET Invoke with EventHandler function. RRS feed

  • Question

  • Dear Developer team,

    i got sample code in C# and I want to convert to VB2008 for unsing on Smart device (CE).

    Thie problem is I can't call Thread "thrReceiveUii = new System.Threading.Thread(ReceiveUiiProc);".

    because I can't convert sub "private void ReceiveUiiProc()" to VB.Net.

    C# code below.

    private void _btReadReadID_Click(object sender, EventArgs e)
            {
    
                ReadID();
            }
    
            byte[] uAccessPwd = new byte[4];
            /// <summary>
            /// read epc
            /// </summary>
            void ReadID()
            {
                if (this.cbAnticollision.Checked)
                {
                    flagAnti = 0x01;
                    iniAntiQ = (byte)int.Parse(this.cbqvalue.Text);
                }
                else
                    flagAnti = 0x00;
    
    
                if (this._btReadReadID.Text == "ReadEPC")
                {
                    if (btnEPCTID.Text == "Stop")
                    {
                        MessageBox.Show("Please stop TID reading");
                        return;
                    }
                    _lvReadID.Visible = true;
                    listView1.Visible = false;
                    this._btReadReadID.Text = "Stop";
                    this._btClearReadID.Enabled = false;
                    this.cbAnticollision.Enabled = false;
                    ClearReadID();
                    
                    run = true;
                }
                else
                {
                    this._btReadReadID.Text = "ReadEPC";
                    this._btClearReadID.Enabled = true;
                    this.cbAnticollision.Enabled = true;
    
    
                    run = false;
                }
    
                if (run)
                {
                    //MessageBox.Show("ReadingMode:" + flagAnti + ",Q" + iniAntiQ);
                    canReceive = UHF.UHFInventory(flagAnti, iniAntiQ);             
                    if (canReceive)
                    {
    
    
                        //enable UII thread receiving
                        if (thrReceiveUii == null)
                        {
                            thrReceiveUii = new System.Threading.Thread(ReceiveUiiProc);
                            thrReceiveUii.IsBackground = true;
                            thrReceiveUii.Start();
                        }
                        else
                        {
                            thrReceiveUii.Start();
                        }
                    }
                }
                else
                {
    
                    //disable UII thread receiving
                    if (thrReceiveUii != null)
                    {
                        thrReceiveUii.Abort();
                        thrReceiveUii = null;
                        canReceive = false;
                    }
                    //stop UHF reading
                    UHF.UHFStopGet();
                }
            }
    
            System.Threading.Thread thrReceiveUii = null;
            bool canReceive = false;
            string tid = "";
    
            private void ReceiveUiiProc()
            {
                while (canReceive)
                {
                    int[] uLenUii = new int[1];
                    byte[] uUii = new byte[128];   //UII should be at least 6 bytes
                    
                     if (UHF.UHFGetReceived(uLenUii, uUii))//
                    {
                        if (uLenUii[0] > 0)
                        {
                            Win32.PlayScanSound();
                            string uii = BitConverter.ToString(uUii, 0, uLenUii[0]).Replace("-", "");                       
                            //show to the interface
                            TagRead ent = new TagRead();
                            ent.Uii = uii;
                            ent.Epc = uii.Substring(4, uii.Length - 4);  //EPC
                           
                            ent.Count = 1;
                            if (!datalist.ContainsKey(ent.Epc))
                            {
    
                                datalist.Add(ent.Epc, 1);
                                ListViewItem lv = new ListViewItem();
                                lv.Text = (datalist.Count).ToString();
                                lv.SubItems.Add(ent.Epc);                          
                                lv.SubItems.Add("1");
                                _lvReadID.Invoke(new EventHandler(delegate
                                {
                                    _lvReadID.Items.Add(lv);
                                }));
    
                                _lbSumTagsReadID.Invoke(new EventHandler(delegate
                                {
                                    _lbSumTagsReadID.Text = datalist.Count.ToString();
                                }));
                            }
                            else
                            {
                                datalist[ent.Epc] += 1;
                                DisplayTagID(ent);
    
                            }
                            //UpdateControl(ent);
    
                        }
                    }
    
                    // System.Threading.Thread.Sleep(50);
                }
            }
            Dictionary<string, int> datalist = new Dictionary<string, int>();//EPC list, EPC, times
    
            private void DisplayTagID(TagRead ent)
            {
    
                _lvReadID.Invoke(new EventHandler(delegate
                               {
                                   for (int cpt = 0; cpt < _lvReadID.Items.Count; cpt++)
                                   {
                                       if (this._lvReadID.Items[cpt].SubItems[1].Text == ent.Epc)
                                       {
                                           _lvReadID.Items[cpt].SubItems[2].Text = datalist[ent.Epc].ToString();
                                           break;
                                       }
                                   }
                               }));
                
            }
    
            private void ClearReadID()
            {
                datalist.Clear();
                datalisttid.Clear();
                listView1.Items.Clear();
                _lvReadID.Items.Clear();
                _lbSumTagsReadID.Text = "0";
            }
    
            #region invalid codes
            delegate void UpdateControlDelegate(TagRead ent);
            List<TagRead> list = new List<TagRead>();
            private void UpdateControl(TagRead ent)
            {
                if (this.InvokeRequired)
                {
                    UpdateControlDelegate d = new UpdateControlDelegate(UpdateControl);
                    this.Invoke(d, ent);
                }
                else
                {
                    bool exist = false;
                    foreach (TagRead var in list)
                    {
                        if (var.Epc == ent.Epc)
                        {
                            exist = true;
                            var.Count += ent.Count;
                        }
                    }
                    if (!exist)
                    {
                        ent.Serial = list.Count + 1;
                        list.Add(ent);
                    }
                    this._lvReadID.Items.Clear();
                    foreach (TagRead var in list)
                    {
                        string[] strArr = new string[3];
                        strArr[0] = var.Serial.ToString();
                        strArr[1] = var.Epc;
                        strArr[2] = var.Count.ToString();
    
                        ListViewItem item = new ListViewItem(strArr);
                        this._lvReadID.Items.Add(item);
                    }
                    _lbSumTagsReadID.Invoke(new EventHandler(delegate
                    {
                        _lbSumTagsReadID.Text = list.Count.ToString();
                    }));
    
                }
            }
            #endregion


    Friday, September 13, 2019 10:54 AM

All replies

  • Hello,

    I don't have time to actually try the conversion (done with Tangible software converter)  so I don't know if it's going to work but worth a try.

    Private Sub _btReadReadID_Click(ByVal sender As Object, ByVal e As EventArgs)
    
    			ReadID()
    End Sub
    
    		Private uAccessPwd(3) As Byte
    		''' <summary>
    		''' read epc
    		''' </summary>
    		Private Sub ReadID()
    			If Me.cbAnticollision.Checked Then
    				flagAnti = &H1
    				iniAntiQ = CByte(Integer.Parse(Me.cbqvalue.Text))
    			Else
    				flagAnti = &H0
    			End If
    
    
    			If Me._btReadReadID.Text = "ReadEPC" Then
    				If btnEPCTID.Text = "Stop" Then
    					MessageBox.Show("Please stop TID reading")
    					Return
    				End If
    				_lvReadID.Visible = True
    				listView1.Visible = False
    				Me._btReadReadID.Text = "Stop"
    				Me._btClearReadID.Enabled = False
    				Me.cbAnticollision.Enabled = False
    				ClearReadID()
    
    				run = True
    			Else
    				Me._btReadReadID.Text = "ReadEPC"
    				Me._btClearReadID.Enabled = True
    				Me.cbAnticollision.Enabled = True
    
    
    				run = False
    			End If
    
    			If run Then
    				'MessageBox.Show("ReadingMode:" + flagAnti + ",Q" + iniAntiQ);
    				canReceive = UHF.UHFInventory(flagAnti, iniAntiQ)
    				If canReceive Then
    
    
    					'enable UII thread receiving
    					If thrReceiveUii Is Nothing Then
    						thrReceiveUii = New System.Threading.Thread(AddressOf ReceiveUiiProc)
    						thrReceiveUii.IsBackground = True
    						thrReceiveUii.Start()
    					Else
    						thrReceiveUii.Start()
    					End If
    				End If
    			Else
    
    				'disable UII thread receiving
    				If thrReceiveUii IsNot Nothing Then
    					thrReceiveUii.Abort()
    					thrReceiveUii = Nothing
    					canReceive = False
    				End If
    				'stop UHF reading
    				UHF.UHFStopGet()
    			End If
    		End Sub
    
    		Private thrReceiveUii As System.Threading.Thread = Nothing
    		Private canReceive As Boolean = False
    		Private tid As String = ""
    
    		Private Sub ReceiveUiiProc()
    			Do While canReceive
    				Dim uLenUii(0) As Integer
    				Dim uUii(127) As Byte 'UII should be at least 6 bytes
    
    				 If UHF.UHFGetReceived(uLenUii, uUii) Then
    					If uLenUii(0) > 0 Then
    						Win32.PlayScanSound()
    						Dim uii As String = BitConverter.ToString(uUii, 0, uLenUii(0)).Replace("-", "")
    						'show to the interface
    						Dim ent As New TagRead()
    						ent.Uii = uii
    						ent.Epc = uii.Substring(4, uii.Length - 4) 'EPC
    
    						ent.Count = 1
    						If Not datalist.ContainsKey(ent.Epc) Then
    
    							datalist.Add(ent.Epc, 1)
    							Dim lv As New ListViewItem()
    							lv.Text = (datalist.Count).ToString()
    							lv.SubItems.Add(ent.Epc)
    							lv.SubItems.Add("1")
    							_lvReadID.Invoke(New EventHandler(Sub()
    								_lvReadID.Items.Add(lv)
    							End Sub))
    
    							_lbSumTagsReadID.Invoke(New EventHandler(Sub()
    								_lbSumTagsReadID.Text = datalist.Count.ToString()
    							End Sub))
    						Else
    							datalist(ent.Epc) += 1
    							DisplayTagID(ent)
    
    						End If
    						'UpdateControl(ent);
    
    					End If
    				 End If
    
    				' System.Threading.Thread.Sleep(50);
    			Loop
    		End Sub
    		Private datalist As New Dictionary(Of String, Integer)() 'EPC list, EPC, times
    
    		Private Sub DisplayTagID(ByVal ent As TagRead)
    
    			_lvReadID.Invoke(New EventHandler(Sub()
    							   For cpt As Integer = 0 To _lvReadID.Items.Count - 1
    								   If Me._lvReadID.Items(cpt).SubItems(1).Text = ent.Epc Then
    									   _lvReadID.Items(cpt).SubItems(2).Text = datalist(ent.Epc).ToString()
    									   Exit For
    								   End If
    							   Next
    			End Sub))
    
    		End Sub
    
    		Private Sub ClearReadID()
    			datalist.Clear()
    			datalisttid.Clear()
    			listView1.Items.Clear()
    			_lvReadID.Items.Clear()
    			_lbSumTagsReadID.Text = "0"
    		End Sub
    
    		#Region "invalid codes"
    		Friend Delegate Sub UpdateControlDelegate(ByVal ent As TagRead)
    		Private list As New List(Of TagRead)()
    		Private Sub UpdateControl(ByVal ent As TagRead)
    			If Me.InvokeRequired Then
    				Dim d As New UpdateControlDelegate(AddressOf UpdateControl)
    				Me.Invoke(d, ent)
    			Else
    				Dim exist As Boolean = False
    				For Each var As TagRead In list
    					If var.Epc = ent.Epc Then
    						exist = True
    						var.Count += ent.Count
    					End If
    				Next
    				If Not exist Then
    					ent.Serial = list.Count + 1
    					list.Add(ent)
    				End If
    				Me._lvReadID.Items.Clear()
    				For Each var As TagRead In list
    					Dim strArr(2) As String
    					strArr(0) = var.Serial.ToString()
    					strArr(1) = var.Epc
    					strArr(2) = var.Count.ToString()
    
    					Dim item As New ListViewItem(strArr)
    					Me._lvReadID.Items.Add(item)
    				Next
    				_lbSumTagsReadID.Invoke(New EventHandler(Sub()
    					_lbSumTagsReadID.Text = list.Count.ToString()
    				End Sub))
    
    			End If
    		End Sub
    		#End Region
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, September 13, 2019 11:20 AM
    Moderator
  • Hi,

    The codes  as follows  may convert sub "private void ReceiveUiiProc()" to VB.Net. You can have a try at them.

    Private Sub ReceiveUiiProc()
            While canReceive
                Dim uLenUii As Integer() = New Integer(0) {}
                Dim uUii As Byte() = New Byte(127) {}
    
                If UHF.UHFGetReceived(uLenUii, uUii) Then
    
                    If uLenUii(0) > 0 Then
                        Win32.PlayScanSound()
                        Dim uii As String = BitConverter.ToString(uUii, 0, uLenUii(0)).Replace("-", "")
                        Dim ent As TagRead = New TagRead()
                        ent.Uii = uii
                        ent.Epc = uii.Substring(4, uii.Length - 4)
                        ent.Count = 1
    
                        If Not datalist.ContainsKey(ent.Epc) Then
                            datalist.Add(ent.Epc, 1)
                            Dim lv As ListViewItem = New ListViewItem()
                            lv.Text = (datalist.Count).ToString()
                            lv.SubItems.Add(ent.Epc)
                            lv.SubItems.Add("1")
                            _lvReadID.Invoke(New EventHandler(Sub()
                                                                  _lvReadID.Items.Add(lv)
                                                              End Sub))
                            _lbSumTagsReadID.Invoke(New EventHandler(Sub()
                                                                         _lbSumTagsReadID.Text = datalist.Count.ToString()
                                                                     End Sub))
                        Else
                            datalist(ent.Epc) += 1
                            DisplayTagID(ent)
                        End If
                    End If
                End If
            End While
        End Sub

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 16, 2019 5:59 AM
    Moderator