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

Thread safety questions

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 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 function 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?

using System.Threading;
public class Listener
{
public delegate void DataArrivedEventHandler();
private DataArrivedEventHandler DataArrivedEvent;

public static event DataArrivedEventHandler DataArrived
{
add {Delegate.Combine(DataArrivedEvent, value);}
remove {Delegate.Remove(DataArrivedEvent, value);}
}

private static Thread oThread;
public static void Listen ()
{
oThread = new Thread(new System.Threading.ThreadStart(
StartListening));
oThread.Name = "BOB";
oThread.Start();
}

private static void StartListening ()
{
do {
//blocking call
if (IsDataAvailable())
DataArrivedEvent ();
} while (true);
}
}

public class CallerClass
{
//The Application starts here.
public void Main ()
{
Thread.CurrentThread.Name = "MAIN";
Listener.DataArrived += new
Listener.DataArrivedEventHandler(HandleData);
}

private void HandleData ()
{
//lengthy database operation
}
}
Nov 16 '05 #1
2 1353
Frank Rizzo <no**@none.com> wrote:
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 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 function 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?
It's in the new thread. Calling a delegate (in the normal way) doesn't
do anything magical thread-wise.
2.When I run my lengthy database operation in HandleData, what is
blocked? The main application thread or the new BOB thread?
The new thread, as per the answer to question 1.
Also, will I
be able to still receive data in my BOB thread, when the lengthy
database operation is performed?
Well, you won't be able to do anything else at the same time in the
same thread. The data will hopefully be queued - but that depends on
what your data source is.
3.Is this type of code safe?
4.Is it good practice? If not, what are the alternatives?


Having a loop that never exits is a bad idea IMO. Your event add/remove
code isn't thread-safe. You haven't given any reason to keep a
reference to the new thread. Other than that, it seems okay.

See http://www.pobox.com/~skeet/csharp/multithreading.html for more
information on threading in general though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
As a side note, here is an alternate method that uses bounded blocking
queues instead of events. Also is a simple Listener and Server to network
send/receives and flow of packets between listener thread and server thread.
Server could have web methods, remoting, etc that allows gui mgr client or
console mgr client to monitor server, etc

--
William Stacey, MVP

"Frank Rizzo" <no**@none.com> wrote in message
news:eY**************@TK2MSFTNGP11.phx.gbl...
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 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 function 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?

using System.Threading;
public class Listener
{
public delegate void DataArrivedEventHandler();
private DataArrivedEventHandler DataArrivedEvent;

public static event DataArrivedEventHandler DataArrived
{
add {Delegate.Combine(DataArrivedEvent, value);}
remove {Delegate.Remove(DataArrivedEvent, value);}
}

private static Thread oThread;
public static void Listen ()
{
oThread = new Thread(new System.Threading.ThreadStart(
StartListening));
oThread.Name = "BOB";
oThread.Start();
}

private static void StartListening ()
{
do {
//blocking call
if (IsDataAvailable())
DataArrivedEvent ();
} while (true);
}
}

public class CallerClass
{
//The Application starts here.
public void Main ()
{
Thread.CurrentThread.Name = "MAIN";
Listener.DataArrived += new
Listener.DataArrivedEventHandler(HandleData);
}

private void HandleData ()
{
//lengthy database operation
}
}


Nov 16 '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...
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.&nbsp; Data can come in on this resource any time, so I have to listen for it the entire time.&nbsp; I wanted to see if what I have is...
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...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.