Results 1 to 19 of 19

Thread: intptr in different threads

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    intptr in different threads

    Hi
    I have a problem with my application.
    I have developer a application to view pictures from 6 vision sensors via ethernet.
    I use a proprietary sensor cost library. The function for request a picture return INTPTR and Sizeof.i draw a image in 6 deifferent panel
    I created 6 threads,1 for each sensor. If a call the function in only one thread it's ok. When i call 6 different function and connection in 6 different threads, very often I see the same picture in different panel.

    this is a same code for the different threads

    dim ptrBuffer_1 as IntPtr
    Dim totBufSize As Integer = 0
    ptrBuffer_1 = New IntPtr
    Dim getInspResult As Integer = vsdConnection(0).GetDebugResult(ptrBuffer_1, totBufSize)

    Dim buffer(totBufSize) As Byte
    Marshal.Copy(ptrBuffer_1, buffer, 0, totBufSize)

    Dim bmp As Bitmap = CreateBitmap(buffer, 640, 480)
    imagePanel(0).DrawContent(bmp) //this is a function for draw picture in the panel for thread sensor 1.

    imagePanel(0) it's for sensor 1 and thread 1
    imagePanel(1) it's for sensor 2 and thread 2
    ecc.....

    where am i wrong?
    Thank's

    N.B.. sorry for my english

  2. #2
    Junior Member
    Join Date
    Oct 2017
    Posts
    21

    Re: intptr in different threads

    Where is your buffer variable coming from? Is it possible that it is overwritten with the bitmap bytes from another thread?

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: intptr in different threads

    dim ptrBuffer_1 as IntPtr
    Dim totBufSize As Integer = 0
    ptrBuffer_1 = New IntPtr
    Dim getInspResult As Integer = vsdConnection(0).GetDebugResult(ptrBuffer_1, totBufSize)

    Dim buffer(totBufSize) As Byte
    Marshal.Copy(ptrBuffer_1, buffer, 0, totBufSize)

    Dim bmp As Bitmap = CreateBitmap(buffer, 640, 480)
    imagePanel(0).DrawContent(bmp) //this is a function for draw picture in the panel for thread sensor 1.

    imagePanel(0) it's for sensor 1 and thread 1
    imagePanel(1) it's for sensor 2 and thread 2
    ecc.....

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    If that code is exact for each thread I'm not surprised it doesn't work. I see nothing in that code that attempts to match the sensors to different panels. It just looks like everything goes in one panel.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: intptr in different threads

    the panel is different for each thread

    imagePanel(0) it's for sensor 1 and thread 1
    imagePanel(1) it's for sensor 2 and thread 2
    ecc.....

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    What about the sensor. Did you make sure that the code for each panel was receiving from the correct sensor?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Hi masterofplc,


    So you have a program that shows pictures from 6 different cameras, right?

    You created 6 parts (thread) of your program each for a different camera.
    Your idea is that each part gets the picture from its camera and shows it on its own panel.

    So now happens sometimes the same picture show up on different panels

    Why? because all 6 parts are trying to get pictures from the cameras at the same time and they can be interfering with each other.

    To resolve this problem you can add a special thing called a "lock". (Synclock)

    This lock makes sure that only one part can get a picture from the cameras at a time. Think of him as "Gatekeeper"

    This way, each part can get its picture without interfering with the others and we avoid having the same picture show up on different panels.

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: intptr in different threads

    Thank you schoemr.
    Can you send me a example?

  9. #9
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Code:
    Private Sub DrawImage(sensorId As Integer)
    
        Dim ptrBuffer As IntPtr
        Dim totBufSize As Integer = 0
        Dim buffer As Byte()
        Dim bmp As Bitmap
        Dim lock As New Object()
    
        ptrBuffer = New IntPtr
        Dim getInspResult As Integer = vsdConnection(sensorId).GetDebugResult(ptrBuffer, totBufSize)
    '----------------------------------------------------
    'here is the synchronization lock:
    '----------------------------------------------------
      SyncLock lock
            buffer = New Byte(totBufSize) {}
            Marshal.Copy(ptrBuffer, buffer, 0, totBufSize)
            bmp = CreateBitmap(buffer, 640, 480)
        End SyncLock
    '-----------------------------------------------------
    
    
        imagePanel(sensorId).DrawContent(bmp)
    End Sub

  10. #10
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Just don't ask me how the synclock arrange the threads and in what order. I don't know. Maybe someone have that technical knowledge. But it uses some criteria to ensure only one at a time can pass through the lock.
    Last edited by schoemr; Feb 4th, 2023 at 01:05 PM.

  11. #11
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    Quote Originally Posted by schoemr View Post
    Code:
    Private Sub DrawImage(sensorId As Integer)
    
        Dim ptrBuffer As IntPtr
        Dim totBufSize As Integer = 0
        Dim buffer As Byte()
        Dim bmp As Bitmap
        Dim lock As New Object()
    
        ptrBuffer = New IntPtr
        Dim getInspResult As Integer = vsdConnection(sensorId).GetDebugResult(ptrBuffer, totBufSize)
    '----------------------------------------------------
    'here is the synchronization lock:
    '----------------------------------------------------
      SyncLock lock
            buffer = New Byte(totBufSize) {}
            Marshal.Copy(ptrBuffer, buffer, 0, totBufSize)
            bmp = CreateBitmap(buffer, 640, 480)
        End SyncLock
    '-----------------------------------------------------
    
    
        imagePanel(sensorId).DrawContent(bmp)
    End Sub
    There is a minor issue with that code. If we're assuming DrawImage is being called by multiple threads, the lock object should not be inside the DrawImage function. It should be outside at a higher scope so that all the threads can lock on that single instance. As it is now each thread will be locking on a different object which would not synchronize.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  12. #12
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Quote Originally Posted by Niya View Post
    There is a minor issue with that code. If we're assuming DrawImage is being called by multiple threads, the lock object should not be inside the DrawImage function. It should be outside at a higher scope so that all the threads can lock on that single instance. As it is now each thread will be locking on a different object which would not synchronize.
    Yes! 🙂 I see now... Thanks Niya

  13. #13
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Niya,

    Like so?

    Code:
    
    Private Shared lock As New Object()
    
    Private Sub DrawImage(sensorId As Integer)
        Dim ptrBuffer As IntPtr
        Dim totBufSize As Integer
        Dim buffer As Byte()
        Dim bmp As Bitmap
    
        ptrBuffer = New IntPtr
        totBufSize = 0
        Dim getInspResult As Integer = vsdConnection(sensorId).GetDebugResult(ptrBuffer, totBufSize)
    '______________________________________
    'Here is the synclock 
    
        SyncLock lock
            buffer = New Byte(totBufSize) {}
            Marshal.Copy(ptrBuffer, buffer, 0, totBufSize)
            bmp = CreateBitmap(buffer, 640, 480)
        End SyncLock
    ' _______________________________________
    
        imagePanel(sensorId).DrawContent(bmp)
    End Sub
    Last edited by schoemr; Feb 4th, 2023 at 01:51 PM.

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    Yea like that. The object should always be such that it is the same instance being used for SyncLock blocks for all threads where such locking is needed. Declaring it at the class level is the most common way.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    @Masterofplc

    Also, you need to provide a more complete sample of your code. There's not really enough to tell exactly where your problem is. It could be synchronization or it could be something else completely.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Hahahaha you're not wanted! 🤣🤣🤣🤣

    This was so funny!!!

  17. #17
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: intptr in different threads

    Quote Originally Posted by schoemr View Post
    Hahahaha you're not wanted! 🤣🤣🤣🤣

    This was so funny!!!
    I seem to have a talent for somehow triggering people online even when saying the most inane things. A curse I guess

    Should have seen the mess I made on Twitter before they shut me up lol...
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  18. #18

    Thread Starter
    New Member
    Join Date
    Feb 2023
    Posts
    5

    Re: intptr in different threads

    thank you all. I'll do some tests on monday

  19. #19
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: intptr in different threads

    Quote Originally Posted by Niya View Post
    I seem to have a talent for somehow triggering people online even when saying the most inane things. A curse I guess

    Should have seen the mess I made on Twitter before they shut me up lol...
    Olympic level trouble maker you 🙂

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