none
How to use backgroundworker getting data from a serialport? RRS feed

  • 问题

  • I use some backgroundworkers in my VB2010 program and every backgroundwoorker needs data from a serialport. And when the backgroundwork completed the data will be displayed. How to do that?
    2011年7月23日 5:37

答案

  • 这边是中文论坛, 可以翻译为中文吗?

    对于你说的如何返回数据: 请问是以什么样的形式返回? 要显示在窗体的某个控件上吗?还是只是提供给主线程?

    如果是第一种: 利用代理很方便:

     Public Delegate Sub SetTitleDelegate(ByVal TitleText As String)
     Private Sub SetLabel(ByVal TitleText As String)
      If Me.InvokeRequired Then
       Me.Invoke(New SetTitleDelegate(AddressOf SetLabel), TitleText)
      Else
       Me.Label1.Text = TitleText
      End If
     End Sub
    
     Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      Dim i As Integer = 0
      While cancel = False
       i += 1
       'Threading.Thread.Sleep(100)
       If System.IO.File.Exists("D:\A\mydata.csv") Then
        SetLabel("find it " & i.ToString)
       Else
        SetLabel("not yet " & i.ToString)
       End If
      End While
     End Sub
    
    

    如果是第二种, 那就定义个公共变量, 每次赋值就行,然后主线程或其他backgroundworker用这个变量取值就行了。

    Best regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月25日 5:43
    版主

全部回复

  • Code like this:

     

    sub backgroundworker1_DoWork

          dim dataFromSerialPort as new SerialData(...)

          .....

    end sub

     

    class SerialData(...)

        sub new()

        dim s1 as SerialPort

        addhandler SerialPort.DataRecieved, Addresof getData

        end sub

        private sub getData()

            dim data as string=SerialPort.ReadExist

            ...

        ebd sub  

    end class

     

     

    How to return data to backgroundwork1?

    2011年7月23日 6:03
  • 这边是中文论坛, 可以翻译为中文吗?

    对于你说的如何返回数据: 请问是以什么样的形式返回? 要显示在窗体的某个控件上吗?还是只是提供给主线程?

    如果是第一种: 利用代理很方便:

     Public Delegate Sub SetTitleDelegate(ByVal TitleText As String)
     Private Sub SetLabel(ByVal TitleText As String)
      If Me.InvokeRequired Then
       Me.Invoke(New SetTitleDelegate(AddressOf SetLabel), TitleText)
      Else
       Me.Label1.Text = TitleText
      End If
     End Sub
    
     Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      Dim i As Integer = 0
      While cancel = False
       i += 1
       'Threading.Thread.Sleep(100)
       If System.IO.File.Exists("D:\A\mydata.csv") Then
        SetLabel("find it " & i.ToString)
       Else
        SetLabel("not yet " & i.ToString)
       End If
      End While
     End Sub
    
    

    如果是第二种, 那就定义个公共变量, 每次赋值就行,然后主线程或其他backgroundworker用这个变量取值就行了。

    Best regards,

     


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月25日 5:43
    版主
  • Thanks
    2011年8月1日 15:04