473,698 Members | 2,246 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(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 "StartTimin g" 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 "mBECardReaderM anager" - an object created by a
third DLL and used by the main application - the handler correctly executes
the "StartTimin g" 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(ByV al LightNumber As Integer, ByVal Duration As
Integer)
Dim pBELight As BELight
pBELight = list.Item(Light Number - 1)
pBELight.StartT iming(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.LightC hanged, AddressOf LightChanged
AddHandler pBELight.TimesU p, AddressOf LightTimesUp
pBELight.LightN umber = intCount
pBELight.Reset( )
list.Add(pBELig ht)
Next
End Sub

Private Sub LightTimesUp(By Val LightNumber As Integer)
RaiseEvent TimesUp(LightNu mber)
End Sub

Private Sub LightChanged(By Val LightNumber As Integer, ByVal LightStatus
As BELight.BELight Status)
'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_Times Up(ByVal LightNumber As Integer) Handles
mBELights.Times Up
RaiseEvent TimesUp(LightNu mber)
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 "ManuallyStartT ime" (see below for
handler)
'****ManuallySt artTime is fired as the result of the user selecting an
option from a ContextMenu
'****that is associated with Seat1

'****BECardRead erManager comes from BELib.dll
Public WithEvents mBECardReaderMa nager As New BECardReaderMan ager
Public WithEvents mBEDIO As New BEDIO.BEDIO

'****No threading problem in the event handler for mBECardReaderMa nager
'****mBEDIO.Lig hts.StartTiming works fine (the timing light comes on)
Private Sub mBECardReaderMa nager_BECardIns erted(ByVal eCardReaderName As
String, ByVal eCardReaderNumb er As Integer, ByVal eCardID As String) Handles
mBECardReaderMa nager.BECardIns erted
mBEDIO.Lights.S tartTiming(eCar dReaderNumber, 240)
End Sub

'>>>>>mBEDIO.Li ghts.StartTimin g does not work here<<<<<
'****(This sub actually handles many more SeatX.ManuallyS tartTime events
than just the one shown)
Private Sub ManuallyStartTi me(ByVal Seat As BESeat) Handles
Seat1.ManuallyS tartTime
'Seat is a passed instance of Seat1, the object that fires the event
mBEDIO.Lights.S tartTiming(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 1481
"BoloBaby" <bo******@hotma il.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 "StartTimin g" 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 "mBECardReaderM anager" - an object created by
a third DLL and used by the main application - the handler correctly
executes the "StartTimin g" 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.InvokeReq uired 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.S tartTiming 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.InvokeReq uired 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
1473
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 remote hosts (say primary remote host, holding procValues, 4 CPU floating numbers per n remote hosts). Now, I want to transfer this data set (double procValues) over a TCP or UDP network connection to a local host for graphing and storing in a...
13
4116
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
1476
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. : MyApp.exe "Go"
11
1935
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 - over and above CLI, CTS etc) 2). Knows which libraries to use for common and maybe not so common tasks 3). Can convincingly pass a C# technical interview (without having
22
2763
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 cropped up before, since I rarely use "float"s for anything, and hardly ever use the function for floating-point numbers in the first place; I just was messing around testing it for all cases and noticed a problem.) Anyway, it is declared and I...
0
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9027
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.