Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

HTTPContext Session and worker thread (Fire and Forget)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Braulio Diez - 08 Feb 2008 14:31 GMT
Hello,

 In my ASP .net application in one of the pages I have to fire a process
that takes quite long to execute, my idea is to execute this using the fire
and forget pattern (let the page to process and respond, but start a worker
thread to execute the process, it takes 1 minute to execute or something like
that).

All this I got it more or less
working(http://www.eggheadcafe.com/articles/20050818.asp).

 Now comes the question...

 My idea is to store the result in a session varialbe (later on using
script services I will play with that value), is a good approach to pass as a
parameter for the worker thread the HTTPContext or Session ? Too risky?(I
know an additional restriction is to use a static method :-() What other
approaches could I take?

 Thanks, Regards
      Braulio

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
George Ter-Saakov - 08 Feb 2008 15:05 GMT
I am not sure about implications of passing Session to another thread but in
general I would not do that. Unless you want to troubleshoot your
application for the rest of your life :)
---------------------------------------------------
I would create something more robust.

1. Table in DB tblJobs(JobId, Owner, Status, TimeIn, TimeOut, MessageIn,
MessageOut)
Status = (Pending, Processing, Completed)
Owner is who kciked off the job. List specific UserId

2. To kick of the job you insert record into tblJobs with MessageIn having
XML serialized object needed to do the job (like bunch of parameters). Also
you would need to increase "ActiveJobs" counter in the sesssion.

3. Some External Thread/External Process/MSMQ (your choice) pooling Jobs
to-do from the table and working on it. After it's done it populates TimeOut
and MessageOut with XML serialized object of the result of the job.

4. Your aspx page is checking for ActiveJobs counter if it's not 0 then
checks result of the job(s) submitted by that Owner. Updates Session if job
is completed.....

--------------------------------------------------------------------
PS: In order to avoid multithreading/multiprocessing problems you must be
careful. Always do optimistic locking when updating tblJobs
Meaning that if you have more than one process/thread that pools tblJobs for
new jobs then folow following steps (actually always do that)

1. Get a pending job "SELECT ... FROM tblJobs Where status = Pending"
2. Set status to processing "UPDATE tblJobs SET status=Processing WHERE
JobId=1 AND status = Pending"
3. Check that 1 (not 0) records were updated. SqlCommand.ExecuteNonQuery
returns that.
4. If returned value was 1 then proceed to execute job. If it's 0 then do
not execute job cause it was grabbed by other pooling thread between step 1
and 2.

George.

George.

> Hello,
>
[quoted text clipped - 27 lines]
> /// http://www.tipsdotnet.com
> /// ------------------------------
Braulio Diez - 09 Feb 2008 16:53 GMT
Well, I think that approach is valid if your batch processes take long time
to execute (30 minutes, one hour)...

But in my case, my process would take a minute to execute, and I want to let
it run and then via AJAX take the result back.

As far as I know Session is thread safe (not the content that you store), so
passing the HTTPContext taking into account that a session can last for
instance 20 minutes and my process can take a minute, should be crazy stuff.

Is there some documentation about this topic indicating what are the
limitations of passing the HTTPContext?

Thanks in advance
 Braulio

Signature

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------

> I am not sure about implications of passing Session to another thread but in
> general I would not do that. Unless you want to troubleshoot your
[quoted text clipped - 69 lines]
> > /// http://www.tipsdotnet.com
> > /// ------------------------------
Braulio Diez - 09 Feb 2008 17:10 GMT
Mmm... sorry now I'm getting more understanding about the HTTPContext, only
it's alive meanwhile the main thread is active but debugging in an XP machine
things goes a bit different and seem to work  (on a Windows 2003 Server or
Vista things changes). :-(((

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------

> I am not sure about implications of passing Session to another thread but in
> general I would not do that. Unless you want to troubleshoot your
[quoted text clipped - 69 lines]
> > /// http://www.tipsdotnet.com
> > /// ------------------------------
Braulio Diez - 09 Feb 2008 18:30 GMT
Googling a bit I have found that, I could:

 - Once the page is loaded on the client side, make the request via script
services:

    - Good thing: I will get notified once the request has been completed
without affecting my page.

   - Bad thing: I have to wait for the page to be served on the client side
and then launch from there the script service petition.

 I could implement as well a custom HttpHandler, but I think we would have
the same scenario.

 Any extra thought about this? Thanks, Regards
    Braulio


/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------

> I am not sure about implications of passing Session to another thread but in
> general I would not do that. Unless you want to troubleshoot your
[quoted text clipped - 69 lines]
> > /// http://www.tipsdotnet.com
> > /// ------------------------------

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.