473,325 Members | 2,860 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,325 software developers and data experts.

.Start() Spawning two consecutive threads instead of one?

Hi All,

I'm using vb.net as my codebehind lang. and the following code is
being executed in my aspx.vb page to stamp a DB row.

Dim oStatsInfo As New StatsInfo(CartID, Batch, Set, Num, 0, 0, 0, 1,
0, 1)
Dim oStatsStamp As New Stats(_DataSource, oStatsInfo)
Dim tStamp As New Threading.Thread(AddressOf
oStatsStamp.StampStatsList)
tStamp.Start()

The code calls a stored proc which stamps a row in the database with
the values for the StatsInfo class using an UPDATE statement. If no
rows are affected (@@ROWCOUNT = 0) then a new row is added to the
database using an INSERT.

Now what is weird is that on rare occasions it looks like .Start() is
spawning to consecutive threads instead of one. I say this because two
rows end up in the database with consecutive ID numbers (generated
automaticly as the PK for the row).

So it looks like two threads get created they both go to call the SP
and while the 1st thread is getting ready to run the INSERT (because
there are no rows yet) the second thread calls the SP, it tries the
UPDATE, sees no rows (because thread 1 hasn't finished the INSERT
yet), and runs the INSERT as well.

Does anyone know if it's possible for the .Start call to accidentally
create two threads? Is that even what is happening.

Thanks for any help you can offer.

Cheers,

Mike Gorgone
Senior Software Engineer
PictureU
mg******@pictureu.com
Dec 7 '07 #1
1 1559
end:no it won't start two threads, but unless you have a thread join in the
page processing, two request can start the thread (if session used, if no
session the join will have no impact).

you should change your sqlcode to be thread safe. either change to isolation
level serializable, or detect the insert.

retry:
update table
where not exisits (select * where ..)
if @@rowcount ==0
begin
insert table
select <values>)
where not exists (select * where ...)
if @@rowcount = 0 goto retry
end

ps: in sql2008 you can just use the new merge statement

-- bruce (sqlwork.com)

-- bruce (sqlwork.com)
"Mike" wrote:
Hi All,

I'm using vb.net as my codebehind lang. and the following code is
being executed in my aspx.vb page to stamp a DB row.

Dim oStatsInfo As New StatsInfo(CartID, Batch, Set, Num, 0, 0, 0, 1,
0, 1)
Dim oStatsStamp As New Stats(_DataSource, oStatsInfo)
Dim tStamp As New Threading.Thread(AddressOf
oStatsStamp.StampStatsList)
tStamp.Start()

The code calls a stored proc which stamps a row in the database with
the values for the StatsInfo class using an UPDATE statement. If no
rows are affected (@@ROWCOUNT = 0) then a new row is added to the
database using an INSERT.

Now what is weird is that on rare occasions it looks like .Start() is
spawning to consecutive threads instead of one. I say this because two
rows end up in the database with consecutive ID numbers (generated
automaticly as the PK for the row).

So it looks like two threads get created they both go to call the SP
and while the 1st thread is getting ready to run the INSERT (because
there are no rows yet) the second thread calls the SP, it tries the
UPDATE, sees no rows (because thread 1 hasn't finished the INSERT
yet), and runs the INSERT as well.

Does anyone know if it's possible for the .Start call to accidentally
create two threads? Is that even what is happening.

Thanks for any help you can offer.

Cheers,

Mike Gorgone
Senior Software Engineer
PictureU
mg******@pictureu.com
Dec 7 '07 #2

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

Similar topics

2
by: Dave Kirby | last post by:
I am working on a network management program written in python that has multiple threads (typically 20+) spawning subprocesses which are used to communicate with other systems on the network. This...
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
3
by: choyk1 | last post by:
As shown below, the genetic algorithm is applied to consecutive objects The next calculation should not begin until the previous calculation ends However, without any code between ....,...
1
by: Novice | last post by:
Hi all, I have written some code that invokes a method a few hundred times and then it immediately finishes. I would like it to instead: 1. start the hundreds of threads (asynchronously) 2....
1
by: Sanjay Kedare | last post by:
Hi, How safe it is to spawn a thread to do some routine job from Application_Start event of ASP.NET Also whenever the ASPNET worker process restarts, will the Application_Start event be...
7
by: Mark B | last post by:
Hi I was wondering if someone could help me with the syntax for some VB.Net ASPX code to: - Start 20 threads - End all threads after the sooner of 10 seconds or if all of the threads...
5
by: Jeremy | last post by:
I have a core VB service that monitors a database, and based on data in the records will execute code to send email notifications. Problem: I don't want my main program code to halt and wait for...
37
by: ales | last post by:
Hello, I have a problem with creation of new thread. The method .Start() of newly created thread delays current thread for 0 - 1 second. Cpu while delay occurs is about 5%. Any idea? Here...
0
by: Mike | last post by:
Hi All, I'm using vb.net as my codebehind lang. and the following code is being executed in my aspx.vb page to stamp a DB row. Dim oStatsInfo As New StatsInfo(CartID, Batch, Set, Num, 0, 0, 0,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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....
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...

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.