Thread in a webservice.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jensen bredal

    Thread in a webservice.


    I need a thread that loads the content of a database table in the cache
    (System.Web.Cac hing.Cache)

    i wrote therefore the code below:


    public class Global : System.Web.Http Application

    {

    .....

    ......

    ......


    public static Thread t;




    {

    get { return post; }

    }


    public Global()

    {

    InitializeCompo nent();

    }

    public static String GetConnectionSt ring

    {

    get { return ConfigurationSe ttings.AppSetti ngs["DBConnStr"]; }

    }





    protected void Application_Sta rt(Object sender, EventArgs e)

    {

    DataSet
    ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
    * from table");


    HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));


    t = new Thread(new ThreadStart(Thr eadProc));

    t.Start();



    Thread.Sleep(36 00);








    }



    .....

    ......

    .......


    public void ThreadProc()

    {

    try

    {

    DataSet
    ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
    * from Table");


    HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta bles[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));

    }

    catch(Exception ex)

    {

    string str = ex.Message; // get the exeption as a string

    }

    ....

    ......

    .....



    But!!!

    This cause the exception: "object reference not set to an instance of
    anobject"



    Can someone tell me what i'm doing wrong?



    Many thanks in advance

    JB


  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Thread in a webservice.

    Hi,

    What is the line that gives you that error?

    cheers,

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



    "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
    news:%23PMr40gV FHA.3620@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    >
    > I need a thread that loads the content of a database table in the cache
    > (System.Web.Cac hing.Cache)
    >
    > i wrote therefore the code below:
    >
    >
    > public class Global : System.Web.Http Application
    >
    > {
    >
    > ....
    >
    > .....
    >
    > .....
    >
    >
    > public static Thread t;
    >
    >
    >
    >
    > {
    >
    > get { return post; }
    >
    > }
    >
    >
    > public Global()
    >
    > {
    >
    > InitializeCompo nent();
    >
    > }
    >
    > public static String GetConnectionSt ring
    >
    > {
    >
    > get { return ConfigurationSe ttings.AppSetti ngs["DBConnStr"]; }
    >
    > }
    >
    >
    >
    >
    >
    > protected void Application_Sta rt(Object sender, EventArgs e)
    >
    > {
    >
    > DataSet
    > ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
    > * from table");
    >
    >
    > HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
    >
    >
    > t = new Thread(new ThreadStart(Thr eadProc));
    >
    > t.Start();
    >
    >
    >
    > Thread.Sleep(36 00);
    >
    >
    >
    >
    >
    >
    >
    >
    > }
    >
    >
    >
    > ....
    >
    > .....
    >
    > ......
    >
    >
    > public void ThreadProc()
    >
    > {
    >
    > try
    >
    > {
    >
    > DataSet
    > ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
    > * from Table");
    >
    >
    > HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta bles[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
    >
    > }
    >
    > catch(Exception ex)
    >
    > {
    >
    > string str = ex.Message; // get the exeption as a string
    >
    > }
    >
    > ...
    >
    > .....
    >
    > ....
    >
    >
    >
    > But!!!
    >
    > This cause the exception: "object reference not set to an instance of
    > anobject"
    >
    >
    >
    > Can someone tell me what i'm doing wrong?
    >
    >
    >
    > Many thanks in advance
    >
    > JB
    >
    >[/color]


    Comment

    • jensen bredal

      #3
      Re: Thread in a webservice.


      in "ThreadProc " the line:

      HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));




      "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > wrote
      in message news:ONC9VUiVFH A.4056@TK2MSFTN GP15.phx.gbl...[color=blue]
      > Hi,
      >
      > What is the line that gives you that error?
      >
      > cheers,
      >
      > --
      > Ignacio Machin,
      > ignacio.machin AT dot.state.fl.us
      > Florida Department Of Transportation
      >
      >
      >
      > "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
      > news:%23PMr40gV FHA.3620@TK2MSF TNGP09.phx.gbl. ..[color=green]
      >>
      >> I need a thread that loads the content of a database table in the cache
      >> (System.Web.Cac hing.Cache)
      >>
      >> i wrote therefore the code below:
      >>
      >>
      >> public class Global : System.Web.Http Application
      >>
      >> {
      >>
      >> ....
      >>
      >> .....
      >>
      >> .....
      >>
      >>
      >> public static Thread t;
      >>
      >>
      >>
      >>
      >> {
      >>
      >> get { return post; }
      >>
      >> }
      >>
      >>
      >> public Global()
      >>
      >> {
      >>
      >> InitializeCompo nent();
      >>
      >> }
      >>
      >> public static String GetConnectionSt ring
      >>
      >> {
      >>
      >> get { return ConfigurationSe ttings.AppSetti ngs["DBConnStr"]; }
      >>
      >> }
      >>
      >>
      >>
      >>
      >>
      >> protected void Application_Sta rt(Object sender, EventArgs e)
      >>
      >> {
      >>
      >> DataSet
      >> ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
      >> * from table");
      >>
      >>
      >> HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
      >>
      >>
      >> t = new Thread(new ThreadStart(Thr eadProc));
      >>
      >> t.Start();
      >>
      >>
      >>
      >> Thread.Sleep(36 00);
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >>
      >> }
      >>
      >>
      >>
      >> ....
      >>
      >> .....
      >>
      >> ......
      >>
      >>
      >> public void ThreadProc()
      >>
      >> {
      >>
      >> try
      >>
      >> {
      >>
      >> DataSet
      >> ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
      >> * from Table");
      >>
      >>
      >> HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta bles[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
      >>
      >> }
      >>
      >> catch(Exception ex)
      >>
      >> {
      >>
      >> string str = ex.Message; // get the exeption as a string
      >>
      >> }
      >>
      >> ...
      >>
      >> .....
      >>
      >> ....
      >>
      >>
      >>
      >> But!!!
      >>
      >> This cause the exception: "object reference not set to an instance of
      >> anobject"
      >>
      >>
      >>
      >> Can someone tell me what i'm doing wrong?
      >>
      >>
      >>
      >> Many thanks in advance
      >>
      >> JB
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Alvin Bruney [MVP - ASP.NET]

        #4
        Re: Thread in a webservice.

        Your cache object is null most probably. Try passing in a reference to the
        cache object to your thread.

        --
        Regards,
        Alvin Bruney - ASP.NET MVP

        [Shameless Author Plug]
        The Microsoft Office Web Components Black Book with .NET
        Now available @ www.lulu.com/owc, Amazon.com etc
        "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
        news:%23ABvwfiV FHA.3544@TK2MSF TNGP12.phx.gbl. ..[color=blue]
        >
        > in "ThreadProc " the line:
        >
        > HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
        >
        >
        >
        >
        > "Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us >
        > wrote in message news:ONC9VUiVFH A.4056@TK2MSFTN GP15.phx.gbl...[color=green]
        >> Hi,
        >>
        >> What is the line that gives you that error?
        >>
        >> cheers,
        >>
        >> --
        >> Ignacio Machin,
        >> ignacio.machin AT dot.state.fl.us
        >> Florida Department Of Transportation
        >>
        >>
        >>
        >> "jensen bredal" <jensen.bredal@ yahoo.dk> wrote in message
        >> news:%23PMr40gV FHA.3620@TK2MSF TNGP09.phx.gbl. ..[color=darkred]
        >>>
        >>> I need a thread that loads the content of a database table in the cache
        >>> (System.Web.Cac hing.Cache)
        >>>
        >>> i wrote therefore the code below:
        >>>
        >>>
        >>> public class Global : System.Web.Http Application
        >>>
        >>> {
        >>>
        >>> ....
        >>>
        >>> .....
        >>>
        >>> .....
        >>>
        >>>
        >>> public static Thread t;
        >>>
        >>>
        >>>
        >>>
        >>> {
        >>>
        >>> get { return post; }
        >>>
        >>> }
        >>>
        >>>
        >>> public Global()
        >>>
        >>> {
        >>>
        >>> InitializeCompo nent();
        >>>
        >>> }
        >>>
        >>> public static String GetConnectionSt ring
        >>>
        >>> {
        >>>
        >>> get { return ConfigurationSe ttings.AppSetti ngs["DBConnStr"]; }
        >>>
        >>> }
        >>>
        >>>
        >>>
        >>>
        >>>
        >>> protected void Application_Sta rt(Object sender, EventArgs e)
        >>>
        >>> {
        >>>
        >>> DataSet
        >>> ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
        >>> * from table");
        >>>
        >>>
        >>> HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta ble[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
        >>>
        >>>
        >>> t = new Thread(new ThreadStart(Thr eadProc));
        >>>
        >>> t.Start();
        >>>
        >>>
        >>>
        >>> Thread.Sleep(36 00);
        >>>
        >>>
        >>>
        >>>
        >>>
        >>>
        >>>
        >>>
        >>> }
        >>>
        >>>
        >>>
        >>> ....
        >>>
        >>> .....
        >>>
        >>> ......
        >>>
        >>>
        >>> public void ThreadProc()
        >>>
        >>> {
        >>>
        >>> try
        >>>
        >>> {
        >>>
        >>> DataSet
        >>> ds=SqlHelper.Ex ecuteDataset(Gl obal.GetConnect ionString,Comma ndType.Text,"se lect
        >>> * from Table");
        >>>
        >>>
        >>> HttpContext.Cur rent.Cache.Inse rt("data",ds.Ta bles[0],null,Cache.NoA bsoluteExpirati on,System.TimeS pan.FromHours(2 ));
        >>>
        >>> }
        >>>
        >>> catch(Exception ex)
        >>>
        >>> {
        >>>
        >>> string str = ex.Message; // get the exeption as a string
        >>>
        >>> }
        >>>
        >>> ...
        >>>
        >>> .....
        >>>
        >>> ....
        >>>
        >>>
        >>>
        >>> But!!!
        >>>
        >>> This cause the exception: "object reference not set to an instance of
        >>> anobject"
        >>>
        >>>
        >>>
        >>> Can someone tell me what i'm doing wrong?
        >>>
        >>>
        >>>
        >>> Many thanks in advance
        >>>
        >>> JB
        >>>
        >>>[/color]
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...