Thread in a webservice.

  • Thread starter Thread starter jensen bredal
  • Start date Start date
J

jensen bredal

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

i wrote therefore the code below:


public class Global : System.Web.HttpApplication

{

.....

......

......


public static Thread t;




{

get { return post; }

}


public Global()

{

InitializeComponent();

}

public static String GetConnectionString

{

get { return ConfigurationSettings.AppSettings["DBConnStr"]; }

}





protected void Application_Start(Object sender, EventArgs e)

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from table");


HttpContext.Current.Cache.Insert("data",ds.Table[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.FromHours(2));


t = new Thread(new ThreadStart(ThreadProc));

t.Start();



Thread.Sleep(3600);








}



.....

......

.......


public void ThreadProc()

{

try

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from Table");


HttpContext.Current.Cache.Insert("data",ds.Tables[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.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
 
in "ThreadProc" the line:

HttpContext.Current.Cache.Insert("data",ds.Table[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.FromHours(2));




Ignacio Machin ( .NET/ C# MVP ) said:
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 said:
I need a thread that loads the content of a database table in the cache
(System.Web.Caching.Cache)

i wrote therefore the code below:


public class Global : System.Web.HttpApplication

{

....

.....

.....


public static Thread t;




{

get { return post; }

}


public Global()

{

InitializeComponent();

}

public static String GetConnectionString

{

get { return ConfigurationSettings.AppSettings["DBConnStr"]; }

}





protected void Application_Start(Object sender, EventArgs e)

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from table");


HttpContext.Current.Cache.Insert("data",ds.Table[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.FromHours(2));


t = new Thread(new ThreadStart(ThreadProc));

t.Start();



Thread.Sleep(3600);








}



....

.....

......


public void ThreadProc()

{

try

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from Table");


HttpContext.Current.Cache.Insert("data",ds.Tables[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.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
 
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 said:
in "ThreadProc" the line:

HttpContext.Current.Cache.Insert("data",ds.Table[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.FromHours(2));




Ignacio Machin ( .NET/ C# MVP ) said:
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 said:
I need a thread that loads the content of a database table in the cache
(System.Web.Caching.Cache)

i wrote therefore the code below:


public class Global : System.Web.HttpApplication

{

....

.....

.....


public static Thread t;




{

get { return post; }

}


public Global()

{

InitializeComponent();

}

public static String GetConnectionString

{

get { return ConfigurationSettings.AppSettings["DBConnStr"]; }

}





protected void Application_Start(Object sender, EventArgs e)

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from table");


HttpContext.Current.Cache.Insert("data",ds.Table[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.FromHours(2));


t = new Thread(new ThreadStart(ThreadProc));

t.Start();



Thread.Sleep(3600);








}



....

.....

......


public void ThreadProc()

{

try

{

DataSet
ds=SqlHelper.ExecuteDataset(Global.GetConnectionString,CommandType.Text,"select
* from Table");


HttpContext.Current.Cache.Insert("data",ds.Tables[0],null,Cache.NoAbsoluteExpiration,System.TimeSpan.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
 
Back
Top