-
Feb 2nd, 2023, 05:58 AM
#1
Thread Starter
New Member
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
-
Feb 2nd, 2023, 07:29 AM
#2
Junior Member
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?
-
Feb 2nd, 2023, 07:59 AM
#3
Thread Starter
New Member
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.....
-
Feb 2nd, 2023, 04:08 PM
#4
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.
-
Feb 2nd, 2023, 08:57 PM
#5
Thread Starter
New Member
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.....
-
Feb 3rd, 2023, 12:49 AM
#6
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?
-
Feb 4th, 2023, 03:05 AM
#7
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.
-
Feb 4th, 2023, 12:13 PM
#8
Thread Starter
New Member
Re: intptr in different threads
Thank you schoemr.
Can you send me a example?
-
Feb 4th, 2023, 12:51 PM
#9
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
-
Feb 4th, 2023, 01:01 PM
#10
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.
-
Feb 4th, 2023, 01:05 PM
#11
Re: intptr in different threads
Originally Posted by schoemr
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.
-
Feb 4th, 2023, 01:07 PM
#12
Re: intptr in different threads
Originally Posted by Niya
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
-
Feb 4th, 2023, 01:11 PM
#13
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.
-
Feb 4th, 2023, 01:58 PM
#14
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.
-
Feb 4th, 2023, 02:57 PM
#15
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.
-
Feb 4th, 2023, 03:05 PM
#16
Re: intptr in different threads
Hahahaha you're not wanted! 🤣🤣🤣🤣
This was so funny!!!
-
Feb 4th, 2023, 03:28 PM
#17
Re: intptr in different threads
Originally Posted by schoemr
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...
-
Feb 4th, 2023, 03:48 PM
#18
Thread Starter
New Member
Re: intptr in different threads
thank you all. I'll do some tests on monday
-
Feb 6th, 2023, 04:05 AM
#19
Re: intptr in different threads
Originally Posted by Niya
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|