Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Threading in Vb.Net 2008

Posted on 2009-04-10
7
Medium Priority
?
177 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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