Cancelling My Thread (from within)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?anAybXNmdA==?=

    Cancelling My Thread (from within)

    I've got a thread that pulls data from the SQL Server.

    After running a query, if the DataTable has records, I go on to process them.

    If the DataTable does not have records, I want to exit the thread.

    How do I exit the thread? I have tried:
    /*************** **************/
    if (table.Rows.Cou nt == 0) {
    e.Cancel = true; // DoWorkEvenArgs e
    e.Result = "No Records were found";
    return;
    }
    // continue with record processing part of thread
    /*************** **************/

    However, I get an exception thrown that my app can not handle, and it throws
    it all the way up to Program.cs:

    try {
    Application.Run (new MdiForm());
    } catch (Exception e) {
    MessageBox.Show (e.Message);
    }

    e.Message = "Exception has been thrown by the target of an invocation."

    That doesn't help me much, but maybe someone here can use it.
  • Pavel Minaev

    #2
    Re: Cancelling My Thread (from within)

    On Aug 15, 11:05 pm, jp2msft <jp2m...@discus sions.microsoft .com>
    wrote:
    I've got a thread that pulls data from the SQL Server.
    >
    After running a query, if the DataTable has records, I go on to process them.
    >
    If the DataTable does not have records, I want to exit the thread.
    >
    How do I exit the thread? I have tried:
    /*************** **************/
    if (table.Rows.Cou nt == 0) {
      e.Cancel = true; // DoWorkEvenArgs e
      e.Result = "No Records were found";
      return;}
    >
    // continue with record processing part of thread
    /*************** **************/
    >
    However, I get an exception thrown that my app can not handle, and it throws
    it all the way up to Program.cs:
    >
    try {
      Application.Run (new MdiForm());} catch (Exception e) {
    >
      MessageBox.Show (e.Message);
    >
    }
    >
    e.Message = "Exception has been thrown by the target of an invocation."
    >
    That doesn't help me much, but maybe someone here can use it.
    You should have a look at the InnerException of that exception - it
    will tell you the actual cause of the problem.

    Other than that, what was the actual question?

    Comment

    • Peter Duniho

      #3
      Re: Cancelling My Thread (from within)

      On Fri, 15 Aug 2008 12:05:03 -0700, jp2msft
      <jp2msft@discus sions.microsoft .comwrote:
      [...]
      How do I exit the thread? I have tried:
      /*************** **************/
      if (table.Rows.Cou nt == 0) {
      e.Cancel = true; // DoWorkEvenArgs e
      e.Result = "No Records were found";
      return;
      }
      [...]
      e.Message = "Exception has been thrown by the target of an invocation."
      >
      That doesn't help me much, but maybe someone here can use it.
      What is the InnerException of that exception? That should lead you to
      more information as to why the exception actually happened. Since you
      didn't post all of the code, it's practically impossible for us to know
      exactly what went wrong. We can make some guesses, but there's usually
      just as much chance of guessing wrong as right, so I generally don't like
      to bother.

      That said, I don't think you really want to set the Cancel property when
      you exit your thread. That's usually set when the background task was
      explicitly cancelled. That didn't happen in this case; you just didn't
      find anything to process. If for no other reason than that if the DoWork
      handler cancels the event, you can't access the Result property without an
      exception, it seems counter-productive to me to be setting the Cancel
      property.

      Pete

      Comment

      • =?Utf-8?B?anAybXNmdA==?=

        #4
        RE: Cancelling My Thread (from within)

        Solved it myself, actually.

        Every time my Thread_RunWorke rCompleted attempted to read the reason
        specified by e.Reason below, it threw an exception.

        My Solution: I stopped reading it!

        "jp2msft" wrote:
        I've got a thread that pulls data from the SQL Server.
        >
        After running a query, if the DataTable has records, I go on to process them.
        >
        If the DataTable does not have records, I want to exit the thread.
        >
        How do I exit the thread? I have tried:
        /*************** **************/
        if (table.Rows.Cou nt == 0) {
        e.Cancel = true; // DoWorkEvenArgs e
        e.Result = "No Records were found";
        return;
        }
        // continue with record processing part of thread
        /*************** **************/
        >
        However, I get an exception thrown that my app can not handle, and it throws
        it all the way up to Program.cs:
        >
        try {
        Application.Run (new MdiForm());
        } catch (Exception e) {
        MessageBox.Show (e.Message);
        }
        >
        e.Message = "Exception has been thrown by the target of an invocation."
        >
        That doesn't help me much, but maybe someone here can use it.

        Comment

        • Peter Duniho

          #5
          Re: Cancelling My Thread (from within)

          On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
          <jp2msft@discus sions.microsoft .comwrote:
          Solved it myself, actually.
          >
          Every time my Thread_RunWorke rCompleted attempted to read the reason
          specified by e.Reason below, it threw an exception.
          >
          My Solution: I stopped reading it!
          If you're not going to read it, why do you set it?

          Comment

          • =?Utf-8?B?anAybXNmdA==?=

            #6
            Re: Cancelling My Thread (from within)

            "Peter Duniho" wrote:
            On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
            If you're not going to read it, why do you set it?
            >
            Actually, since I couldn't figure out how to read it, I stopped setting it,
            too!

            Is there a way to set the e.Result field so that my RunWorkerComple ted event
            *can* read this back in?

            Comment

            • Obaum1

              #7
              Re: Cancelling My Thread (from within)

              On Aug 15, 10:05 pm, jp2msft <jp2m...@discus sions.microsoft .com>
              wrote:
              I've got a thread that pulls data from the SQL Server.
              >
              After running a query, if the DataTable has records, I go on to process them.
              >
              If the DataTable does not have records, I want to exit the thread.
              >
              How do I exit the thread? I have tried:
              /*************** **************/
              if (table.Rows.Cou nt == 0) {
                e.Cancel = true; // DoWorkEvenArgs e
                e.Result = "No Records were found";
                return;}
              >
              // continue with record processing part of thread
              /*************** **************/
              >
              However, I get an exception thrown that my app can not handle, and it throws
              it all the way up to Program.cs:
              >
              try {
                Application.Run (new MdiForm());} catch (Exception e) {
              >
                MessageBox.Show (e.Message);
              >
              }
              >
              e.Message = "Exception has been thrown by the target of an invocation."
              >
              That doesn't help me much, but maybe someone here can use it.
              Hello

              Why are you want to manage threads when you can get stuff asynchrony
              and use the complete event and if e.result == 0
              Do what you want
              And use the .net timer to run this function

              Comment

              • Peter Duniho

                #8
                Re: Cancelling My Thread (from within)

                On Fri, 15 Aug 2008 14:42:02 -0700, jp2msft
                <jp2msft@discus sions.microsoft .comwrote:
                "Peter Duniho" wrote:
                >On Fri, 15 Aug 2008 14:16:09 -0700, jp2msft
                >If you're not going to read it, why do you set it?
                >>
                >
                Actually, since I couldn't figure out how to read it, I stopped setting
                it,
                too!
                >
                Is there a way to set the e.Result field so that my RunWorkerComple ted
                event
                *can* read this back in?
                Yes. As I noted in my previous reply, just don't set
                DoWorkEventArgs .Cancel. I don't think that's appropriate in your
                situation anyway.

                Pete

                Comment

                Working...