CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 1999
    Location
    Ohio, USA
    Posts
    163

    cannot abort threads

    In one of my multi threading programs I cannot get my threads to abort. I have about 5 threads and the threads starts one at a time. By the time the fifth thread is started the program gets really slow. The program will not execute pass LoadWheelStn1. How should I resolve this problem?
    Code:
     
    Private Sub LoadWheelStn1()
            If sPortOpenFlag = "T" Then
                Dim MyThread1 As New Thread(AddressOf ThreadStn1)
                MyThread1.Start()
            End If
    
        End Sub
    
        Private Sub ThreadStn1()
    
            Dim varRcvBuff1 As Integer
    
            Try
                Thread.Sleep(3000)
                Application.DoEvents()
    
                SyncLock Me
                    varRcvBuff1 = AxAComm1.get_ValueD(900)
                    'do stuff
                End SyncLock
    
    
                Do While (varRcvBuff1 <> 25)
                    Thread.Sleep(331)
                    Application.DoEvents()
                    SyncLock Me
                        varRcvBuff1 = AxAComm1.get_ValueD(900)
                        'do stuff
                    End SyncLock
                Loop
    
                If (varRcvBuff1 = 25) Then
                    varRcvBuff1 = 0
                    LoadWheelStn1()
                    If Thread.CurrentThread.IsAlive Then
                        Thread.CurrentThread.Abort()
                    End If
                End If
    
            Catch ex As Exception
                Select Case Err.Number
                    Case 0
    
                    Case Else
    
                End Select
            Finally
                Thread.CurrentThread.Abort()
    
            End Try
        End Sub
    I am using vb 2005
    Last edited by shaminda; April 17th, 2010 at 03:37 PM.

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: cannot abort threads

    I seriously think you need a new methodology for this app.

    You start a thread, then call a function within that thread, then in that function, you are starting the thread over again. I am not sure what you are trying to do, but I think a new way of doing it is in order.

    My Guess is the reason your app get slow, is that you end up creating hundreds of threads.

  3. #3
    Join Date
    Mar 1999
    Location
    Ohio, USA
    Posts
    163

    Re: cannot abort threads

    I could have done every thing I want in one function. But unfortunately the ActiveX control I use to read from a PLC does not support multithreading. So I had to do it this way. So here is how it work:

    1) I read some data from the first thread
    2) I read some data from the second thread
    3) I read some data from the third thread
    4) same
    5) same

    I don't know from which one I am going to receive the data though. I may receive the first data I need from the third thread. So simultaneously I have to look for data. Yes I am probably creating hundreds of threads. So my question is how do I abort the threads or what is a better way of doing it?

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: cannot abort threads

    Are you reading from a serial port e.g. AxAComm1? If so why are you trying to do it with multipile threads? Either I missed something or this is an odd way to do it seeing as how data will come into the port as a stream.

    As for your thread not being aborted could be because you are starting a new thread within the thread. Why? Why not just loop back to the top of the routine if you need to execute the code again or several times.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 1999
    Location
    Ohio, USA
    Posts
    163

    Re: cannot abort threads

    Yes, I am reading data from a serial port. AxAComm1 which is an activeX control does not support multithreading and the company is out of business too. I could have easily used functions but it doesn't work. The biggest problem I have is I don't know when I am getting the data and to which thread (1 through 5) I am getting the data. That's why I have SyncLock statements. The ActiveX control is cable of reading only one piece of data at a time. If you notice I have a Thread.Sleep(3000) also, which I use to wait 3 seconds to avoid double reads.

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: cannot abort threads

    Hmm I really don;t understand a need for multi threading when dealing with a comm port. There is a reason why the control does not support multi threading because the port does not support it either.

    Sounds like you are making this much more complicated than it needs to be and is the source of your problems. Depending on what you are doing with the data it is quite possible that you do not even need the 3rd party control as VB2005 comes with a serial port class object which works rather well.

    That said only one device can be connected to a given com port and any data comes into that port as a single stream and can be handled just fine by a single thread. I've been doing this for a lot of years and have never saw any case where I needed additional threads to process a single stream. Now if you are using more than one port you should set up an object for each port still no need for more than one thread in most cases. I deal with up to 4 quite often on the same thread and never had an issue though it would work fine with one thread per port as well.

    All you really need in most cases is to Dim with events an instance of the System.IO.Ports.SerialPort set the properties and place some code in the datareceived event.
    or you can also use a timer and check the in buffer of the port and read when there is the expected number of bytes there. Either method should work with most 3rd party tools as well.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured