473,396 Members | 2,089 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.

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
ThreadAbortException. I'm not doing any other operation than .Start() on the
thread. What is wrong?

m_thread = new Thread(DataInThread);
m_thread.Name = "Data in thread";
m_thread.Start(new object[] { l_frm.COMport, 9600 });
public void DataInThread(object 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
Apr 25 '06 #1
4 1803
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:
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
ThreadAbortException. I'm not doing any other operation than .Start() on the
thread. What is wrong?

m_thread = new Thread(DataInThread);
m_thread.Name = "Data in thread";
m_thread.Start(new object[] { l_frm.COMport, 9600 });
public void DataInThread(object 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

Apr 26 '06 #2
Joachim wrote:
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?


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

Apr 26 '06 #3
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(object sender, StringEventArgs evArgs);
public event DataInput OnDataInput;

public MyGUIclass()
{
InitializeComponent();
OnDataInput += new DataInput(DataInputHandler);
}
private void button_click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DataInThread), new
object[] { "COM1", 9600 });
}
}

protected void DataInThread(object parameter)
{
if (parameter.GetType() != typeof(object[]))
{
return;
}

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

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

try
{
SerialPortHandler l_spHandler = new
SerialPortHandler((string) l_params[0], (int) l_params[1]);
l_spHandler.ReadTimeout = 500;

try
{
m_dataInThreadIsRunning = true;
while (m_dataInThreadIsRunning)
{
try
{
string l_str = l_spHandler.ReadLine();
//For some reason it hangs on the row below about
10
//seconds before it enters the DataInputHandler
//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(this, new StringEventArgs(l_str));
}
catch (TimeoutException)
{
}
catch (InvalidOperationException)
{
}
}
}
catch (IOException)
{
m_dataInThreadIsRunning = false;
}
l_spHandler.Dispose();
}
catch (UnauthorizedAccessException e)
{
m_dataInThreadIsRunning = false;
MessageBox.Show(e.Message);
}
}
protected void DataInputHandler(object sender, EventArgs evArgs)
{
/*Some code*/
}

"Jon Skeet [C# MVP]" wrote:
Joachim wrote:
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?


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

Apr 26 '06 #4
Joachim wrote:
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?


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

Apr 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
0
by: Mark Phillips | last post by:
Hello, I am having a problem in which a file can get stuck open when a thread that is attempting to write to it gets aborted (ThreadAbortedException occurs). The log file gets stuck open until...
4
by: Wal Turner | last post by:
Consider the following simple class: public class ClassA { public static string VAR = "hello"; } public void MethodA() { while(true)
4
by: Vivek | last post by:
Hi, I have a question regarding ThreadAbortException. Why is the thread abort exception rethrown at the end of a catch clause? Why is ThreadAbortException's behavior designed to be this way? ...
6
by: David Waz... | last post by:
Moved an app from W/2000 Asp V1.0 to W/2003, VS/2003, ASPV 1.1 Page runs a long job, uploading 2 large fixed length files (300,000 rows) into SQL database. A process is run against the data,...
0
by: Chee | last post by:
Hi all Please forgive me if this has been asked before. I am getting a ThreadAbortException in "Unknown Module" when i try to load a page from VS.NET. One problem is it does not happen all the...
4
by: splap20 | last post by:
Every day a large chunk of XML is posted up to my app via http. The app interprets this XML and writes results to a SQLServer. Occasionally a ThreadAbortException is thrown during this process....
6
by: foolmelon | last post by:
If a childThread is in the middle of a catch block and handling an exception caught, the main thread calls childThread.Abort(). At that time a ThreadAbortException is thrown in the childThread. ...
3
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
Hi, I am running a web service which sometimes throws exceptions. I have a lot of error trapping within the web service, but I have missed the current problem. I am working on the current issue,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.