471,325 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Thread fails to start when called from Server.Execute

Page1.aspx calls Page2.aspx like this:

----------------
Server.Execute("Page2.aspx");
---------------

Page2.aspx's Page_Load event calls a function, getMsgs().This function
runs normally when it is called directly, as in

---------------
getMsgs()
---------------

However, when called on a thread, it doesn't appear to start. I'm
really at a loss to know where to check or monitor the thread for
errors. The threading code looks like this:

--------------
try
{
Thread trd = new Thread(new
ThreadStart(this.getMsgs));
trd.IsBackground = true;
trd.Start();
Thread.Sleep(1000);
}
catch (Exception ex)
{
logEvent.write("Could not start getMsgs. Error: " +
ex.Message);//custom event logger

}

---------------

I'd very much appreciate any tips you may have.

Thanks.

--Brent
Jun 27 '08 #1
7 1272
Brent <wr********@gmail.comwrote:
Page1.aspx calls Page2.aspx like this:

----------------
Server.Execute("Page2.aspx");
---------------

Page2.aspx's Page_Load event calls a function, getMsgs().This function
runs normally when it is called directly, as in

---------------
getMsgs()
---------------

However, when called on a thread, it doesn't appear to start. I'm
really at a loss to know where to check or monitor the thread for
errors. The threading code looks like this:
I very much doubt that the thread is really failing to start. When you
say "it doesn't appear to start" how are you determining that?

Have you put logging in at the start of the getMsgs() method?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #2
>
I very much doubt that the thread is really failing to start. When you
say "it doesn't appear to start" how are you determining that?

Have you put logging in at the start of the getMsgs() method?

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
Yes. That's the odd thing. I have done something like this:

try
{
logEvent.write("Before thread start");
Thread trd = new Thread(new
ThreadStart(this.getMsgs));
trd.IsBackground = true;
trd.Start();
logEvent.write("After thread start");
Thread.Sleep(1000);
}
catch (Exception ex)
{
logEvent.write("Could not start getMsgs. Error: " +
ex.Message);//custom event logger

}

....and then, in getMsgs()
-----------------------

void getMsgs()
{
logEvent.write("getMsgs started.");
}

---------------------

....only to find that the getMsgs event log entry ("getMsgs started")
never got written. The two event log entries in the Page_Load event
did appear, however.

I read somewhere that a StackTrace could sometimes prevent a thread
from running properly, so I removed all that from my error reporting,
in hopes a little voodoo might help. Nothing.

I'm a bit at a loss, really.

Thanks for your reply!

--Brent
Jun 27 '08 #3
Brent <wr********@gmail.comwrote:

<snip>
...only to find that the getMsgs event log entry ("getMsgs started")
never got written. The two event log entries in the Page_Load event
did appear, however.
How is logEvent declared? Is it thread-static or anything like that? If
you use a debugger and put a breakpoint in getMsgs(), does that ever
get hit?
I read somewhere that a StackTrace could sometimes prevent a thread
from running properly, so I removed all that from my error reporting,
in hopes a little voodoo might help. Nothing.
I'd want to see some more detail about that - it sounds pretty
unlikely.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Jun 27 '08 #4
On Jun 7, 2:00 am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Brent <writebr...@gmail.comwrote:

<snip>
...only to find that the getMsgs event log entry ("getMsgs started")
never got written. The two event log entries in the Page_Load event
did appear, however.

How is logEvent declared? Is it thread-static or anything like that? If
you use a debugger and put a breakpoint in getMsgs(), does that ever
get hit?
I read somewhere that a StackTrace could sometimes prevent a thread
from running properly, so I removed all that from my error reporting,
in hopes a little voodoo might help. Nothing.

I'd want to see some more detail about that - it sounds pretty
unlikely.

--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon.skeet
C# in Depth:http://csharpindepth.com
logEvent is a static method declared in a DLL referenced on the page.
I also tried logging with another custom logging method that isn't
static, but there was no difference, e.g. the log.error line here
wrote nothing:

void getMsgs()
{
Log log = new Log();

log.error("Thread started");

}

I'm confused, I must say.
Jun 27 '08 #5
I really appreciate your help.

For some further information, I've simplified the page to this, and it
still doesn't work:
--------------------------------------------------------

<%@ Page Language="C#" EnableViewState="false"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="toolbox" %>

<script language="C#" runat="server">

public void Page_Load(Object sender, EventArgs e)
{
Log log = new Log();

log.error("before thread"); <--write OK

Thread trd = new Thread(new ThreadStart(this.getMsgs));
trd.IsBackground = true;
trd.Start();
Thread.Sleep(1000);

log.error("after thread"); //<---write OK

}

public void getMsgs()
{
Log log = new Log();
log.error("getMsgs started");// <---never does anything
}

</script>
---------------------------------------------

The Log class, declared in toolbox.dll, looks like this:

-------------------------------------------

public class Log
{

static readonly object lockErrorLog = new object();
public void error(string sError)
{
string sPath = HttpContext.Current.Server.MapPath("/") +
"logfiles";
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}

string sLogName = DateTime.Now.ToString("yyyy-MM-dd");

sPath = sPath + "\\Errors_" + sLogName + ".txt";

lock (lockErrorLog)
{
try
{
if (!File.Exists(sPath))
{
using (StreamWriter sw =
File.CreateText(sPath))
{
sw.WriteLine(sError);

sw.WriteLine("==================================== ==========");
sw.WriteLine("");
}
}
else
{
using (StreamWriter sw =
File.AppendText(sPath))
{
sw.WriteLine(sError);

sw.WriteLine("==================================== ==========");
sw.WriteLine("");
}
}
}
catch { }
}
}
}
Jun 27 '08 #6
On Jun 7, 5:13 pm, Brent <writebr...@gmail.comwrote:
I really appreciate your help.

For some further information, I've simplified the page to this, and it
still doesn't work:
<snip>

It's the logging that's the problem. Where it's using
HttpContext.Current, that's a per-thread context - the new thread
doesn't *have* an HttpContext.

Try logging by writing to a hard-coded path in a way which doesn't use
*anything* from ASP.NET - make sure you have access to the path from
within the ASP.NET account though. I'm pretty sure you'll find it logs
okay then.

Jon
Jun 27 '08 #7
On Jun 7, 9:55 am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jun 7, 5:13 pm, Brent <writebr...@gmail.comwrote:
I really appreciate your help.
For some further information, I've simplified the page to this, and it
still doesn't work:

<snip>

It's the logging that's the problem. Where it's using
HttpContext.Current, that's a per-thread context - the new thread
doesn't *have* an HttpContext.

Try logging by writing to a hard-coded path in a way which doesn't use
*anything* from ASP.NET - make sure you have access to the path from
within the ASP.NET account though. I'm pretty sure you'll find it logs
okay then.

Jon
Jon:

This did it. I added a new entry to Web.config, and that effectively
hard-coded the path.

This bug I don't know I would have ever caught. It wouldn't have shown
up in any try-catch block inside a thread, and have been invisible to
me. Thanks to your insight, I'm back on track.

Thanks!
Jun 27 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Deepak Sarda | last post: by
9 posts views Thread by phil | last post: by
31 posts views Thread by AlexeiOst | last post: by
3 posts views Thread by Paul Rubin | last post: by
1 post views Thread by ryan1234 | last post: by
16 posts views Thread by Paul Schwann | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.