Showing posts with label seperate. Show all posts
Showing posts with label seperate. Show all posts

Thursday, March 29, 2012

Seperate Content from Code

After nearing completion of my first full .Net web application I am left with a page consisitng of around 600 lines of code, approximately split half and half between VB script and HTML page content.

Can anyone point me in the right direction of the best way to encapsulate the VB code into its own seperate file and then rerefence this from the form in the HTML page?

Hi,

you could place the code into separate aspx.vb file into its own class deriving form System.Web.UI.Page. Then putting the aspx to inherit from this code-behind class (with Inherits directive) would work.

Say you have files

page1.aspx (important ones are CodeFile and Inherits attributes)

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" %>

...

page1.aspx.vb

Partial Class page1

Inherits System.Web.UI.Page

End Class

When you could put code into aspx.vb file into the class.

More information :http://www.asp.net/QuickStart/aspnet/doc/pages/codebehind.aspx

Of course if have code & logic which is such that it's common to more than one form, you might want to consider having a separate component (class library or in App_Code) where you could centralize logic that is needed in multiple places.


Thanks joteke, I think I'm slowly getting there.

So the .aspx file inherits the class that you create in the aspx.vb file ?

Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

What does the'AutoEventWireup' attribute do?


Also, do i need to import the namespaces in the aspx file or the aspx.vb file ?

duncanhall:

So the .aspx file inherits the class that you create in the aspx.vb file ?

Yes. Basically with partial classes in ASP.NET 2.0 (when using CodeFile attribute) a second partial class is created. This class contains control declarations for controls residing on aspx (removes the need for you to declare them on aspx.vb as you had to do with ASP.NEt v1.x).

These two partial classes are merged when compilation occurs and are used as base class for class which is created when the actual aspx is parsed and compiled (yes, basically the aspx itself)

duncanhall:



Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

Yes, note that the code-behind class is now equally part of your page.

duncanhall:


What does the'AutoEventWireup' attribute do?

It indicates whether Page's event are autowired. If enabled, it means that you have a method which matches Page_[EventName] or [ControlID]_[EventName] name so that method would automatically be assigned as event handler for the correspondng event of P?ager or the Control. Normally these event methods need to be wired explicitly.

See:ASP.NET Web Server Control Event Model


If code is in code-behind, then you import in code-behind.
Thanks a lot joteke, everything seems to make sense now, and my first app is pretty much finished!

Seperate c# script in a DLL file

Hi,
Can I seperate the following script block in a sperate DLL file instead of
embedded in aspx/ascx?
I know codebehind/src attribute can do it, but I found that it can't trigger
"Page_Load" event if I seperate and compile the code into DLL file
<script runat="server">
Sub Page_Load( )
'NB1.CreateBlankFile( )
End Sub
</script>
Sample Code:
<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. Register TagPrefix="aspnetian" Namespace="aspnetian"
Assembly="NavBar" %>
<html>
<head>
<script runat="server">
Sub Page_Load( )
'NB1.CreateBlankFile( )
End Sub
</script>
</head>
<body>
<table border="1" width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center" width="150">
<img src="http://pics.10026.com/?src=aspnetian.jpg"/>
</td>
<td align="center">
<h1>NavBar Control Client Page<h1>
</td>
</tr>
<tr>
<td width="150">
<form runat="server">
<aspnetian:NavBar id="NB1"
showdividers="False" runat="server">
<strong>Navigation Bar</strong>
<br/>
</aspnetian:NavBar>
</form>
</td>
<td>
This is where page content might be placed
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
</td>
</tr>
</table>
</body>
</html>First of all your script is a vb rather thean c# one.
Secondly, you need Inherits attribute.
Eliyahu
"RC" <rc@.gmail.com> wrote in message
news:u72Il9dWFHA.1040@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Can I seperate the following script block in a sperate DLL file instead of
> embedded in aspx/ascx?
> I know codebehind/src attribute can do it, but I found that it can't
trigger
> "Page_Load" event if I seperate and compile the code into DLL file
> <script runat="server">
> Sub Page_Load( )
> 'NB1.CreateBlankFile( )
> End Sub
> </script>
> Sample Code:
> <%@. Page Language="vb" %>
> <%@. Register TagPrefix="aspnetian" Namespace="aspnetian"
> Assembly="NavBar" %>
> <html>
> <head>
> <script runat="server">
> Sub Page_Load( )
> 'NB1.CreateBlankFile( )
> End Sub
> </script>
> </head>
> <body>
> <table border="1" width="100%" cellpadding="20" cellspacing="0">
> <tr>
> <td align="center" width="150">
> <img src="http://pics.10026.com/?src=aspnetian.jpg"/>
> </td>
> <td align="center">
> <h1>NavBar Control Client Page<h1>
> </td>
> </tr>
> <tr>
> <td width="150">
> <form runat="server">
> <aspnetian:NavBar id="NB1"
> showdividers="False" runat="server">
> <strong>Navigation Bar</strong>
> <br/>
> </aspnetian:NavBar>
> </form>
> </td>
> <td>
> This is where page content might be placed
> <br/><br/><br/><br/><br/><br/><br/><br/><br/>
> </td>
> </tr>
> </table>
> </body>
> </html>
>

Seperate c# script in a DLL file

Hi,

Can I seperate the following script block in a sperate DLL file instead of
embedded in aspx/ascx?

I know codebehind/src attribute can do it, but I found that it can't trigger
"Page_Load" event if I seperate and compile the code into DLL file

<script runat="server">
Sub Page_Load( )
'NB1.CreateBlankFile( )
End Sub
</script
Sample Code:
<%@dotnet.itags.org. Page Language="vb" %>
<%@dotnet.itags.org. Register TagPrefix="aspnetian" Namespace="aspnetian"
Assembly="NavBar" %>
<html>
<head>
<script runat="server">
Sub Page_Load( )
'NB1.CreateBlankFile( )
End Sub
</script>
</head>
<body>
<table border="1" width="100%" cellpadding="20" cellspacing="0">
<tr>
<td align="center" width="150">
<img src="http://pics.10026.com/?src=aspnetian.jpg"/>
</td>
<td align="center">
<h1>NavBar Control Client Page<h1>
</td>
</tr>
<tr>
<td width="150">
<form runat="server">
<aspnetian:NavBar id="NB1"
showdividers="False" runat="server">
<strong>Navigation Bar</strong>
<br/>
</aspnetian:NavBar>
</form>
</td>
<td>
This is where page content might be placed
<br/><br/><br/><br/><br/><br/><br/><br/><br/>
</td>
</tr>
</table>
</body>
</htmlFirst of all your script is a vb rather thean c# one.

Secondly, you need Inherits attribute.

Eliyahu

"RC" <rc@.gmail.com> wrote in message
news:u72Il9dWFHA.1040@.TK2MSFTNGP10.phx.gbl...
> Hi,
> Can I seperate the following script block in a sperate DLL file instead of
> embedded in aspx/ascx?
> I know codebehind/src attribute can do it, but I found that it can't
trigger
> "Page_Load" event if I seperate and compile the code into DLL file
> <script runat="server">
> Sub Page_Load( )
> 'NB1.CreateBlankFile( )
> End Sub
> </script>
> Sample Code:
> <%@. Page Language="vb" %>
> <%@. Register TagPrefix="aspnetian" Namespace="aspnetian"
> Assembly="NavBar" %>
> <html>
> <head>
> <script runat="server">
> Sub Page_Load( )
> 'NB1.CreateBlankFile( )
> End Sub
> </script>
> </head>
> <body>
> <table border="1" width="100%" cellpadding="20" cellspacing="0">
> <tr>
> <td align="center" width="150">
> <img src="http://pics.10026.com/?src=aspnetian.jpg"/>
> </td>
> <td align="center">
> <h1>NavBar Control Client Page<h1>
> </td>
> </tr>
> <tr>
> <td width="150">
> <form runat="server">
> <aspnetian:NavBar id="NB1"
> showdividers="False" runat="server">
> <strong>Navigation Bar</strong>
> <br/>
> </aspnetian:NavBar>
> </form>
> </td>
> <td>
> This is where page content might be placed
> <br/><br/><br/><br/><br/><br/><br/><br/><br/>
> </td>
> </tr>
> </table>
> </body>
> </html>

seperate bthe dll from a project working on multiple dlls

Hi ya,

i am working on a project which includes dll's of many projects

Now i want to seperate my app , because whenever a change is made in any of the included project , i have to recompile my project.

so can anyone suggest me ....how to seperate my application.

Thanks

one Solution file for One Project file is probably the best way to do it.

But maybe i dont understand you properly.

if you have one project, and you reference say, 5 .dll files from other projects. When those projects change, all you have to do is re-compile them, your project that references those ones should not need to be re-compilled, just the new .dll should overwrite the old one.

Creating a Solution with Project Dependancies can also simplify the process, that way if you need to mod a .dll your referencing, you already have the project in your solution.

HTH,

mcm

Seperate Auth for single page

Can you specify a different authentication method ('none') for a single
page? I have the web.config designed for 'Forms' auth, but I have one page
that I want outside the auth method, without putting it into a seperate
folder with it's own web.config.

Thanks

--
Curt Christianson
Owner/Lead Developer, DF-Software
www.Darkfalz.comYou can specify some pages to require login, and others to not require login
via your web.config file by using the <location> tag.

Here is an example with sample code that you can download and play with.
http://www.dotnetbips.com/displayarticle.aspx?id=117

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
Hire top-notch developers at http://www.able-consulting.com

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:%23GKNgVP4DHA.1700@.TK2MSFTNGP11.phx.gbl...
> Can you specify a different authentication method ('none') for a single
> page? I have the web.config designed for 'Forms' auth, but I have one page
> that I want outside the auth method, without putting it into a seperate
> folder with it's own web.config.
> Thanks
> --
> Curt Christianson
> Owner/Lead Developer, DF-Software
> www.Darkfalz.com
Yes - see the <location> element of web.config.

example:

<location path="Logon.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location
HTH,

--
Scott
http://www.OdeToCode.com

On Thu, 22 Jan 2004 08:40:56 -0600, "Curt_C [MVP]"
<software_AT_darkfalz.com> wrote:

>Can you specify a different authentication method ('none') for a single
>page? I have the web.config designed for 'Forms' auth, but I have one page
>that I want outside the auth method, without putting it into a seperate
>folder with it's own web.config.
>Thanks

Seperate data content from UI

I have data in XML. I need to be able to present the data in a web page
which may be charts, tables and other such like controls. I need to be
able to modify the appearance of this page simply and quickly (a
requirement being a two day turnaround). Ultimately I need a page where
the user can control the layout themselves, but this is not an immediate
requirement. Also, it may be that they need to control not just the
appearance but functionality and behaviour(which may be a seperate
issue?). Now I'm new to this kind of thing, since I dont usually work
on the GUI side of things but this particular project is all about the
GUI. So what I'm asking for is some ideas not solutions :-) I've
thought it may be possible to do this sort of thing with cascading
stylesheets?? But that wouldn't allow me to change the behaviour of
controls on the form (though it certainly would be a start no?). Also
what about XUL? (or is that really going in the wrong direction) Is
there anything I can do specifically ASP-orientated that would make my
task easier, or am I really posing an HTML question? Perhaps it's even
a case of writing XSL to transform the XML so that it outputs HTML, and
the behaviour of the controls could be dependent on the XML they are given?

Ideas greatly appreciated :-)There are a two issues here. You need the ability to change
"appearence" but also you may need the ability to change
"functionality".

Design-wise, I would go with something similar to MVC. That way you
have a pretty good separation.

Implementation-wise I would go with ASP.NET 2 with Skins and
Personalization (as long as they are ready, I don't see the reason to
create my own) as long as you develop using ASP.NET 2.

If you don't, CSS is a good candidate for styling/markup separation but
quite difficult for changing behavior. There you should rely on sth
like different views on the MVC model.

XUL is supported only by FireFox right now, so I don't know if you want
to go to this direction.

Check Personalization and Skins in ASP.NET 2. I think you 'll find the
answers to these questions.

Regards,
Tasos
* Tasos Vogiatzoglou wrote:
> There are a two issues here. You need the ability to change
> "appearence" but also you may need the ability to change
> "functionality".
> Design-wise, I would go with something similar to MVC. That way you
> have a pretty good separation.
> Implementation-wise I would go with ASP.NET 2 with Skins and
> Personalization (as long as they are ready, I don't see the reason to
> create my own) as long as you develop using ASP.NET 2.
> If you don't, CSS is a good candidate for styling/markup separation but
> quite difficult for changing behavior. There you should rely on sth
> like different views on the MVC model.
> XUL is supported only by FireFox right now, so I don't know if you want
> to go to this direction.
> Check Personalization and Skins in ASP.NET 2. I think you 'll find the
> answers to these questions.
> Regards,
> Tasos

Many thanks for putting me in the right direction, I shall go find
Google :-)

Seperate data content from UI

I have data in XML. I need to be able to present the data in a web page
which may be charts, tables and other such like controls. I need to be
able to modify the appearance of this page simply and quickly (a
requirement being a two day turnaround). Ultimately I need a page where
the user can control the layout themselves, but this is not an immediate
requirement. Also, it may be that they need to control not just the
appearance but functionality and behaviour(which may be a seperate
issue?). Now I'm new to this kind of thing, since I dont usually work
on the GUI side of things but this particular project is all about the
GUI. So what I'm asking for is some ideas not solutions :-) I've
thought it may be possible to do this sort of thing with cascading
stylesheets' But that wouldn't allow me to change the behaviour of
controls on the form (though it certainly would be a start no?). Also
what about XUL? (or is that really going in the wrong direction) Is
there anything I can do specifically ASP-orientated that would make my
task easier, or am I really posing an HTML question? Perhaps it's even
a case of writing XSL to transform the XML so that it outputs HTML, and
the behaviour of the controls could be dependent on the XML they are given?
Ideas greatly appreciated :-)There are a two issues here. You need the ability to change
"appearence" but also you may need the ability to change
"functionality".
Design-wise, I would go with something similar to MVC. That way you
have a pretty good separation.
Implementation-wise I would go with ASP.NET 2 with Skins and
Personalization (as long as they are ready, I don't see the reason to
create my own) as long as you develop using ASP.NET 2.
If you don't, CSS is a good candidate for styling/markup separation but
quite difficult for changing behavior. There you should rely on sth
like different views on the MVC model.
XUL is supported only by FireFox right now, so I don't know if you want
to go to this direction.
Check Personalization and Skins in ASP.NET 2. I think you 'll find the
answers to these questions.
Regards,
Tasos
* Tasos Vogiatzoglou wrote:
> There are a two issues here. You need the ability to change
> "appearence" but also you may need the ability to change
> "functionality".
> Design-wise, I would go with something similar to MVC. That way you
> have a pretty good separation.
> Implementation-wise I would go with ASP.NET 2 with Skins and
> Personalization (as long as they are ready, I don't see the reason to
> create my own) as long as you develop using ASP.NET 2.
> If you don't, CSS is a good candidate for styling/markup separation but
> quite difficult for changing behavior. There you should rely on sth
> like different views on the MVC model.
> XUL is supported only by FireFox right now, so I don't know if you want
> to go to this direction.
> Check Personalization and Skins in ASP.NET 2. I think you 'll find the
> answers to these questions.
> Regards,
> Tasos
>
Many thanks for putting me in the right direction, I shall go find
Google :-)

Seperate folder page Inheritance issue

Hi,
I have a page "Project Folder\Customers\Products.aspx" which I want to inherit from another page "Project Folder\Product.aspx". As mentioned both files are in seperate folder.
In the customer specific Product.aspx page I want to use some code of the main product page. How can I do this? How can I inherit from page in a different folder?
Thanks in advance.

(if this is 2005) the code files should be in the app_code folder then. The security model in 2005 makes it difficult to get at the other files.
I don't think you can actually inherit from an other page (aspx). You have to create a custom control (ascx) and put this control in each of your page. If you want to inherit the code, juste create a class which inherits from Page and your pages just need to inherit from this class.
Yes. I am using .net 2.0 and VS 2005. Sorry i didn't mentioned that.
or use master pages...that's what they are there for :}

seperate service thread

I'd like to create a subsystem in my asp.net application that is responsible
for emails that need to be send out based upon certain events so that the
main request/response threads aren't responsible for the communication with
the smtp server, etc. I don't want to create a seperate thread for every
instance in which a notification must be sent...I'd rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this...should I just launch another thread upon application start? What do
I do w/ the thread when it has no responsibility. Should it just loop, look
for work and do work/sleep for a few seconds, etc?

I'm sure someone has done something like this before or come across some
article, etc...I appreciate any comments.

~PaulI suggest you look into using a Windows Service to handle such background
tasks.

Here's more information on Windows Services:
http://msdn.microsoft.com/library/d...dowsService.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"PJ" <pjwal@.hotmail.com> wrote in message
news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> I'd like to create a subsystem in my asp.net application that is
> responsible
> for emails that need to be send out based upon certain events so that the
> main request/response threads aren't responsible for the communication
> with
> the smtp server, etc. I don't want to create a seperate thread for every
> instance in which a notification must be sent...I'd rather record the
> necessary information to the db or web cache and let the subsystem be
> responsible for it from there. What is the best way to go about
> this...should I just launch another thread upon application start? What
> do
> I do w/ the thread when it has no responsibility. Should it just loop,
> look
> for work and do work/sleep for a few seconds, etc?
> I'm sure someone has done something like this before or come across some
> article, etc...I appreciate any comments.
> ~Paul
"PJ" <pjwal@.hotmail.com> wrote in message
news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> I'd like to create a subsystem in my asp.net application that is
responsible
> for emails that need to be send out based upon certain events so that the
> main request/response threads aren't responsible for the communication
with
> the smtp server, etc. I don't want to create a seperate thread for every
> instance in which a notification must be sent...I'd rather record the
> necessary information to the db or web cache and let the subsystem be
> responsible for it from there. What is the best way to go about
> this...should I just launch another thread upon application start? What
do
> I do w/ the thread when it has no responsibility. Should it just loop,
look
> for work and do work/sleep for a few seconds, etc?
> I'm sure someone has done something like this before or come across some
> article, etc...I appreciate any comments.

Here's a type called SingleThreadWorkQueue for this kind of thing. When you
create a SingleThreadWorkQueue is creates a thread and a Queue. The thread
pulls items off the queue and then invokes a delegate (a function outside
the type), and passes it whatever was on the queue.

So you would just bundle up the information to send an email into an object,
and pass it to SingleThreadWorkQueue.AddWork. The worker thread would then
dequeue the work and invoke your work function.

Like this:

-- somewhere in global scope --
public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
SendAnEmail)

public shared sub SendAnEmail(work as Object)
dim em as MyEmailInfo = directcast(work,MyEmailInfo)
--send the email
end sub

-- anywhere you want to generate an email, somehting like --

MyGlobal.OutgoingEmailQueue.AddWork(new EmailObject("joe@.hotmail.com","helo,
joe"))

Private Class SingleThreadWorkQueue
Private workQueue As Queue = Queue.Synchronized(New Queue())
Private wp As DoWork
Private wt As Thread
Public Delegate Sub DoWork(ByVal w As Object)
Public Event ExceptionOccured(ByVal e As Exception)

Public Sub New(ByVal WorkProc As DoWork)
wp = WorkProc
wt = New Thread(AddressOf workerThreadProc)
wt.IsBackground = True
wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
wp.Method.Name
wt.Start()
End Sub

Private Sub workerThreadProc()
SyncLock workQueue
Do
Monitor.Wait(workQueue, 5000)
Do While workQueue.Count > 0
Try
Dim w As Object = workQueue.Dequeue
wp.Invoke(w)
Catch ex As Exception
RaiseEvent ExceptionOccured(ex)
End Try
Loop
Loop
End SyncLock
End Sub
Public Sub AddWork(ByVal work As Object)
workQueue.Enqueue(work)
If Monitor.TryEnter(workQueue) Then
Monitor.Pulse(workQueue)
Monitor.Exit(workQueue)
End If
End Sub

End Class
This could work in some cases, but if the thread is especially long running
I'm afraid it could time out once the initiating thread (the ASP.NET page)
is long gone. A windows service should never time out, so it might be a bit
more reliable. A Queue could still be used to store up the list of to-dos.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uMVqAKEjEHA.3968@.TK2MSFTNGP10.phx.gbl...
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
>> I'd like to create a subsystem in my asp.net application that is
> responsible
>> for emails that need to be send out based upon certain events so that the
>> main request/response threads aren't responsible for the communication
> with
>> the smtp server, etc. I don't want to create a seperate thread for every
>> instance in which a notification must be sent...I'd rather record the
>> necessary information to the db or web cache and let the subsystem be
>> responsible for it from there. What is the best way to go about
>> this...should I just launch another thread upon application start? What
> do
>> I do w/ the thread when it has no responsibility. Should it just loop,
> look
>> for work and do work/sleep for a few seconds, etc?
>>
>> I'm sure someone has done something like this before or come across some
>> article, etc...I appreciate any comments.
>
> Here's a type called SingleThreadWorkQueue for this kind of thing. When
> you
> create a SingleThreadWorkQueue is creates a thread and a Queue. The
> thread
> pulls items off the queue and then invokes a delegate (a function outside
> the type), and passes it whatever was on the queue.
> So you would just bundle up the information to send an email into an
> object,
> and pass it to SingleThreadWorkQueue.AddWork. The worker thread would
> then
> dequeue the work and invoke your work function.
> Like this:
> -- somewhere in global scope --
> public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
> SendAnEmail)
> public shared sub SendAnEmail(work as Object)
> dim em as MyEmailInfo = directcast(work,MyEmailInfo)
> --send the email
> end sub
>
> -- anywhere you want to generate an email, somehting like --
>
> MyGlobal.OutgoingEmailQueue.AddWork(new
> EmailObject("joe@.hotmail.com","helo,
> joe"))
>
>
> Private Class SingleThreadWorkQueue
> Private workQueue As Queue = Queue.Synchronized(New Queue())
> Private wp As DoWork
> Private wt As Thread
> Public Delegate Sub DoWork(ByVal w As Object)
> Public Event ExceptionOccured(ByVal e As Exception)
> Public Sub New(ByVal WorkProc As DoWork)
> wp = WorkProc
> wt = New Thread(AddressOf workerThreadProc)
> wt.IsBackground = True
> wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
> wp.Method.Name
> wt.Start()
> End Sub
> Private Sub workerThreadProc()
> SyncLock workQueue
> Do
> Monitor.Wait(workQueue, 5000)
> Do While workQueue.Count > 0
> Try
> Dim w As Object = workQueue.Dequeue
> wp.Invoke(w)
> Catch ex As Exception
> RaiseEvent ExceptionOccured(ex)
> End Try
> Loop
> Loop
> End SyncLock
> End Sub
> Public Sub AddWork(ByVal work As Object)
> workQueue.Enqueue(work)
> If Monitor.TryEnter(workQueue) Then
> Monitor.Pulse(workQueue)
> Monitor.Exit(workQueue)
> End If
> End Sub
> End Class
"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> This could work in some cases, but if the thread is especially long
running
> I'm afraid it could time out once the initiating thread (the ASP.NET page)
> is long gone. A windows service should never time out, so it might be a
bit
> more reliable. A Queue could still be used to store up the list of
to-dos.

You wouldn't want to run especially long running things this way. Just
things that take too long to do while your user is waiting. Sending an
email is a perfect use because it can take about a second to do all the SMTP
stuff, and you just don't need to add that to your user's wait time. I also
use this mechanism to write to my log file. Multiple threads can add log
entries very quickly and the worker thread writes them all out to disk.
Things you might be tempted to use the ThreadPool.QueueUserWorkItem for if
that weren't frowned upon in asp.net, and things that require access from a
single thread, like a file.

A service will work, but for something like this I think it's not really
worth the hastle of administering and configuring the interprocess
communication.

David
This is good stuff. Excuse my ignorance, but why is it necessary to use a
delegate? Why can't the wrokerThreadProc just call SendAnEmail directly?

Thanks~
Paul

"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uMVqAKEjEHA.3968@.TK2MSFTNGP10.phx.gbl...
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> > I'd like to create a subsystem in my asp.net application that is
> responsible
> > for emails that need to be send out based upon certain events so that
the
> > main request/response threads aren't responsible for the communication
> with
> > the smtp server, etc. I don't want to create a seperate thread for
every
> > instance in which a notification must be sent...I'd rather record the
> > necessary information to the db or web cache and let the subsystem be
> > responsible for it from there. What is the best way to go about
> > this...should I just launch another thread upon application start?
What
> do
> > I do w/ the thread when it has no responsibility. Should it just loop,
> look
> > for work and do work/sleep for a few seconds, etc?
> > I'm sure someone has done something like this before or come across some
> > article, etc...I appreciate any comments.
>
> Here's a type called SingleThreadWorkQueue for this kind of thing. When
you
> create a SingleThreadWorkQueue is creates a thread and a Queue. The
thread
> pulls items off the queue and then invokes a delegate (a function outside
> the type), and passes it whatever was on the queue.
> So you would just bundle up the information to send an email into an
object,
> and pass it to SingleThreadWorkQueue.AddWork. The worker thread would
then
> dequeue the work and invoke your work function.
> Like this:
> -- somewhere in global scope --
> public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
> SendAnEmail)
> public shared sub SendAnEmail(work as Object)
> dim em as MyEmailInfo = directcast(work,MyEmailInfo)
> --send the email
> end sub
>
> -- anywhere you want to generate an email, somehting like --
>
> MyGlobal.OutgoingEmailQueue.AddWork(new
EmailObject("joe@.hotmail.com","helo,
> joe"))
>
>
> Private Class SingleThreadWorkQueue
> Private workQueue As Queue = Queue.Synchronized(New Queue())
> Private wp As DoWork
> Private wt As Thread
> Public Delegate Sub DoWork(ByVal w As Object)
> Public Event ExceptionOccured(ByVal e As Exception)
> Public Sub New(ByVal WorkProc As DoWork)
> wp = WorkProc
> wt = New Thread(AddressOf workerThreadProc)
> wt.IsBackground = True
> wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
> wp.Method.Name
> wt.Start()
> End Sub
> Private Sub workerThreadProc()
> SyncLock workQueue
> Do
> Monitor.Wait(workQueue, 5000)
> Do While workQueue.Count > 0
> Try
> Dim w As Object = workQueue.Dequeue
> wp.Invoke(w)
> Catch ex As Exception
> RaiseEvent ExceptionOccured(ex)
> End Try
> Loop
> Loop
> End SyncLock
> End Sub
> Public Sub AddWork(ByVal work As Object)
> workQueue.Enqueue(work)
> If Monitor.TryEnter(workQueue) Then
> Monitor.Pulse(workQueue)
> Monitor.Exit(workQueue)
> End If
> End Sub
> End Class
I'm curious about this as well. What exactly would time out? I assume
"time out" means certain object instances would be recycled? If the page
instance is recycled, how will it affect a worker thread if the thread is
not referencing any of the page's members? Will an object created in the
page, i.e. an EmailObject, that's passed to the queue get recycyled if the
page instance does?

Thanks for the clarification?

"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> > This could work in some cases, but if the thread is especially long
> running
> > I'm afraid it could time out once the initiating thread (the ASP.NET
page)
> > is long gone. A windows service should never time out, so it might be a
> bit
> > more reliable. A Queue could still be used to store up the list of
> to-dos.
> You wouldn't want to run especially long running things this way. Just
> things that take too long to do while your user is waiting. Sending an
> email is a perfect use because it can take about a second to do all the
SMTP
> stuff, and you just don't need to add that to your user's wait time. I
also
> use this mechanism to write to my log file. Multiple threads can add log
> entries very quickly and the worker thread writes them all out to disk.
> Things you might be tempted to use the ThreadPool.QueueUserWorkItem for if
> that weren't frowned upon in asp.net, and things that require access from
a
> single thread, like a file.
> A service will work, but for something like this I think it's not really
> worth the hastle of administering and configuring the interprocess
> communication.
> David
Once a page has been rendered, any threads it created are eventually
orphaned and become potential targets for the .net garbage collector. How
often the garbage collector runs varies, depending on the load of your
server & such.

David is right that this won't likely be a problem if all the thread is
doing is sending a quick email to an exchange queue. Once the email has
been generated and is in the queue, it shouldn't matter if the thread that
created it gets recycled. However, if the code is only doing something
really quick like that then I'm not sure why you'd need a separate thread
for it anyway.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net

"PJ" <pjwal@.hotmail.com> wrote in message
news:%23bJ78LIjEHA.3928@.TK2MSFTNGP11.phx.gbl...
> I'm curious about this as well. What exactly would time out? I assume
> "time out" means certain object instances would be recycled? If the page
> instance is recycled, how will it affect a worker thread if the thread is
> not referencing any of the page's members? Will an object created in the
> page, i.e. an EmailObject, that's passed to the queue get recycyled if the
> page instance does?
> Thanks for the clarification?
>
> "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
>>
>> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
>> news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
>> > This could work in some cases, but if the thread is especially long
>> running
>> > I'm afraid it could time out once the initiating thread (the ASP.NET
> page)
>> > is long gone. A windows service should never time out, so it might be
>> > a
>> bit
>> > more reliable. A Queue could still be used to store up the list of
>> to-dos.
>>>
>> You wouldn't want to run especially long running things this way. Just
>> things that take too long to do while your user is waiting. Sending an
>> email is a perfect use because it can take about a second to do all the
> SMTP
>> stuff, and you just don't need to add that to your user's wait time. I
> also
>> use this mechanism to write to my log file. Multiple threads can add log
>> entries very quickly and the worker thread writes them all out to disk.
>> Things you might be tempted to use the ThreadPool.QueueUserWorkItem for
>> if
>> that weren't frowned upon in asp.net, and things that require access from
> a
>> single thread, like a file.
>>
>> A service will work, but for something like this I think it's not really
>> worth the hastle of administering and configuring the interprocess
>> communication.
>>
>> David
>>
>>
There's a noticable difference when holding up the main response/request
thread at times. Also, some CDONTS errors seem to slip through any error
handling we have in place.

As far as orphaned threads...

What if I use a Singleton instance to create the thread? This singleton
could behave much like David's code snippet. The worker thread could be
created in the static constructor. Will the worker thread still be a target
for the garbage collector? I don't see how...I mean, if I add an item to
the web cache in a page instance, it doesn't get marked for garbage
collection, right?

Thanks for your time.

Public Class NotificationManager
Private Sub New()
End Sub

Shared Sub New()

'start thread with addressof Notify
End Sub

Private Shared ReadOnly _instance As NotificationManager = New
NotificationManager
Private _notificationList As Queue

Public Shared Function GetInstance() As NotificationManager
Return _instance
End Function

Public Sub AddWork(ByVal work as Object)
'add email data to queue
End Sub

Private Sub Notify
'loop, check queue, send email, etc
End Sub
End Class

'client code
NotificationManager.GetInstance().AddWork(myEmailO bject)

"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:OTwt$bIjEHA.592@.TK2MSFTNGP11.phx.gbl...
> Once a page has been rendered, any threads it created are eventually
> orphaned and become potential targets for the .net garbage collector. How
> often the garbage collector runs varies, depending on the load of your
> server & such.
> David is right that this won't likely be a problem if all the thread is
> doing is sending a quick email to an exchange queue. Once the email has
> been generated and is in the queue, it shouldn't matter if the thread that
> created it gets recycled. However, if the code is only doing something
> really quick like that then I'm not sure why you'd need a separate thread
> for it anyway.
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:%23bJ78LIjEHA.3928@.TK2MSFTNGP11.phx.gbl...
> > I'm curious about this as well. What exactly would time out? I assume
> > "time out" means certain object instances would be recycled? If the
page
> > instance is recycled, how will it affect a worker thread if the thread
is
> > not referencing any of the page's members? Will an object created in
the
> > page, i.e. an EmailObject, that's passed to the queue get recycyled if
the
> > page instance does?
> > Thanks for the clarification?
> > "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> > message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
> >>
> >> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> >> news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> >> > This could work in some cases, but if the thread is especially long
> >> running
> >> > I'm afraid it could time out once the initiating thread (the ASP.NET
> > page)
> >> > is long gone. A windows service should never time out, so it might
be
> >> > a
> >> bit
> >> > more reliable. A Queue could still be used to store up the list of
> >> to-dos.
> >> >>
> >> You wouldn't want to run especially long running things this way. Just
> >> things that take too long to do while your user is waiting. Sending an
> >> email is a perfect use because it can take about a second to do all the
> > SMTP
> >> stuff, and you just don't need to add that to your user's wait time. I
> > also
> >> use this mechanism to write to my log file. Multiple threads can add
log
> >> entries very quickly and the worker thread writes them all out to disk.
> >> Things you might be tempted to use the ThreadPool.QueueUserWorkItem for
> >> if
> >> that weren't frowned upon in asp.net, and things that require access
from
> > a
> >> single thread, like a file.
> >>
> >> A service will work, but for something like this I think it's not
really
> >> worth the hastle of administering and configuring the interprocess
> >> communication.
> >>
> >> David
> >>
> >>
Also, if the Singleton instance holds a reference to the thread ( as a
private class member ), will that not prevent the thread from being garbage
collected?

"PJ" <pjwal@.hotmail.com> wrote in message
news:ODtMq0IjEHA.592@.TK2MSFTNGP11.phx.gbl...
> There's a noticable difference when holding up the main response/request
> thread at times. Also, some CDONTS errors seem to slip through any error
> handling we have in place.
> As far as orphaned threads...
> What if I use a Singleton instance to create the thread? This singleton
> could behave much like David's code snippet. The worker thread could be
> created in the static constructor. Will the worker thread still be a
target
> for the garbage collector? I don't see how...I mean, if I add an item to
> the web cache in a page instance, it doesn't get marked for garbage
> collection, right?
> Thanks for your time.
> Public Class NotificationManager
> Private Sub New()
> End Sub
> Shared Sub New()
> 'start thread with addressof Notify
> End Sub
> Private Shared ReadOnly _instance As NotificationManager = New
> NotificationManager
> Private _notificationList As Queue
> Public Shared Function GetInstance() As NotificationManager
> Return _instance
> End Function
> Public Sub AddWork(ByVal work as Object)
> 'add email data to queue
> End Sub
> Private Sub Notify
> 'loop, check queue, send email, etc
> End Sub
> End Class
> 'client code
> NotificationManager.GetInstance().AddWork(myEmailO bject)
>
> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> news:OTwt$bIjEHA.592@.TK2MSFTNGP11.phx.gbl...
> > Once a page has been rendered, any threads it created are eventually
> > orphaned and become potential targets for the .net garbage collector.
How
> > often the garbage collector runs varies, depending on the load of your
> > server & such.
> > David is right that this won't likely be a problem if all the thread is
> > doing is sending a quick email to an exchange queue. Once the email has
> > been generated and is in the queue, it shouldn't matter if the thread
that
> > created it gets recycled. However, if the code is only doing something
> > really quick like that then I'm not sure why you'd need a separate
thread
> > for it anyway.
> > --
> > I hope this helps,
> > Steve C. Orr, MCSD, MVP
> > http://Steve.Orr.net
> > "PJ" <pjwal@.hotmail.com> wrote in message
> > news:%23bJ78LIjEHA.3928@.TK2MSFTNGP11.phx.gbl...
> > > I'm curious about this as well. What exactly would time out? I
assume
> > > "time out" means certain object instances would be recycled? If the
> page
> > > instance is recycled, how will it affect a worker thread if the thread
> is
> > > not referencing any of the page's members? Will an object created in
> the
> > > page, i.e. an EmailObject, that's passed to the queue get recycyled if
> the
> > > page instance does?
> > > > Thanks for the clarification?
> > > > > "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> > > message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
> > >>
> > >> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> > >> news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> > >> > This could work in some cases, but if the thread is especially long
> > >> running
> > >> > I'm afraid it could time out once the initiating thread (the
ASP.NET
> > > page)
> > >> > is long gone. A windows service should never time out, so it might
> be
> > >> > a
> > >> bit
> > >> > more reliable. A Queue could still be used to store up the list of
> > >> to-dos.
> > >> > >>
> > >> You wouldn't want to run especially long running things this way.
Just
> > >> things that take too long to do while your user is waiting. Sending
an
> > >> email is a perfect use because it can take about a second to do all
the
> > > SMTP
> > >> stuff, and you just don't need to add that to your user's wait time.
I
> > > also
> > >> use this mechanism to write to my log file. Multiple threads can add
> log
> > >> entries very quickly and the worker thread writes them all out to
disk.
> > >> Things you might be tempted to use the ThreadPool.QueueUserWorkItem
for
> > >> if
> > >> that weren't frowned upon in asp.net, and things that require access
> from
> > > a
> > >> single thread, like a file.
> > >>
> > >> A service will work, but for something like this I think it's not
> really
> > >> worth the hastle of administering and configuring the interprocess
> > >> communication.
> > >>
> > >> David
> > >>
> > >>
> >
"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:OTwt$bIjEHA.592@.TK2MSFTNGP11.phx.gbl...
> Once a page has been rendered, any threads it created are eventually
> orphaned and become potential targets for the .net garbage collector. How
> often the garbage collector runs varies, depending on the load of your
> server & such.

Nope. Threads are roots. A running thread and everything it references is
safe from the garbage collector. Even if this weren't the case, the static
reference to the thread would keep it reachable and keep it from being
collected.

David
"PJ" <pjwal@.hotmail.com> wrote in message
news:OeoRvLJjEHA.3536@.TK2MSFTNGP10.phx.gbl...
> Also, if the Singleton instance holds a reference to the thread ( as a
> private class member ), will that not prevent the thread from being
garbage
> collected?

The garbage collector never collects running threads. But you are right,
singletons are never collected because of the static (shared in VB)
reference. Classes are roots, just like threads are roots, and so any
object referenced by a class is not elegible for collection.

David

seperate service thread

I'd like to create a subsystem in my asp.net application that is responsible
for emails that need to be send out based upon certain events so that the
main request/response threads aren't responsible for the communication with
the smtp server, etc. I don't want to create a seperate thread for every
instance in which a notification must be sent...I'd rather record the
necessary information to the db or web cache and let the subsystem be
responsible for it from there. What is the best way to go about
this...should I just launch another thread upon application start? What do
I do w/ the thread when it has no responsibility. Should it just loop, look
for work and do work/sleep for a few seconds, etc?
I'm sure someone has done something like this before or come across some
article, etc...I appreciate any comments.
~PaulI suggest you look into using a Windows Service to handle such background
tasks.
Here's more information on Windows Services:
http://msdn.microsoft.com/library/d...dowsService.asp
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"PJ" <pjwal@.hotmail.com> wrote in message
news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> I'd like to create a subsystem in my asp.net application that is
> responsible
> for emails that need to be send out based upon certain events so that the
> main request/response threads aren't responsible for the communication
> with
> the smtp server, etc. I don't want to create a seperate thread for every
> instance in which a notification must be sent...I'd rather record the
> necessary information to the db or web cache and let the subsystem be
> responsible for it from there. What is the best way to go about
> this...should I just launch another thread upon application start? What
> do
> I do w/ the thread when it has no responsibility. Should it just loop,
> look
> for work and do work/sleep for a few seconds, etc?
> I'm sure someone has done something like this before or come across some
> article, etc...I appreciate any comments.
> ~Paul
>
"PJ" <pjwal@.hotmail.com> wrote in message
news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> I'd like to create a subsystem in my asp.net application that is
responsible
> for emails that need to be send out based upon certain events so that the
> main request/response threads aren't responsible for the communication
with
> the smtp server, etc. I don't want to create a seperate thread for every
> instance in which a notification must be sent...I'd rather record the
> necessary information to the db or web cache and let the subsystem be
> responsible for it from there. What is the best way to go about
> this...should I just launch another thread upon application start? What
do
> I do w/ the thread when it has no responsibility. Should it just loop,
look
> for work and do work/sleep for a few seconds, etc?
> I'm sure someone has done something like this before or come across some
> article, etc...I appreciate any comments.
Here's a type called SingleThreadWorkQueue for this kind of thing. When you
create a SingleThreadWorkQueue is creates a thread and a Queue. The thread
pulls items off the queue and then invokes a delegate (a function outside
the type), and passes it whatever was on the queue.
So you would just bundle up the information to send an email into an object,
and pass it to SingleThreadWorkQueue.AddWork. The worker thread would then
dequeue the work and invoke your work function.
Like this:
-- somewhere in global scope --
public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
SendAnEmail)
public shared sub SendAnEmail(work as Object)
dim em as MyEmailInfo = directcast(work,MyEmailInfo)
--send the email
end sub
-- anywhere you want to generate an email, somehting like --
MyGlobal.OutgoingEmailQueue.AddWork(new EmailObject("joe@.hotmail.com","helo,
joe"))
Private Class SingleThreadWorkQueue
Private workQueue As Queue = Queue.Synchronized(New Queue())
Private wp As DoWork
Private wt As Thread
Public Delegate Sub DoWork(ByVal w As Object)
Public Event ExceptionOccured(ByVal e As Exception)
Public Sub New(ByVal WorkProc As DoWork)
wp = WorkProc
wt = New Thread(AddressOf workerThreadProc)
wt.IsBackground = True
wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
wp.Method.Name
wt.Start()
End Sub
Private Sub workerThreadProc()
SyncLock workQueue
Do
Monitor.Wait(workQueue, 5000)
Do While workQueue.Count > 0
Try
Dim w As Object = workQueue.Dequeue
wp.Invoke(w)
Catch ex As Exception
RaiseEvent ExceptionOccured(ex)
End Try
Loop
Loop
End SyncLock
End Sub
Public Sub AddWork(ByVal work As Object)
workQueue.Enqueue(work)
If Monitor.TryEnter(workQueue) Then
Monitor.Pulse(workQueue)
Monitor.Exit(workQueue)
End If
End Sub
End Class
This could work in some cases, but if the thread is especially long running
I'm afraid it could time out once the initiating thread (the ASP.NET page)
is long gone. A windows service should never time out, so it might be a bit
more reliable. A Queue could still be used to store up the list of to-dos.
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uMVqAKEjEHA.3968@.TK2MSFTNGP10.phx.gbl...
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> responsible
> with
> do
> look
>
> Here's a type called SingleThreadWorkQueue for this kind of thing. When
> you
> create a SingleThreadWorkQueue is creates a thread and a Queue. The
> thread
> pulls items off the queue and then invokes a delegate (a function outside
> the type), and passes it whatever was on the queue.
> So you would just bundle up the information to send an email into an
> object,
> and pass it to SingleThreadWorkQueue.AddWork. The worker thread would
> then
> dequeue the work and invoke your work function.
> Like this:
> -- somewhere in global scope --
> public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
> SendAnEmail)
> public shared sub SendAnEmail(work as Object)
> dim em as MyEmailInfo = directcast(work,MyEmailInfo)
> --send the email
> end sub
>
> -- anywhere you want to generate an email, somehting like --
>
> MyGlobal.OutgoingEmailQueue.AddWork(new
> EmailObject("joe@.hotmail.com","helo,
> joe"))
>
>
> Private Class SingleThreadWorkQueue
> Private workQueue As Queue = Queue.Synchronized(New Queue())
> Private wp As DoWork
> Private wt As Thread
> Public Delegate Sub DoWork(ByVal w As Object)
> Public Event ExceptionOccured(ByVal e As Exception)
> Public Sub New(ByVal WorkProc As DoWork)
> wp = WorkProc
> wt = New Thread(AddressOf workerThreadProc)
> wt.IsBackground = True
> wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
> wp.Method.Name
> wt.Start()
> End Sub
> Private Sub workerThreadProc()
> SyncLock workQueue
> Do
> Monitor.Wait(workQueue, 5000)
> Do While workQueue.Count > 0
> Try
> Dim w As Object = workQueue.Dequeue
> wp.Invoke(w)
> Catch ex As Exception
> RaiseEvent ExceptionOccured(ex)
> End Try
> Loop
> Loop
> End SyncLock
> End Sub
> Public Sub AddWork(ByVal work As Object)
> workQueue.Enqueue(work)
> If Monitor.TryEnter(workQueue) Then
> Monitor.Pulse(workQueue)
> Monitor.Exit(workQueue)
> End If
> End Sub
> End Class
>
"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> This could work in some cases, but if the thread is especially long
running
> I'm afraid it could time out once the initiating thread (the ASP.NET page)
> is long gone. A windows service should never time out, so it might be a
bit
> more reliable. A Queue could still be used to store up the list of
to-dos.
>
You wouldn't want to run especially long running things this way. Just
things that take too long to do while your user is waiting. Sending an
email is a perfect use because it can take about a second to do all the SMTP
stuff, and you just don't need to add that to your user's wait time. I also
use this mechanism to write to my log file. Multiple threads can add log
entries very quickly and the worker thread writes them all out to disk.
Things you might be tempted to use the ThreadPool.QueueUserWorkItem for if
that weren't frowned upon in asp.net, and things that require access from a
single thread, like a file.
A service will work, but for something like this I think it's not really
worth the hastle of administering and configuring the interprocess
communication.
David
This is good stuff. Excuse my ignorance, but why is it necessary to use a
delegate? Why can't the wrokerThreadProc just call SendAnEmail directly?
Thanks~
Paul
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:uMVqAKEjEHA.3968@.TK2MSFTNGP10.phx.gbl...
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:uEVqiE8iEHA.704@.TK2MSFTNGP09.phx.gbl...
> responsible
the
> with
every
What
> do
> look
>
> Here's a type called SingleThreadWorkQueue for this kind of thing. When
you
> create a SingleThreadWorkQueue is creates a thread and a Queue. The
thread
> pulls items off the queue and then invokes a delegate (a function outside
> the type), and passes it whatever was on the queue.
> So you would just bundle up the information to send an email into an
object,
> and pass it to SingleThreadWorkQueue.AddWork. The worker thread would
then
> dequeue the work and invoke your work function.
> Like this:
> -- somewhere in global scope --
> public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf
> SendAnEmail)
> public shared sub SendAnEmail(work as Object)
> dim em as MyEmailInfo = directcast(work,MyEmailInfo)
> --send the email
> end sub
>
> -- anywhere you want to generate an email, somehting like --
>
> MyGlobal.OutgoingEmailQueue.AddWork(new
EmailObject("joe@.hotmail.com","helo,
> joe"))
>
>
> Private Class SingleThreadWorkQueue
> Private workQueue As Queue = Queue.Synchronized(New Queue())
> Private wp As DoWork
> Private wt As Thread
> Public Delegate Sub DoWork(ByVal w As Object)
> Public Event ExceptionOccured(ByVal e As Exception)
> Public Sub New(ByVal WorkProc As DoWork)
> wp = WorkProc
> wt = New Thread(AddressOf workerThreadProc)
> wt.IsBackground = True
> wt.Name = "worker thread for " & wp.Target.GetType.Name & "." &
> wp.Method.Name
> wt.Start()
> End Sub
> Private Sub workerThreadProc()
> SyncLock workQueue
> Do
> Monitor.Wait(workQueue, 5000)
> Do While workQueue.Count > 0
> Try
> Dim w As Object = workQueue.Dequeue
> wp.Invoke(w)
> Catch ex As Exception
> RaiseEvent ExceptionOccured(ex)
> End Try
> Loop
> Loop
> End SyncLock
> End Sub
> Public Sub AddWork(ByVal work As Object)
> workQueue.Enqueue(work)
> If Monitor.TryEnter(workQueue) Then
> Monitor.Pulse(workQueue)
> Monitor.Exit(workQueue)
> End If
> End Sub
> End Class
>
I'm curious about this as well. What exactly would time out? I assume
"time out" means certain object instances would be recycled? If the page
instance is recycled, how will it affect a worker thread if the thread is
not referencing any of the page's members? Will an object created in the
page, i.e. an EmailObject, that's passed to the queue get recycyled if the
page instance does?
Thanks for the clarification?
"David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> news:uLBHzYGjEHA.1348@.tk2msftngp13.phx.gbl...
> running
page)
> bit
> to-dos.
> You wouldn't want to run especially long running things this way. Just
> things that take too long to do while your user is waiting. Sending an
> email is a perfect use because it can take about a second to do all the
SMTP
> stuff, and you just don't need to add that to your user's wait time. I
also
> use this mechanism to write to my log file. Multiple threads can add log
> entries very quickly and the worker thread writes them all out to disk.
> Things you might be tempted to use the ThreadPool.QueueUserWorkItem for if
> that weren't frowned upon in asp.net, and things that require access from
a
> single thread, like a file.
> A service will work, but for something like this I think it's not really
> worth the hastle of administering and configuring the interprocess
> communication.
> David
>
Once a page has been rendered, any threads it created are eventually
orphaned and become potential targets for the .net garbage collector. How
often the garbage collector runs varies, depending on the load of your
server & such.
David is right that this won't likely be a problem if all the thread is
doing is sending a quick email to an exchange queue. Once the email has
been generated and is in the queue, it shouldn't matter if the thread that
created it gets recycled. However, if the code is only doing something
really quick like that then I'm not sure why you'd need a separate thread
for it anyway.
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"PJ" <pjwal@.hotmail.com> wrote in message
news:%23bJ78LIjEHA.3928@.TK2MSFTNGP11.phx.gbl...
> I'm curious about this as well. What exactly would time out? I assume
> "time out" means certain object instances would be recycled? If the page
> instance is recycled, how will it affect a worker thread if the thread is
> not referencing any of the page's members? Will an object created in the
> page, i.e. an EmailObject, that's passed to the queue get recycyled if the
> page instance does?
> Thanks for the clarification?
>
> "David Browne" <davidbaxterbrowne no potted meat@.hotmail.com> wrote in
> message news:%23whL8LHjEHA.1776@.TK2MSFTNGP15.phx.gbl...
> page)
> SMTP
> also
> a
>
There's a noticable difference when holding up the main response/request
thread at times. Also, some CDONTS errors seem to slip through any error
handling we have in place.
As far as orphaned threads...
What if I use a Singleton instance to create the thread? This singleton
could behave much like David's code snippet. The worker thread could be
created in the static constructor. Will the worker thread still be a target
for the garbage collector? I don't see how...I mean, if I add an item to
the web cache in a page instance, it doesn't get marked for garbage
collection, right?
Thanks for your time.
Public Class NotificationManager
Private Sub New()
End Sub
Shared Sub New()
'start thread with addressof Notify
End Sub
Private Shared ReadOnly _instance As NotificationManager = New
NotificationManager
Private _notificationList As Queue
Public Shared Function GetInstance() As NotificationManager
Return _instance
End Function
Public Sub AddWork(ByVal work as Object)
'add email data to queue
End Sub
Private Sub Notify
'loop, check queue, send email, etc
End Sub
End Class
'client code
NotificationManager.GetInstance().AddWork(myEmailObject)
"Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
news:OTwt$bIjEHA.592@.TK2MSFTNGP11.phx.gbl...
> Once a page has been rendered, any threads it created are eventually
> orphaned and become potential targets for the .net garbage collector. How
> often the garbage collector runs varies, depending on the load of your
> server & such.
> David is right that this won't likely be a problem if all the thread is
> doing is sending a quick email to an exchange queue. Once the email has
> been generated and is in the queue, it shouldn't matter if the thread that
> created it gets recycled. However, if the code is only doing something
> really quick like that then I'm not sure why you'd need a separate thread
> for it anyway.
> --
> I hope this helps,
> Steve C. Orr, MCSD, MVP
> http://Steve.Orr.net
>
> "PJ" <pjwal@.hotmail.com> wrote in message
> news:%23bJ78LIjEHA.3928@.TK2MSFTNGP11.phx.gbl...
page
is
the
the
be
log
from
really
>
Also, if the Singleton instance holds a reference to the thread ( as a
private class member ), will that not prevent the thread from being garbage
collected?
"PJ" <pjwal@.hotmail.com> wrote in message
news:ODtMq0IjEHA.592@.TK2MSFTNGP11.phx.gbl...
> There's a noticable difference when holding up the main response/request
> thread at times. Also, some CDONTS errors seem to slip through any error
> handling we have in place.
> As far as orphaned threads...
> What if I use a Singleton instance to create the thread? This singleton
> could behave much like David's code snippet. The worker thread could be
> created in the static constructor. Will the worker thread still be a
target
> for the garbage collector? I don't see how...I mean, if I add an item to
> the web cache in a page instance, it doesn't get marked for garbage
> collection, right?
> Thanks for your time.
> Public Class NotificationManager
> Private Sub New()
> End Sub
> Shared Sub New()
> 'start thread with addressof Notify
> End Sub
> Private Shared ReadOnly _instance As NotificationManager = New
> NotificationManager
> Private _notificationList As Queue
> Public Shared Function GetInstance() As NotificationManager
> Return _instance
> End Function
> Public Sub AddWork(ByVal work as Object)
> 'add email data to queue
> End Sub
> Private Sub Notify
> 'loop, check queue, send email, etc
> End Sub
> End Class
> 'client code
> NotificationManager.GetInstance().AddWork(myEmailObject)
>
> "Steve C. Orr [MVP, MCSD]" <Steve@.Orr.net> wrote in message
> news:OTwt$bIjEHA.592@.TK2MSFTNGP11.phx.gbl...
How
that
thread
assume
> page
> is
> the
> the
ASP.NET
> be
Just
an
the
I
> log
disk.
for
> from
> really
>

Seperate validation for seperate user controls on same page

asp.net 2.0
I have a page with a tab control on it. Each tab has a different user
control on it. each user control is used for different data entry (like
separate data entry forms) and has its own validation controls and
validation summary control.

When I'm on the first tab, it uses all the validation control on the other
tab as well. How can I keep all the logic for the validation separate for
each UC as if they were their own pages?

Thanks.

--
moondaddy@dotnet.itags.org.noemail.noemailWhen I'm on the first tab, it uses all the validation control on the other

Quote:

Originally Posted by

tab as well. How can I keep all the logic for the validation separate for
each UC as if they were their own pages?


Difficult to do... not sure but you may want to look into "ValidationGroup".

--
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujinionline.com
http://eduzine.edujinionline.com
-------------
Hi moondaddy,

As for the validation separation question you posted here, I've also found
your another thread "Validation Groups". I've posted some suggestion about
use the ASP.NET 2.0 Validation Group feature there. Please feel free to
followup there if you have any further questions on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

This posting is provided "AS IS" with no warranties, and confers no rights.

Seperate validation for seperate user controls on same page

asp.net 2.0
I have a page with a tab control on it. Each tab has a different user
control on it. each user control is used for different data entry (like
separate data entry forms) and has its own validation controls and
validation summary control.
When I'm on the first tab, it uses all the validation control on the other
tab as well. How can I keep all the logic for the validation separate for
each UC as if they were their own pages?
Thanks.
moondaddy@dotnet.itags.org.noemail.noemail> When I'm on the first tab, it uses all the validation control on the other
> tab as well. How can I keep all the logic for the validation separate for
> each UC as if they were their own pages?
Difficult to do... not sure but you may want to look into "ValidationGroup".
Happy Hacking,
Gaurav Vaish | www.mastergaurav.com
www.edujinionline.com
http://eduzine.edujinionline.com
---
Hi moondaddy,
As for the validation separation question you posted here, I've also found
your another thread "Validation Groups". I've posted some suggestion about
use the ASP.NET 2.0 Validation Group feature there. Please feel free to
followup there if you have any further questions on this.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

seperating an address field....?

Does anyone know an easy way to seperate an address in one database field into a couple of different fields.

if i have the following in one table

address:
1234 Main St.

number road_name Type
1234 Main St.

is this possible?It is very much possible. Just make fields for different sections of the address.

id
addr_streetNumber
addr_roadName
addr_type

I usually store the whole street as one then store the city/state/zipinformation in different fields. What db are you trying to dothis for?
it is sql server.

the issue i'm trying to figure out is that the addresses are not in thedatabase in a consistent manner. Here is a random sample of whatmy be in there. I thought about using the split() method, butknow I don't think this is the way to go.

Thanks for the help

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6011 CENTURY OAKS DR

3224 S HI LO CIR

P O BOX 11400

4105 9TH AVE.

1109 JORDAN LANE
I've spent a lot of years in the mailing industry and I knowall-too-well how inconsistent user-entered data can be. The firststep is to make the data consistent. My suggestion to you is topurchase an address standardizationprogram. This will go up against a USPS database and correct yourdata, and as an added bonus plus it can field your data automaticallythe way you need.
"My suggestion to you is topurchase an address standardizationprogram"
Any suggestions on a good product? Does it allow batchprocessing? We have about 20,000+ records that need to bevalidated and seperated.

Thanks.
Is this an ongoing need or one time only?

Personally I've used BCC Software'sMail Manager product for bulk address processing. And I've usedMelissa Data's APIs for individual address standardization from within an application.

If this is a once-and-done process, Melissa Data is one of many companies offeringCASS certification as a service, and they price their service per thousand records.
one time thing. thanks for the help... i'll look into those options.