Solved

Threading in Vb.Net 2008

Posted on 2009-04-10
7
164 Views
Last Modified: 2013-11-26
Hi Experts,
I hav a problem with threads.

I have a function which reads data from a Multicast group. All this data is gathered in some buffers. I then need to access the buffer through another function. Once the buffer is accesed, I need to empty those buffers. The Thread is just reading data all the time and putting it into a buffer. I have tried to pause the thread but with no luck.

What / how would be the best way? I have attached some of the code.
Public Sub StartReadThread() 
      m_thread = New Thread(AddressOf ReadData)
       m_thread.IsBackground = True
       m_thread.Start()
End Sub
 
Private Shared Sub ReadData()
'Do something to recieve data into some Buffer(s)
End Sub
 
Public function GetBuffer(mydata() as byte) as byte
dim myBuf as byte()
myBuf = myReadBuf
redim myReadBuf(0)
return myBuf
end funtion

Open in new window

0
Comment
Question by:ismohamed
  • 4
  • 3
7 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 24117222
You can use SyncLock to keep the thread from adding data while GetBuffer() is accessing the buffer...and conversely keep GetBuffer() from accessing the buffer while the thread is adding to it:
http://msdn.microsoft.com/en-us/library/3a86s51t(VS.80).aspx

Simple example:
Private MySync As New Object
 
Private Shared Sub ReadData()
    While True
        SyncLock MySync ' <-- code will STOP here and wait for GetBuffer() to finish if necessary
 
            ' ...update the buffer in here...
 
        End SyncLock
 
        ' ...while here, before the next iteration in the while loop,
        ' the GetBuffer() function below will have a chance to gain control of "MySync" and access the buffer
 
    End While
End Sub
 
Public function GetBuffer(mydata() as byte) as byte
    Dim myBuf as byte()
 
    SyncLock MyLock ' <-- code will STOP here and wait for ReadData() to finish if necessary
        myBuf = myReadBuf
        redim myReadBuf(0)
    End SyncLock
 
    Return myBuf
End Function

Open in new window

0
 

Author Comment

by:ismohamed
ID: 24117956
Hi Idle mind,

I think the above solution worked and I am going to award you the points, thank you for that.

But, what I have done is the above code is running in a DLL. This DLL is used in a Delphi project. Whenever I call the GetBuffer Function from the Delphi Application the application halts. If I run the dll in a VB.NET project everything is fine. Any suggestions?
0
 

Author Comment

by:ismohamed
ID: 24118175
Hi Idle,

Just to be sure, the code you attached, in the GetBuffer you still mean to refer to MySync and not MyLock right? I do not see you declaring MyLock anywhere, that is why I am asking.

Forget above comment, i had some code error regarding the DLL, but still in Delphi I cannot get it to sync and in VB.net I can when using my DLL
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24118290
Yes...sorry.  There's a typo in there (serious pitfall of coding in the browser).
0
 

Author Comment

by:ismohamed
ID: 24119266
Idle mind,

Ok I have narrowed down the error. Is there anyway I can safely handle the thread in Getbuffer. The reason I am asking is that when I  start the thread in my DLL, at some poin the Delphi code comes up with a runtime error. This has definately something to do with the Thread. The SyncLock, I assume is some sort of a pause. Is there anything else I can do to ensure that the thread is not ongoing?

I get a runtime error 7c812aebin Delphi.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24119349
In lieu of SyncLock, I think you could setup some boolean flags that you manually toggle and check in your loop/functions...


0
 

Author Comment

by:ismohamed
ID: 24130711
Idle mind,

SyncLock worked as I previously said perfectly and you have been awarded the points.
 
The problem I was running into was when I was running the code as a DLL and using the DLL in Delphi, I got a runtime error. I tried to add the boolean flags (also before posting the code), with no help.
I ended up changing the GetBuffer function to a procedure instead as this is what was causing the runtime error in Delphi and my DLL. I guess Delphi does not like functions and threads too well.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question