473,385 Members | 1,814 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,385 software developers and data experts.

Thread safety questions (plain text repost)

I have an app that listen to data on a certain resource. Data can come
in on this resource any time, so I have to listen for it the entire
time. I wanted to see if what I have is thread-safe and makes sense.
Consider the code below. The app starts and creates code to handle an
even from the Listener class. The listener class creates a new thread
(will call it thread BOB) inside it that listens to data. When the data
arrives, it is received on the BOB thread. Then an event is fired, which
is then handled in the calling class (Form1 in this case). That is
followed by a lengthy database operation. My questions are as follows:

1.When event is fired, and Form1.HandleData sub kicks in, is it received
on the main application thread or the new BOB thread? If HandleData runs
on the main thread, where, when and how is the event marshalled between
threads?
2.When I run my lengthy database operation in HandleData, what is
blocked? The main application thread or the new BOB thread? Also, will I
be able to still receive data in my BOB thread, when the lengthy
database operation is performed?
3.Is this type of code safe?
4.Is it good practice? If not, what are the alternatives?

Imports System.Threading
Public Class Listener
Public Shared Event DataArrived()
Private Shared oThread As Thread

Public Shared Sub Listen()
oThread = New Thread(AddressOf StartListening)
oThread.Name = “BOB”
oThread.Start()
End Sub

Private Shared Sub StartListening()
‘blocking call
Do
If IsDataAvailable() Then
RaiseEvent DataArrived()
End If
Loop
End Sub
End Class

Public Class Form1()
‘The Application starts here.
Shared Sub Main()
Thread.CurrentThread.Name = “MAIN”
AddHandler Listener.DataArrived, AddressOf HandleData
End Sub

Private Sub HandleData()
‘lengthy database operation
End Sub
End Class

'///Thank you.
Nov 21 '05 #1
0 1143

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

Similar topics

3
by: Philip V Pham | last post by:
These questions apply to std vector, map, and cout: I am uncertain of the thread safety for reading/writing for std templates. I know if all threads are reading concurrently, it is thread...
3
by: Mike Brown | last post by:
I have questions about thread safety in the 'random' module. When using the random.Random class (be it Mersenne Twister or Wichmann-Hill based), is it sufficiently thread-safe (preserving entropy...
4
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
2
by: Frank Rizzo | last post by:
I have an app that listen to data on a certain resource.  Data can come in on this resource any time, so I have to listen for it the entire time.  I wanted to see if what I have is...
22
by: Brett | last post by:
I have a second thread, t2, that errors out and will stop. It's status is then "Stopped". I try to start t2 from thread 1, t1, by checking If t2.threadstate = "Stopped" Then t2.start() ...
7
by: intrader | last post by:
I have the following small classes: //----------------code--------------- using System; using System.Collections.Generic; using System.Text; namespace ValidatorsLibrary { public class...
10
by: Paul | last post by:
Hi all, All of the classes in my DAL are static, with constants defining the stored procedures and parameters. I've been having some problems with my site which makes me wonder if there's a...
2
by: Nuno Magalhaes | last post by:
Hello, Is there any keyword applicable to a class to make it thread-safe? Without having to put lock(this){} in all functions? Thanks.
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.