Thread fails to start when called from Server.Execute

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

    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
  • Jon Skeet [C# MVP]

    #2
    Re: Thread fails to start when called from Server.Execute

    Brent <writebrent@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 - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Brent

      #3
      Re: Thread fails to start when called from Server.Execute

      >
      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

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Thread fails to start when called from Server.Execute

        Brent <writebrent@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 - <skeet@pobox.co m>
        Web site: http://www.pobox.com/~skeet
        Blog: http://www.msmvps.com/jon.skeet
        C# in Depth: http://csharpindepth.com

        Comment

        • Brent

          #5
          Re: Thread fails to start when called from Server.Execute

          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.

          Comment

          • Brent

            #6
            Re: Thread fails to start when called from Server.Execute

            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 { }
            }
            }
            }

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Thread fails to start when called from Server.Execute

              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

              Comment

              • Brent

                #8
                Re: Thread fails to start when called from Server.Execute

                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!

                Comment

                Working...