Newbie Multi-Threading

M

Mark B

Hi there

I am using a component to check whether an email address is valid:
http://www.aspnetmx.com/demo.aspx

What I want to do is check 5 emails at once, using Vb.net "threads".

I have looked at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconfreethreading.asp
but are having difficulty as I haven't done threads before and there are no
examples there to do it.

My new .aspx page has 5 session variables:

Email01
Email02
Email03
Email04
Email05

What I want is:

1) To start 5 threads to validate to the 5 email addresses. The validation
call for each one is simply say:

If Mx.Validate(Session("Email01"))=True Then
bolValidEmail01 = True
Else
bolValidEmail01 = False
End If

2) If after 5 seconds all the threads haven't completed, then stop all
threads.

3) If all the threads have finished or 5 seconds have passed then move onto
4)

4) If less than 3 out of 5 emails are valid then set the variable
bolAlertUser to True.

Any help would be appreciated ...

Thanks
Mark
 
C

Cor Ligthert

Mark,

The time to start and stop the thread will be much more than checking those
emailaddresses one after each other in a loop, what is your idea behind
this.

Cor
 
K

Kevin Spencer

Hi Mark,

First, I'm not at all sure you need multi-threading. I checked out your
component (very good component BTW), and it checks an email address in a
matter of milliseconds. Therefore, checking 5 email addresses would take
less than a second. It would take longer to open a database connection.

Second, when you get into threading, you're in for a real headache,
especially when it comes to ASP.Net. Why? Well, first, what are you going to
do with regards to the response? You're still going to have to wait for your
threads to complete before sending the Response. After all, the Response is
supposed to tell the user whether the email addresses are correct or not.
Second, a child thread runs separately (asynchronously) from the main
thread, which makes communication between the main thread and spawned
threads problematic. In essence, they cannot "talk to" each other. The
spawned thread has access to the context of the main thread, so it can read
and set fields and properties in the main thread, but you run into problems
when multiple threads try to read and/or set the same field/properties at
the same time. This can result in race conditions (where you don't know
which thread got to a variable first, including the main thread), and
deadlocks (when a thread attempts to gain an exclusive lock on something,
which blocks another thread's execution until the object is released, and
the programmatic equivalent of gridlock occurs, causing your app to stall
without remedy). So, working with multi-threading is demanding both of your
mental resources, and your program's resources. With ASP.Net, you also have
the issue to deal with of your main thread having to stay alive until all
child threads are completed, or unexpected results can occur (such as the
child threads dying). An ASP.Net Page instance lives for a matter of
milliseconds, until shortly after the Response is sent.

That said, multi-threading CAN be useful in an ASP.Net app, as long as
you're familiar with all the issues involved, account for all the issues
involved, and use it only when necessary, which brings us back to my first
paragraph. Multi-threading can be useful when a long-running process must
occur, which the Page instance is not dependent upon. So, if you want to
learn more (and there are PLENTY of examples as well as articles and
tutorials on the MSDN site), check out the following link:

http://search.microsoft.com/search/results.aspx?qu=.Net+threading&View=msdn&st=b&c=4&s=1&swc=4

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.
 
M

Mark B

Thanks for all of that info.

I'd say 70% of the time it takes milliseconds to check an email address but
the other 30% can take a minute or longer. Here's a log file of one I did
yesterday. From it you can see in the timestamps there that there was a
delay of 23 seconds between "[ 19:35:13.265 ] -- Attempting SMTP Connection
and Mailbox Verification to mx1.hotmail.com --" and "[ 19:35:36.250 ] A
connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond.":

Results
======
(e-mail address removed) is not a valid email address.

MX Records
(Preference:Mail Exchanger)

5:mx1.hotmail.com
5:mx2.hotmail.com
5:mx3.hotmail.com
5:mx4.hotmail.com

Validation Log
===========

[ 19:35:13.250 ] -- Validating (e-mail address removed) to
MXValidateLevel.Mailboxlevel. --
[ 19:35:13.250 ] -- Checking Email Address (e-mail address removed) Syntax --
[ 19:35:13.250 ]The match was True
[ 19:35:13.250 ] -- Done Checking Email Address Syntax --
[ 19:35:13.250 ] -- Checking to see if (e-mail address removed) is in the list of
BadEmailAddresses. --
[ 19:35:13.250 ] -- (e-mail address removed) was not found in the list of
BadEmailAddresses --
[ 19:35:13.250 ] -- Extracting Domain Name from (e-mail address removed) --
[ 19:35:13.250 ]Domain Name: hotmail.com
[ 19:35:13.250 ] -- Done Extracting Domain Name from (e-mail address removed) --
[ 19:35:13.250 ] -- Checking to see if hotmail.com is in the list of
KnownDomains --
[ 19:35:13.250 ] -- hotmail.com was not found in the list of KnownDomains --
[ 19:35:13.265 ] -- Checking to see if hotmail.com is in the list of
MailboxDomains --
[ 19:35:13.265 ] -- hotmail.com was not found in the list of
MailboxDomains --
[ 19:35:13.265 ] -- Checking Domain Name and MX Records --
[ 19:35:13.265 ] -- MX Records Found --
[ 19:35:13.265 ] -- Attempting SMTP Connection and Mailbox Verification to
mx1.hotmail.com --
[ 19:35:36.250 ]A connection attempt failed because the connected party did
not properly respond after a period of time, or established connection
failed because connected host has failed to respond
[ 19:35:36.250 ] -- Attempting SMTP Connection and Mailbox Verification to
mx2.hotmail.com --
[ 19:35:36.296 ] -- Connected to mx2.hotmail.com --
[ 19:35:36.312 ]220 MC6-F19.hotmail.com Sending unsolicited commercial or
bulk e-mail to Microsoft's computer network is prohibited. Other
restrictions are found at http://privacy.msn.com/Anti-spam/. Violations will
result in use of equipment located in California and other states. Mon, 13
Jun 2005 19:36:18 -0700
[ 19:35:36.312 ]HELO
[ 19:35:36.328 ]250 MC6-F19.hotmail.com (3.0.1.19) Hello [216.176.194.4]
[ 19:35:36.328 ]MAIL FROM:<>
[ 19:35:36.343 ]250 <>....Sender OK
[ 19:35:36.343 ]RCPT TO:<[email protected]>
[ 19:35:36.359 ]550 Requested action not taken: mailbox unavailable
[ 19:35:36.359 ]QUIT
[ 19:35:36.375 ]221 MC6-F19.hotmail.com Service closing transmission channel
[ 19:35:36.375 ] -- Completed SMTP Connection and Unsuccessful Mailbox
Verification to mx2.hotmail.com --

I said I want to check 5 email addresses at once but really I want to check
20 ...

So then with the issue of some of them timing out, I thought if I could
verify 15 out of 20 then that ratio would be an acceptable level for our
particular purpose. In other words I could accept the probability that if 15
were OK then the others would be too. If any came back within the allocated
5 second time as 'Not A Valid Email Address", then the user could be alerted
to amend the particular email address and the webpage wouldn't advance.

So I'd say I need 20 threads which all get aborted after 5 seconds (user
wait time) if they all don't finish before then. Then a tally could be made
of the 20 "True" or "False" or "Not Finished" results to see whether the
user needs to be alerted or whether they can go on to the next page.

So the VB.Net code could be:

Dim intResult(20) as Integer = 1
Dim i as Integer
'intResult(i) 1 = Not Finished, 2 = True , 3 =False

For i= 1 To 20

'*******Start thread 01 here
If Mx.Validate(Session("Email"+Format$(i,"00")))=True Then
intResult(i)= 2
Else
intResult(i)= 3
End If
' *********End thread 01 here

Next i

I guess I need to figure out how to:

1) Do ******* Start thread 01 here
2) Do ******** End thread 01 here
3) Do the main engine code:

Start all threads
Pause 5 Seconds
End All threads
Inspect the 20 intResult(i)'s
If more then 15 intResult(i)'s = 2 (e.g. True) then progress to next web
page.



Any help with syntax fro 1) to 3) would be appreciated ...



Kevin Spencer said:
Hi Mark,

First, I'm not at all sure you need multi-threading. I checked out your
component (very good component BTW), and it checks an email address in a
matter of milliseconds. Therefore, checking 5 email addresses would take
less than a second. It would take longer to open a database connection.

Second, when you get into threading, you're in for a real headache,
especially when it comes to ASP.Net. Why? Well, first, what are you going to
do with regards to the response? You're still going to have to wait for your
threads to complete before sending the Response. After all, the Response is
supposed to tell the user whether the email addresses are correct or not.
Second, a child thread runs separately (asynchronously) from the main
thread, which makes communication between the main thread and spawned
threads problematic. In essence, they cannot "talk to" each other. The
spawned thread has access to the context of the main thread, so it can read
and set fields and properties in the main thread, but you run into problems
when multiple threads try to read and/or set the same field/properties at
the same time. This can result in race conditions (where you don't know
which thread got to a variable first, including the main thread), and
deadlocks (when a thread attempts to gain an exclusive lock on something,
which blocks another thread's execution until the object is released, and
the programmatic equivalent of gridlock occurs, causing your app to stall
without remedy). So, working with multi-threading is demanding both of your
mental resources, and your program's resources. With ASP.Net, you also have
the issue to deal with of your main thread having to stay alive until all
child threads are completed, or unexpected results can occur (such as the
child threads dying). An ASP.Net Page instance lives for a matter of
milliseconds, until shortly after the Response is sent.

That said, multi-threading CAN be useful in an ASP.Net app, as long as
you're familiar with all the issues involved, account for all the issues
involved, and use it only when necessary, which brings us back to my first
paragraph. Multi-threading can be useful when a long-running process must
occur, which the Page instance is not dependent upon. So, if you want to
learn more (and there are PLENTY of examples as well as articles and
tutorials on the MSDN site), check out the following link:

http://search.microsoft.com/search/results.aspx?qu=.Net+threading&View=msdn&st=b&c=4&s=1&swc=4

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top