473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1503
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Deepak Sarda | last post by:
Hello everyone. I have run into something which I believe is a bug or a shortcoming of the threading.Thread module. My program spawns 15 threads. For this I've creating a new class with...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
31
by: AlexeiOst | last post by:
Everywhere in documentation there are recommendations to use threads from thread pooling for relatively short tasks. As I understand, fetching a page or multiple pages (sometimes up to 50 but not...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
3
by: BoloBaby | last post by:
All, I believe I am having a threading problem. Class "BELights" is part of a larger DLL that is used by my main application. A user control (of type BESeat) within the main application raises...
3
by: Paul Rubin | last post by:
As I understand it, generators are supposed to run til they hit a yield statement: import time def f(): print 1 time.sleep(3) for i in range(2,5): yield i
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
1
by: ryan1234 | last post by:
My ultimate goal is to get something like "ping.exe" to re-direct it's standardOutput in real time to an .aspx page. I've been able to get this behavior to work just fine in a regular console...
16
by: Paul Schwann | last post by:
Hi group, I am relatively new to C# (although I have a lot of programming excperience in other languages like Java and C). Currently I am searching for a solution to this problem: Suppose you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.