Solved

Threading in Vb.Net 2008

Posted on 2009-04-10
7
163 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 37
fso.FolderExists("\\server\HiddenFolder$") 4 49
Server Error 11 47
How useful is the free version of Selenium? 3 8
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 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