ThreadAbortException as soon as breakpoint reached in thread

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

    ThreadAbortException as soon as breakpoint reached in thread

    When setting a breakpoint in a thread that I have created and the debugger
    reaches that point, Visual Studio just hangs for about 10 seconds and then,
    when I try F10 the thread ends and the debug output shows a
    ThreadAbortExce ption. I'm not doing any other operation than .Start() on the
    thread. What is wrong?

    m_thread = new Thread(DataInTh read);
    m_thread.Name = "Data in thread";
    m_thread.Start( new object[] { l_frm.COMport, 9600 });


    public void DataInThread(ob ject parameter)
    {
    //Any breakpoint in this function, irrellevant of where it is placed, stops
    the execution but with a delay of about 10 seconds, and then aborts abruptly
    when pressing F10
  • Joachim

    #2
    RE: ThreadAbortExce ption as soon as breakpoint reached in thread

    It is a Windows application, and I have heard that creating threads in
    Windows applications can be difficult to get right but I don't know why. How
    can I use threads so that they work together with the GUI?

    "Joachim" wrote:
    [color=blue]
    > When setting a breakpoint in a thread that I have created and the debugger
    > reaches that point, Visual Studio just hangs for about 10 seconds and then,
    > when I try F10 the thread ends and the debug output shows a
    > ThreadAbortExce ption. I'm not doing any other operation than .Start() on the
    > thread. What is wrong?
    >
    > m_thread = new Thread(DataInTh read);
    > m_thread.Name = "Data in thread";
    > m_thread.Start( new object[] { l_frm.COMport, 9600 });
    >
    >
    > public void DataInThread(ob ject parameter)
    > {
    > //Any breakpoint in this function, irrellevant of where it is placed, stops
    > the execution but with a delay of about 10 seconds, and then aborts abruptly
    > when pressing F10[/color]

    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: ThreadAbortExce ption as soon as breakpoint reached in thread

      Joachim wrote:[color=blue]
      > It is a Windows application, and I have heard that creating threads in
      > Windows applications can be difficult to get right but I don't know why. How
      > can I use threads so that they work together with the GUI?[/color]

      See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

      If you could post a short but complete program which demonstrates the
      problem you're having, that would help. See
      http://www.pobox.com/~skeet/csharp/complete.html for more information.

      Jon

      Comment

      • Joachim

        #4
        Re: ThreadAbortExce ption as soon as breakpoint reached in thread

        Lets say you have a GUI thread. When pressing a button the GUI thread creates
        a thread which listens to a COM port. When the COM port listening thread gets
        input from the COM port it wants to use this information from the COM port to
        display in the GUI. Which is the best way to do this?

        E g

        class MyGUIclass
        {
        public delegate void DataInput(objec t sender, StringEventArgs evArgs);
        public event DataInput OnDataInput;

        public MyGUIclass()
        {
        InitializeCompo nent();
        OnDataInput += new DataInput(DataI nputHandler);
        }
        private void button_click(ob ject sender, EventArgs e)
        {
        ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Da taInThread), new
        object[] { "COM1", 9600 });
        }
        }

        protected void DataInThread(ob ject parameter)
        {
        if (parameter.GetT ype() != typeof(object[]))
        {
        return;
        }

        object[] l_params = (object[]) parameter;
        if (l_params.Lengt h != 2)
        {
        return;
        }

        if (((object[])parameter)[0].GetType() != typeof(string))
        {
        return;
        }
        if (((object[])parameter)[1].GetType() != typeof(int))
        {
        return;
        }

        try
        {
        SerialPortHandl er l_spHandler = new
        SerialPortHandl er((string) l_params[0], (int) l_params[1]);
        l_spHandler.Rea dTimeout = 500;

        try
        {
        m_dataInThreadI sRunning = true;
        while (m_dataInThread IsRunning)
        {
        try
        {
        string l_str = l_spHandler.Rea dLine();
        //For some reason it hangs on the row below about
        10
        //seconds before it enters the DataInputHandle r
        //I suspect this has something to do with that
        OnDataInput
        //is a part of the GUI thread and here I'm trying to
        use it
        // in a sub-thread of the GUI thread. How to solve this?
        OnDataInput(thi s, new StringEventArgs (l_str));
        }
        catch (TimeoutExcepti on)
        {
        }
        catch (InvalidOperati onException)
        {
        }
        }
        }
        catch (IOException)
        {
        m_dataInThreadI sRunning = false;
        }
        l_spHandler.Dis pose();
        }
        catch (UnauthorizedAc cessException e)
        {
        m_dataInThreadI sRunning = false;
        MessageBox.Show (e.Message);
        }
        }


        protected void DataInputHandle r(object sender, EventArgs evArgs)
        {
        /*Some code*/
        }



        "Jon Skeet [C# MVP]" wrote:
        [color=blue]
        > Joachim wrote:[color=green]
        > > It is a Windows application, and I have heard that creating threads in
        > > Windows applications can be difficult to get right but I don't know why. How
        > > can I use threads so that they work together with the GUI?[/color]
        >
        > See http://www.pobox.com/~skeet/csharp/t...winforms.shtml
        >
        > If you could post a short but complete program which demonstrates the
        > problem you're having, that would help. See
        > http://www.pobox.com/~skeet/csharp/complete.html for more information.
        >
        > Jon
        >
        >[/color]

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: ThreadAbortExce ption as soon as breakpoint reached in thread

          Joachim wrote:[color=blue]
          > Lets say you have a GUI thread. When pressing a button the GUI thread creates
          > a thread which listens to a COM port. When the COM port listening thread gets
          > input from the COM port it wants to use this information from the COM port to
          > display in the GUI. Which is the best way to do this?[/color]

          You'll need to use InvokeRequired/BeginInvoke/Invoke to make sure you
          access the UI on the correct thread, as per the article I pointed you
          at.

          Jon

          Comment

          Working...