Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Multithreading in ASP.NET app???

Reply
Thread Tools

Multithreading in ASP.NET app???

 
 
JV
Guest
Posts: n/a
 
      03-13-2006
My ASP.NET application needs to accept data from a post and return quickly.
I've added a worker thread to wake up and save data asynchronously after the
data is received. But, it appears to me that IIS kills the process and
thus the worker thread right after my main ASP.NET thread returns (anyone
know for certain?).

Do you know of a design pattern to accomplish this?


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      03-13-2006
JV,

You have to figure out for what reason ASP.NET is killing the process.
ASP.NET does have application recycling if the app goes down, and sometimes,
if memory consumption is too high, it will recycle the process.

What I would recommend is writing a service that will perform this
operation for you, then using a technology like MSMQ (through
System.Messaging) to send a message to the service, which will then handle
the updating of the data.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- http://www.velocityreviews.com/forums/(E-Mail Removed)

"JV" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My ASP.NET application needs to accept data from a post and return
> quickly. I've added a worker thread to wake up and save data
> asynchronously after the data is received. But, it appears to me that
> IIS kills the process and thus the worker thread right after my main
> ASP.NET thread returns (anyone know for certain?).
>
> Do you know of a design pattern to accomplish this?
>



 
Reply With Quote
 
 
 
 
Oscar Thornell
Guest
Posts: n/a
 
      03-13-2006
Hi,

Check this out...

/Oscar

//Buisness layer...gets called from an ASP.NET web page...
public bool InitAsyncOperation(string someData)
{
try
{
//Access layer
Access access = Access.GetInstance(CurrentUser);

if(access.IsRunning)
{
return false;
}
else
{
//First we do some sync operation...
Wrapper wrapper = access.Validate(someData);

//Then we kick of the async...
MyDelegate d = new MyDelegate(<Another method...that performs the async
operation...>);
AsyncHelper.Execute(d, wrapper);
}
}
catch(Exception ex)
{
if(ExceptionPolicy.HandleException(ex, CONTROLLER_EXCEPTION_POLICY))
throw;
}

return true;
}




///Class AsyncHelper
using System;
using System.Reflection;
using System.Threading;

namespace <SomeCompany>.<SomeApp>.Util
{
/// <summary>
/// Starting with the 1.1 release of the .NET Framework, the SDK docs
/// now carry a caution that mandates calling EndInvoke on delegates
/// you've called BeginInvoke on in order to avoid potential leaks.
/// This means you cannot simply "fire-and-forget" a call to BeginInvoke
/// when spawning a new worker thread from the thread pool without the
risk
/// of creating a memory leak.
///
/// The usage model is that instead of calling BeginInvoke against a
delegate,
/// you would instead call AsyncHelper.Execute, passing that delegate and
it's parameters as input.
/// See: http://staff.develop.com/woodring for further information.
/// </summary>
/// <example>
/// delegate void CalcAndDisplaySumDelegate( int a, int b );
/// CalcAndDisplaySumDelegate d = new
CalcAndDisplaySumDelegate(someCalc.Add);
/// AsyncHelper.Execute(d, 2, 3);
/// </example>
public class AsyncHelper
{
private static WaitCallback callback = new
WaitCallback(DynamicInvokeShim);

/// <summary>
/// Takes a delegate and a list of arguments. Performs asynchronous
invocation of the
/// target(the Class.Method the delegates points to..).
/// </summary>
/// <param name="d"></param>
/// <param name="args"></param>
/// <Author>Oscar Thornell</Author>
public static void Execute(Delegate d, params object[] args)
{
ThreadPool.QueueUserWorkItem(callback, new TargetInfo(d, args));
}

private static void DynamicInvokeShim(object obj)
{
TargetInfo targetInfo = (TargetInfo)obj;
targetInfo.Target.DynamicInvoke(targetInfo.Args);
}

class TargetInfo
{
internal readonly Delegate Target;
internal readonly object[] Args;

internal TargetInfo(Delegate d, object[] args)
{
Target = d;
Args = args;
}
}
}
}



"JV" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My ASP.NET application needs to accept data from a post and return
> quickly. I've added a worker thread to wake up and save data
> asynchronously after the data is received. But, it appears to me that
> IIS kills the process and thus the worker thread right after my main
> ASP.NET thread returns (anyone know for certain?).
>
> Do you know of a design pattern to accomplish this?
>



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      03-13-2006
As you observed, you are not gaining much with multithreading in Asp.Net.

You can make a Windows service that will pick up requests made by the
Asp.Net application. The requests can be passed on in a number of ways. Two
most common ones are via a database and via files.

Eliyahu

"JV" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My ASP.NET application needs to accept data from a post and return
> quickly. I've added a worker thread to wake up and save data
> asynchronously after the data is received. But, it appears to me that
> IIS kills the process and thus the worker thread right after my main
> ASP.NET thread returns (anyone know for certain?).
>
> Do you know of a design pattern to accomplish this?
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      03-13-2006
I wouldn't recommend using a database or files for notifications to the
service. That would require you to poll for the result, which on a general
level, is not too efficient.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Eliyahu Goldin" <(E-Mail Removed)> wrote in message
news:OWN%(E-Mail Removed)...
> As you observed, you are not gaining much with multithreading in Asp.Net.
>
> You can make a Windows service that will pick up requests made by the
> Asp.Net application. The requests can be passed on in a number of ways.
> Two most common ones are via a database and via files.
>
> Eliyahu
>
> "JV" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> My ASP.NET application needs to accept data from a post and return
>> quickly. I've added a worker thread to wake up and save data
>> asynchronously after the data is received. But, it appears to me that
>> IIS kills the process and thus the worker thread right after my main
>> ASP.NET thread returns (anyone know for certain?).
>>
>> Do you know of a design pattern to accomplish this?
>>

>
>



 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      03-13-2006
System.IO.FileSystemWatcher is a nice class that takes care of notification
in case of files.

Eliyahu

"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> I wouldn't recommend using a database or files for notifications to the
> service. That would require you to poll for the result, which on a
> general level, is not too efficient.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Eliyahu Goldin" <(E-Mail Removed)> wrote in message
> news:OWN%(E-Mail Removed)...
>> As you observed, you are not gaining much with multithreading in Asp.Net.
>>
>> You can make a Windows service that will pick up requests made by the
>> Asp.Net application. The requests can be passed on in a number of ways.
>> Two most common ones are via a database and via files.
>>
>> Eliyahu
>>
>> "JV" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> My ASP.NET application needs to accept data from a post and return
>>> quickly. I've added a worker thread to wake up and save data
>>> asynchronously after the data is received. But, it appears to me that
>>> IIS kills the process and thus the worker thread right after my main
>>> ASP.NET thread returns (anyone know for certain?).
>>>
>>> Do you know of a design pattern to accomplish this?
>>>

>>
>>

>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      03-13-2006
Eliyahu Goldin wrote:
> System.IO.FileSystemWatcher is a nice class that takes care of notification
> in case of files.


Not reliably, in my experience. I don't know *exactly* what goes wrong,
but often FileSystemWatcher can end up missing events, or waiting a
while and then delivering batches etc. I'm not the only one to have
noticed this. I still use FSW, but only in conjunction with something
which manually polls in case something goes wrong. It's all a bit of a
hack

Jon

 
Reply With Quote
 
JV
Guest
Posts: n/a
 
      03-13-2006
I've only skimmed over your code thus far, but it appears you are spawning a
thread from the threadpool for each ASP.NET request? If I'm correct about
that, that would become a scalability issue.
My goal is to have a single worker thread that manages the dataset and
multiple requests putting/getting data. Wish I could have just used MSSQL
for this.


"Oscar Thornell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Check this out...
>
> /Oscar
>
> //Buisness layer...gets called from an ASP.NET web page...
> public bool InitAsyncOperation(string someData)
> {
> try
> {
> //Access layer
> Access access = Access.GetInstance(CurrentUser);
>
> if(access.IsRunning)
> {
> return false;
> }
> else
> {
> //First we do some sync operation...
> Wrapper wrapper = access.Validate(someData);
>
> //Then we kick of the async...
> MyDelegate d = new MyDelegate(<Another method...that performs the
> async operation...>);
> AsyncHelper.Execute(d, wrapper);
> }
> }
> catch(Exception ex)
> {
> if(ExceptionPolicy.HandleException(ex, CONTROLLER_EXCEPTION_POLICY))
> throw;
> }
>
> return true;
> }
>
>
>
>
> ///Class AsyncHelper
> using System;
> using System.Reflection;
> using System.Threading;
>
> namespace <SomeCompany>.<SomeApp>.Util
> {
> /// <summary>
> /// Starting with the 1.1 release of the .NET Framework, the SDK docs
> /// now carry a caution that mandates calling EndInvoke on delegates
> /// you've called BeginInvoke on in order to avoid potential leaks.
> /// This means you cannot simply "fire-and-forget" a call to
> BeginInvoke
> /// when spawning a new worker thread from the thread pool without the
> risk
> /// of creating a memory leak.
> ///
> /// The usage model is that instead of calling BeginInvoke against a
> delegate,
> /// you would instead call AsyncHelper.Execute, passing that delegate and
> it's parameters as input.
> /// See: http://staff.develop.com/woodring for further information.
> /// </summary>
> /// <example>
> /// delegate void CalcAndDisplaySumDelegate( int a, int b );
> /// CalcAndDisplaySumDelegate d = new
> CalcAndDisplaySumDelegate(someCalc.Add);
> /// AsyncHelper.Execute(d, 2, 3);
> /// </example>
> public class AsyncHelper
> {
> private static WaitCallback callback = new
> WaitCallback(DynamicInvokeShim);
>
> /// <summary>
> /// Takes a delegate and a list of arguments. Performs asynchronous
> invocation of the
> /// target(the Class.Method the delegates points to..).
> /// </summary>
> /// <param name="d"></param>
> /// <param name="args"></param>
> /// <Author>Oscar Thornell</Author>
> public static void Execute(Delegate d, params object[] args)
> {
> ThreadPool.QueueUserWorkItem(callback, new TargetInfo(d, args));
> }
>
> private static void DynamicInvokeShim(object obj)
> {
> TargetInfo targetInfo = (TargetInfo)obj;
> targetInfo.Target.DynamicInvoke(targetInfo.Args);
> }
>
> class TargetInfo
> {
> internal readonly Delegate Target;
> internal readonly object[] Args;
>
> internal TargetInfo(Delegate d, object[] args)
> {
> Target = d;
> Args = args;
> }
> }
> }
> }
>
>
>
> "JV" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> My ASP.NET application needs to accept data from a post and return
>> quickly. I've added a worker thread to wake up and save data
>> asynchronously after the data is received. But, it appears to me that
>> IIS kills the process and thus the worker thread right after my main
>> ASP.NET thread returns (anyone know for certain?).
>>
>> Do you know of a design pattern to accomplish this?
>>

>
>



 
Reply With Quote
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      03-13-2006
Hi,


"JV" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> My ASP.NET application needs to accept data from a post and return
> quickly. I've added a worker thread to wake up and save data


TO wake up ?
How r u storing the thread reference? in Application ?
How r u running it?
Show some code for more details.

What if you create the thread at each request? This allow you to create one
thread per request and not having to worry about concurrency, the drawback
is that you may create lots of threads depending of how busy is your web
site.



cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



 
Reply With Quote
 
JV
Guest
Posts: n/a
 
      03-13-2006
Thanks for the reply!

Actually I was storing the reference to the object in Session, but I could
just as easily move it to Application. That would actually make more sense.

I am trying to avoid creating a thread per request. That sort of defeats
the purpose of returning quickly for scalability.



"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:(E-Mail Removed)...
> Hi,
>
>
> "JV" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> My ASP.NET application needs to accept data from a post and return
>> quickly. I've added a worker thread to wake up and save data

>
> TO wake up ?
> How r u storing the thread reference? in Application ?
> How r u running it?
> Show some code for more details.
>
> What if you create the thread at each request? This allow you to create
> one thread per request and not having to worry about concurrency, the
> drawback is that you may create lots of threads depending of how busy is
> your web site.
>
>
>
> cheers,
>
> --
> Ignacio Machin,
> ignacio.machin AT dot.state.fl.us
> Florida Department Of Transportation
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous requests vs. multithreading BoomWackaBoom ASP .Net 1 11-15-2003 05:42 PM
MultiThreading Muralidharan Ramakrishnan ASP .Net 1 11-10-2003 01:49 PM
Multithreading with HttpWebRequest Fieldadvice ASP .Net 3 11-07-2003 10:09 AM
Session State does not Work with Multithreading and SQL Server - Bug??? Ilia ASP .Net 6 11-04-2003 03:55 PM
Multithreading Boris Condarco ASP .Net 5 06-30-2003 01:02 PM



Advertisments