Link to home
Start Free TrialLog in
Avatar of ismohamed
ismohamed

asked on

Threading in Vb.Net 2008

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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ismohamed
ismohamed

ASKER

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?
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
Yes...sorry.  There's a typo in there (serious pitfall of coding in the browser).
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.
In lieu of SyncLock, I think you could setup some boolean flags that you manually toggle and check in your loop/functions...


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.