Threading, using APPLICATION objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Cosmas

    Threading, using APPLICATION objects

    I need to execute some threads that load items into my APPLICATION object. I
    haven't figured out how to do that when I fire off a thread on a page, that
    takes its time and loads data into the APPLICATION level object which will
    be used later. Here is an example code I've used to fire it off.

    Dim pclsUserService s As clsUserServices = New clsUserServices
    Dim pobjUserService s As Thread
    pobjThread100 = New Thread(New ThreadStart(Add ressOf
    pclsUserService s.Initialize))
    pobjUserService s.Start()
    pobjThread100.S tart()

    What I need to do int he Initialize example here, it so actually load some
    data into the APPLICATION object. When I try to do this in the CLASS, I
    noticed that the APPLICATION object is not supported. Any ideas on how to
    accomplish this?


  • Brock Allen

    #2
    Re: Threading, using APPLICATION objects

    Can you explain a bit more what you're trying to do. It sounds like you want
    to load some data in a background thread so it's available later. Where do
    you want to save this loaded data? You said "Applicatio n", but I'm not sure
    if you mean the HttpApplication-derived objects or the old Application state
    bag held over from classic ASP. If you're talking about the later, then you
    can pass the Context.Applica tion over to the secondary thread (just set it
    as a field on your clsUserServices ). I think you're going to have a slew
    of sychronization problems tho. If I were doing this, I'd just do a lazy
    load on the data you need form the database. It'll make for less error prone
    code, IMO.

    -Brock
    DevelopMentor

    [color=blue]
    > I need to execute some threads that load items into my APPLICATION
    > object. I haven't figured out how to do that when I fire off a thread
    > on a page, that takes its time and loads data into the APPLICATION
    > level object which will be used later. Here is an example code I've
    > used to fire it off.
    >
    > Dim pclsUserService s As clsUserServices = New clsUserServices
    > Dim pobjUserService s As Thread
    > pobjThread100 = New Thread(New ThreadStart(Add ressOf
    > pclsUserService s.Initialize))
    > pobjUserService s.Start()
    > pobjThread100.S tart()
    > What I need to do int he Initialize example here, it so actually load
    > some data into the APPLICATION object. When I try to do this in the
    > CLASS, I noticed that the APPLICATION object is not supported. Any
    > ideas on how to accomplish this?
    >[/color]


    Comment

    • Bruce Barker

      #3
      Re: Threading, using APPLICATION objects

      as you new thread is not associated with a request context, you should pass
      the application object to the new thread. you shoudl also use a mutex to
      control creating the thread or you can have mulitple threads running at
      once.

      -- bruce (sqlwork.com)


      "John Cosmas" <johncosmas@cha rter.net> wrote in message
      news:E3inf.9$It 6.7@fe02.lga...[color=blue]
      >I need to execute some threads that load items into my APPLICATION object.
      >I haven't figured out how to do that when I fire off a thread on a page,
      >that takes its time and loads data into the APPLICATION level object which
      >will be used later. Here is an example code I've used to fire it off.
      >
      > Dim pclsUserService s As clsUserServices = New clsUserServices
      > Dim pobjUserService s As Thread
      > pobjThread100 = New Thread(New ThreadStart(Add ressOf
      > pclsUserService s.Initialize))
      > pobjUserService s.Start()
      > pobjThread100.S tart()
      >
      > What I need to do int he Initialize example here, it so actually load some
      > data into the APPLICATION object. When I try to do this in the CLASS, I
      > noticed that the APPLICATION object is not supported. Any ideas on how to
      > accomplish this?
      >[/color]


      Comment

      • John Cosmas

        #4
        Re: Threading, using APPLICATION objects

        Brock;

        Thanks for your response. Yes, you're heading in the right direction, but I
        need confirmation on how to pull this off, since I'm a little new to all of
        this. I'm trying to load some data that the customer will use later in the
        application. Since, this information is READ ONLY, this THREAD is intended
        to be FIRE and FORGET. So, based on your response, I am to assume that I
        need to pass the APPLICATION("my Data") into the clsUserServices .SomeMethod.
        If so, I am also assuming that you will then take the CONTEXT.APPLICA TION
        and do what you need to, and I can read from it later in my pages without a
        problem? I hope this clarifies it a bit.

        "Brock Allen" <ballen@NOSPAMd evelop.com> wrote in message
        news:b8743b115a 0e78c7cd2c9f1dd fde@msnews.micr osoft.com...[color=blue]
        > Can you explain a bit more what you're trying to do. It sounds like you
        > want to load some data in a background thread so it's available later.
        > Where do you want to save this loaded data? You said "Applicatio n", but
        > I'm not sure if you mean the HttpApplication-derived objects or the old
        > Application state bag held over from classic ASP. If you're talking about
        > the later, then you can pass the Context.Applica tion over to the secondary
        > thread (just set it as a field on your clsUserServices ). I think you're
        > going to have a slew of sychronization problems tho. If I were doing this,
        > I'd just do a lazy load on the data you need form the database. It'll make
        > for less error prone code, IMO.
        > -Brock
        > DevelopMentor
        > http://staff.develop.com/ballen
        >[color=green]
        >> I need to execute some threads that load items into my APPLICATION
        >> object. I haven't figured out how to do that when I fire off a thread
        >> on a page, that takes its time and loads data into the APPLICATION
        >> level object which will be used later. Here is an example code I've
        >> used to fire it off.
        >>
        >> Dim pclsUserService s As clsUserServices = New clsUserServices
        >> Dim pobjUserService s As Thread
        >> pobjThread100 = New Thread(New ThreadStart(Add ressOf
        >> pclsUserService s.Initialize))
        >> pobjUserService s.Start()
        >> pobjThread100.S tart()
        >> What I need to do int he Initialize example here, it so actually load
        >> some data into the APPLICATION object. When I try to do this in the
        >> CLASS, I noticed that the APPLICATION object is not supported. Any
        >> ideas on how to accomplish this?
        >>[/color]
        >
        >[/color]


        Comment

        • Brock Allen

          #5
          Re: Threading, using APPLICATION objects

          Well, my reason for suggesting doing it in a lazy-load manner, rather than
          the 2nd thread is there is the window of time where the data has not yet
          been loaded and a request might arrive that wants that data. Now you have
          to do locking. So, IMO, the locking is more effort and error prone (especially
          if you've not done this before) than just writing it to be lazy loaded. Now,
          of course, if the data takes seconds to load, then that's different and the
          approach you're going for is proabbaly worth that effort.

          So off the top of my head here's some pseudo code that should give you the
          idea:

          Application_Sta rt()
          {
          WorkerClass c = new WorkerClass();
          Application["WorkerClas s"] = c;
          Thread t = new Thread(c.Init);
          t.Start();
          }

          WorkerClass.cs:

          class WorkerClass
          {
          Data _data;
          Mutex Lock = new Mutex();

          void Init()
          {
          Lock.WaitOne();
          if (_data == null) _data = GetDataFromDB() ;
          Lock.ReleaseMut ex();
          }

          Data GetData()
          {
          if (_data != null) return _data;
          Lock.WaitOne();
          if (_data == null) Init();
          Lock.ReleaseMut ex();
          return _data;
          }
          }

          SomePage.aspx:

          void Page_Load()
          {
          WorkerClass c = Application["WorkerClas s"];
          Data d = c.GetData();
          }

          -Brock
          DevelopMentor

          [color=blue]
          > Brock;
          >
          > Thanks for your response. Yes, you're heading in the right direction,
          > but I need confirmation on how to pull this off, since I'm a little
          > new to all of this. I'm trying to load some data that the customer
          > will use later in the application. Since, this information is READ
          > ONLY, this THREAD is intended to be FIRE and FORGET. So, based on
          > your response, I am to assume that I need to pass the
          > APPLICATION("my Data") into the clsUserServices .SomeMethod. If so, I am
          > also assuming that you will then take the CONTEXT.APPLICA TION and do
          > what you need to, and I can read from it later in my pages without a
          > problem? I hope this clarifies it a bit.
          >
          > "Brock Allen" <ballen@NOSPAMd evelop.com> wrote in message
          > news:b8743b115a 0e78c7cd2c9f1dd fde@msnews.micr osoft.com...
          >[color=green]
          >> Can you explain a bit more what you're trying to do. It sounds like
          >> you
          >> want to load some data in a background thread so it's available
          >> later.
          >> Where do you want to save this loaded data? You said "Applicatio n",
          >> but
          >> I'm not sure if you mean the HttpApplication-derived objects or the
          >> old
          >> Application state bag held over from classic ASP. If you're talking
          >> about
          >> the later, then you can pass the Context.Applica tion over to the
          >> secondary
          >> thread (just set it as a field on your clsUserServices ). I think
          >> you're
          >> going to have a slew of sychronization problems tho. If I were doing
          >> this,
          >> I'd just do a lazy load on the data you need form the database. It'll
          >> make
          >> for less error prone code, IMO.
          >> -Brock
          >> DevelopMentor
          >> http://staff.develop.com/ballen[color=darkred]
          >>> I need to execute some threads that load items into my APPLICATION
          >>> object. I haven't figured out how to do that when I fire off a
          >>> thread on a page, that takes its time and loads data into the
          >>> APPLICATION level object which will be used later. Here is an
          >>> example code I've used to fire it off.
          >>>
          >>> Dim pclsUserService s As clsUserServices = New clsUserServices
          >>> Dim pobjUserService s As Thread
          >>> pobjThread100 = New Thread(New ThreadStart(Add ressOf
          >>> pclsUserService s.Initialize))
          >>> pobjUserService s.Start()
          >>> pobjThread100.S tart()
          >>> What I need to do int he Initialize example here, it so actually
          >>> load
          >>> some data into the APPLICATION object. When I try to do this in the
          >>> CLASS, I noticed that the APPLICATION object is not supported. Any
          >>> ideas on how to accomplish this?[/color][/color][/color]


          Comment

          Working...