473,394 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

(Expert question) How to make this thread-safe?

All,

I believe I am having a threading problem. Class "BELights" is part of a
larger DLL that is used by my main application. A user control (of type
BESeat) within the main application raises an event and attempts to execute
the "StartTiming" method of BELights. The method does not execute which
leads me to believe that BELights is not thread-safe. (Or is it the user
control that is not thread safe?)

When an event fires for "mBECardReaderManager" - an object created by a
third DLL and used by the main application - the handler correctly executes
the "StartTiming" method of BELights. There doesn't seem to be any
threading issue in that part of the program, only between the user control
and BELights.

I've Googled this problem many times over and can't seem to find an answer
that makes any sense to me, therefore I'll ask here. How do I make BELights
thread-safe so that the desired method executes properly.

"Simplified" sample code follows with the relevant parts included.

'*****
'Pared down version of collection class - assume it is much more complicated
but that these are the relevant parts
'BELights is located in BEDIO.dll
'*****

Public Class BELights
Inherits CollectionBase

Public Event TimesUp(ByVal LightNumber As Integer)

Public Sub StartTiming(ByVal LightNumber As Integer, ByVal Duration As
Integer)
Dim pBELight As BELight
pBELight = list.Item(LightNumber - 1)
pBELight.StartTiming(Duration)
End Sub

Public Sub New()
Dim intCount As Integer
Dim pBELight As BELight
'add the lights
For intCount = 1 To 14
pBELight = New BELight
'add handlers for the two BELight events
AddHandler pBELight.LightChanged, AddressOf LightChanged
AddHandler pBELight.TimesUp, AddressOf LightTimesUp
pBELight.LightNumber = intCount
pBELight.Reset()
list.Add(pBELight)
Next
End Sub

Private Sub LightTimesUp(ByVal LightNumber As Integer)
RaiseEvent TimesUp(LightNumber)
End Sub

Private Sub LightChanged(ByVal LightNumber As Integer, ByVal LightStatus
As BELight.BELightStatus)
'do some irrelevant code...
End Sub

End Class

'*****
'BELights is accessed through this class which is included in BEDIO.dll
'Again, this class is simplified
'*****

Public Class BEDIO
Private WithEvents mBELights As New BELights

Public Event TimesUp(ByVal LightNumber As Integer)

Public ReadOnly Property Lights() As BELights
Get
Return mBELights
End Get
End Property

Private Sub mBELights_TimesUp(ByVal LightNumber As Integer) Handles
mBELights.TimesUp
RaiseEvent TimesUp(LightNumber)
End Sub

End Class

'*****
'My main application contains this code
'frmMain is the startup object
'*****

Public Class frmMain
Inherits System.Windows.Forms.Form

'****This form contains an instance of the user control type BESeat,
known as Seat1
'****Seat1 fires an event called "ManuallyStartTime" (see below for
handler)
'****ManuallyStartTime is fired as the result of the user selecting an
option from a ContextMenu
'****that is associated with Seat1

'****BECardReaderManager comes from BELib.dll
Public WithEvents mBECardReaderManager As New BECardReaderManager
Public WithEvents mBEDIO As New BEDIO.BEDIO

'****No threading problem in the event handler for mBECardReaderManager
'****mBEDIO.Lights.StartTiming works fine (the timing light comes on)
Private Sub mBECardReaderManager_BECardInserted(ByVal eCardReaderName As
String, ByVal eCardReaderNumber As Integer, ByVal eCardID As String) Handles
mBECardReaderManager.BECardInserted
mBEDIO.Lights.StartTiming(eCardReaderNumber, 240)
End Sub

'>>>>>mBEDIO.Lights.StartTiming does not work here<<<<<
'****(This sub actually handles many more SeatX.ManuallyStartTime events
than just the one shown)
Private Sub ManuallyStartTime(ByVal Seat As BESeat) Handles
Seat1.ManuallyStartTime
'Seat is a passed instance of Seat1, the object that fires the event
mBEDIO.Lights.StartTiming(Seat.LightNumber, 240)
End Sub

End Class
WHEW...

OK, if you made it that far, you are a real trooper. Also, I'd hope that my
problem is fairly clear. I don't have experience in making stuff like this
thread-safe... maybe you do. Any help would be greatly appreciated.

Thanks,

Gardner
Nov 20 '05 #1
3 1464
"BoloBaby" <bo******@hotmail.com> schrieb
All,

I believe I am having a threading problem. Class "BELights" is part
of a larger DLL that is used by my main application. A user control
(of type BESeat) within the main application raises an event and
attempts to execute the "StartTiming" method of BELights. The method
does not execute which leads me to believe that BELights is not
thread-safe. (Or is it the user control that is not thread safe?)

When an event fires for "mBECardReaderManager" - an object created by
a third DLL and used by the main application - the handler correctly
executes the "StartTiming" method of BELights. There doesn't seem to
be any threading issue in that part of the program, only between the
user control and BELights.

I've Googled this problem many times over and can't seem to find an
answer that makes any sense to me, therefore I'll ask here. How do I
make BELights thread-safe so that the desired method executes
properly.

"Simplified" sample code follows with the relevant parts included.
[...]


I've been reading the code and tried to understand, but my main question
remains: Where do you create and start a thread?? I don't see it.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
> I've been reading the code and tried to understand, but my main question
remains: Where do you create and start a thread?? I don't see it.


Armin,

I don't explicitly start any additional threads. I've encountered a
similiar instance like this in the past where controls and classes from
different DLLs/EXEs seem to operate in their own, non-explicitly created
threads.

In fact, in another part of this application (a different EXE), I was adding
and removing user controls to a panel object. While doing so, I noticed
that the Panel.Controls.Add and Panel.Controls.Remove methods were both not
firing. I checked Panel.InvokeRequired and - sure enough - it was "True."

There isn't a lick of multi-threading code anywhere in my application, and
yet I seem to be having this problem. Perhaps each new instance of the user
control I am using creates a new thread for itself.

I can't figure any other reason for mBEDIO.Lights.StartTiming to NOT run
except that it is not thread-safe. No exceptions are being thrown, the
method simply fails to execute. As indicated in the sample code, the method
works just fine when called during a different event handler.
Seat1.InvokeRequired returns false, so I assume the problem is in BELights.

Maybe I'm confused. Is there some other reason why this method could
possibly fail to execute without any exceptions being thrown? The
parameters being passed to the method are exactly the same...

Gardner
Nov 20 '05 #3
Actually, now that I think about it, the BELight object does access a
digital i/o card's driver when StartTiming is called. Perhaps the card's
driver is somehow getting the whole process on a different thread...
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: KevinGPO | last post by:
Two quick queries: 1. I have programmed a little network performance monitor. It monitors a set of remote hosts over a network. It gets the CPU statistics and all gets collected at one of the...
13
by: aarklon | last post by:
Hi all, I accidentally found expert C programming by peter van der linden here:: http://sunner.cn/courses/C/ebook/Expert.C.Programming.pdf
13
by: Ghislain Tanguay | last post by:
I have a compiled vb.net app and I want to give the user a choice to launch it from the start line command and pass it a parameter or not. How can I do that in my code? Is it possible? Ex. :...
11
by: Neo Morpheous | last post by:
Ok, first lets start with some definitions: By "Expert", I mean someone who : 1). Is familiar with and understands the *MAJOR* concepts/philosopies underlying C# (and possible .Net as a whole...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.