473,385 Members | 1,769 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.

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.                 &n bsp; 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.                 &n bsp; 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.                 &n bsp; Is this type of code safe?
4.                 &n bsp; 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()
        &n bsp;   oThread = New Thread(AddressOf StartListening)
        &n bsp;   oThread.Name = “BOB”
        &n bsp;   oThread.Start()
      End Sub
 
      Private Shared Sub StartListening()
        &n bsp;   ‘blocking call
        &n bsp;   Do
        &n bsp;   If IsDataAvailable() Then
        &n bsp;       &nbs p; RaiseEvent DataArrived()
        &n bsp;   End If
        &n bsp;   Loop
      End Sub
End Class
 
Public Class Form1()
      ‘The Application starts here.
      Shared Sub Main()
        &n bsp;   Thread.CurrentThread.Name = “MAIN”
        &n bsp;   AddHandler Listener.DataArrived, AddressOf HandleData
      End Sub
 
      Private Sub HandleData()
        &n bsp;   ‘lengthy database operation
      End Sub
End Class
 
'///Thank you.    
Nov 21 '05 #1
2 1390
* Frank Rizzo <no**@none.com> scripsit:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Can you repost your message in text/plain format?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Herfried K. Wagner [MVP] wrote:
* Frank Rizzo <no**@none.com> scripsit:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


Can you repost your message in text/plain format?

Oops, sorry. Message reposted to the NG.
Nov 21 '05 #3

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...
9
by: Alexander Fleck | last post by:
Hi, I' ve to make a software module thread safe. I know how to realize that and what' re the main topics of thread safety. But I don' t know how thread safety can be tested. I read about a test...
2
by: Frank Rizzo | last post by:
I tried it on dotnet.vb, but got no answers. I'll try it here now. 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...
0
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 thread-safe and...
4
by: Warren Sirota | last post by:
Hi, I've got a method that I want to execute in a multithreaded environment (it's a specialized spider. I want to run a whole bunch of copies at low priority as a service). It works well running...
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...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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: 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
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...

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.