Solved

Threading in Vb.Net 2008

Posted on 2009-04-10
7
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
LVL 86

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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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