473,657 Members | 2,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(thi s.getMsgs));
trd.IsBackgroun d = true;
trd.Start();
Thread.Sleep(10 00);
}
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 1540
Brent <wr********@gma il.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.co m>
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.co m>
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(thi s.getMsgs));
trd.IsBackgroun d = true;
trd.Start();
logEvent.write( "After thread start");
Thread.Sleep(10 00);
}
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********@gma il.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.co m>
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.co mwrote:
Brent <writebr...@gma il.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.co m>
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("Thre ad 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="Syst em" %>
<%@ Import Namespace="Syst em.Web" %>
<%@ Import Namespace="Syst em.Net" %>
<%@ Import Namespace="Syst em.IO" %>
<%@ Import Namespace="Syst em.Text.Regular Expressions" %>
<%@ Import Namespace="Syst em.Text" %>
<%@ Import Namespace="Syst em.Data" %>
<%@ Import Namespace="Syst em.Threading" %>
<%@ Import Namespace="tool box" %>

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

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

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

Thread trd = new Thread(new ThreadStart(thi s.getMsgs));
trd.IsBackgroun d = true;
trd.Start();
Thread.Sleep(10 00);

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

}

public void getMsgs()
{
Log log = new Log();
log.error("getM sgs 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.Cur rent.Server.Map Path("/") +
"logfiles";
if (!Directory.Exi sts(sPath))
{
Directory.Creat eDirectory(sPat h);
}

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

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

lock (lockErrorLog)
{
try
{
if (!File.Exists(s Path))
{
using (StreamWriter sw =
File.CreateText (sPath))
{
sw.WriteLine(sE rror);

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

sw.WriteLine("= =============== =============== =============== ");
sw.WriteLine("" );
}
}
}
catch { }
}
}
}
Jun 27 '08 #6
On Jun 7, 5:13 pm, Brent <writebr...@gma il.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.Cur rent, 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.co mwrote:
On Jun 7, 5:13 pm, Brent <writebr...@gma il.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.Cur rent, 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
2385
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 threading.Thread as the base class. Then I create objects of this class and call their start() methods in a loop. The program works fine when run locally in a shell. The problem starts
9
2776
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, you've >provided next to no useful (IMHO) information to >let anyone help you more than this This is about 5% of the code. Uses no locks.
31
2486
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 tipical) from the Internet and doing some processing on those would be considered to be a big/long task for a thread from a pool. In our app it is possible to break the task into some small ones (thread per fetch and processing thereafter or event...
20
2391
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 line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to each thread. Is there a way for thread A to occasionally communication to thread B that something has happened? ...
3
1478
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 an event and attempts to execute the "StartTiming" method of BELights. The method does not execute which leads me to believe that BELights is not thread-safe. (Or is it the user control that is not thread safe?) When an event fires for...
3
1528
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
12213
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
1
1377
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 application. I execute the process and the output is re- directed to a windows textbox. Below is the code I'm using to do that: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
16
8382
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 have 3 methods A, B and C. All of them shall be run threaded (I/O tasks) and one after another. I can create a thread and schedule one method just like:
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
6175
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.