Proper Use of Threading and Mutex

J

Jon Person

I'm currently writing a CF application which uses threads to work with a
buffer. The first thread appends to a String variable named Buffer, and the
second thread processes Buffer and removes processed data from it as well,
similar to this (simplified):

[Class1]
Sub Thread1Process()
' Add new data to the buffer
Do While IsSupposedToRun
' Get new data
' ...
' Notify of the new data
RaiseEvent NewDataAvailable(Data)
Loop
End Sub

[Class2]
Private Buffer As String

' Raised when new data is available
Sub NewDataAvailable(ByVal Data As String) Handles Class1.NewDataAvailable
' Append the new data to the buffer
Buffer = Buffer & NewData
End Sub

' Processes the contents of the Buffer string
Sub Thread2Process()
Do While IsSupposedToRun
Try
' Process the buffer
' ...
' Notify of various events
RaiseEvent ProcessedData()
' Remove the processed data from the buffer
Buffer = Buffer.SubString(10)
Catch ex As Exception
Stop
End Try
Loop
End Sub

When I set these threads to normal priority, I get a lockup almost
instantly. Setting the threads to "Lowest" priority makes them work a while
longer, but the lockup still occurs. When I set a breakpoint inside the
loops, it appears that the first thread (Thread1Process) is still chugging
away properly, but the second thread has either exited or been suspended,
yes no exception was thrown.

I'm suspecting that the problem is being caused by my lack of experience
with proper threading. I'm guessing that, for one, I may have to use a
Mutex before and after modifying the Buffer variable so only one thread can
access it at a time.

Would someone please help me find out if I'm missing some important
threading best practices with this code, and perhaps offer some suggestions
as to why else it is unstable? This would help me isolate the problem
further.

Thanks in advance!

Jon
 
J

Jon Person

After further reading through the groups, it appears that the problem is
caused by the fact that my calling application is not using the Invoke
method to update the UI.

In the Desktop Framework version of my component, I didn't have to use
Invoke. Does the Desktop Framework have some integrated handling of
updating a form on the proper thread? Or is this blind luck? :)

Thanks,

Jon
 
J

Jon Skeet [C# MVP]

Jon Person said:
After further reading through the groups, it appears that the problem is
caused by the fact that my calling application is not using the Invoke
method to update the UI.

In the Desktop Framework version of my component, I didn't have to use
Invoke. Does the Desktop Framework have some integrated handling of
updating a form on the proper thread? Or is this blind luck? :)

Blind luck. *Always* update forms on the UI thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top