Solved

Threading in Vb.Net 2008

Posted on 2009-04-10
7
162 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now