Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread was being aborted

Status
Not open for further replies.

bati77

Programmer
Feb 2, 2006
4
CA
Hi,

I hope I will find the solution in this forum.
I am creating controls at run-time using the Invoke
method of the Form. (coz it does not work when I just call the method that adds the control... there are different threads in my Application)

Now... After I Remove a control from a Form 'Successfully'
and I try to Add a new control using the Invoke method of the form I get the Error: "Thread was being aborted"

Can someone pleaae tell me what is going on and how to solve that ?

Thank you in advance

Bati
 
Multi-threading in the GUI is not a good idea. When it comes to adding, removing, or interacting with a for or conrtols on a form (pretty much anything that is controlled by GDI+).

I would recommend using form.InvokeRequired and form.BeginInvoke to call a method that handles adding/removing controls.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Also - make sure you aren't calling Thread.Abort() in your code. It's generally bad news, as it doesn't give you a chance to clean up any resources (via IDisposable), so over time you get memory leaks.

If you need to tell a thread to stop, use the correct synchronization mechanisms like the ManualResetEvent and WaitHandle objects.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Cliff, I actually use Thread.Abort for our processes. So long as you keep the process in a try/catch and clean up local variables in the finally you shouldn't see much for leaks.

I've never used the ManualResetEvent or WaitHandle, would you go into more detail on them? Most of the code I've seen in MS samples on threading uses a boolean value to track when a thread should exit, but that is about worthless if the thread is stuck in an I/O wait. The nice thing about thread.abort is that it throws an exception, so the response is easy to catch and almost immediate. My users can hit the cancel button and the process ends and control returns in under a second.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I went over it in this thread796-1182906

But basically, using a boolean for synchronization is not a good idea, as the value is stored in a Int32 value, and it's theoretically possible for the CPU to get interrupted when that value is being accessed, causing a hard-to-find race condition. The .net synchronization objects in System.Threading are guaranteed to be atomic.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
He called you cliff and you din't even shout at him.

Christiaan Baes
Belgium

"Time for a new sig." - Me
 
[lol]

I've just been playing around with threading using the ThreadPool (specifically ThreadPool.QueueUserWorkItem) and that seems to work quite well and appears to be very straight-forward to implement. Has anyone used this method and does it have any pro's or con's compared to setting up the threads yourself?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
He called you cliff and you din't even shout at him.
Not a big deal.
I've been called far worse!

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Has anyone used this method and does it have any pro's or con's compared to setting up the threads yourself?
It's just a different way to accomplish similar goals.

Which to choose depends on how your app is constructed. Sometimes using the userworkitem route isn't convenient or plays well with the other code you've written.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
hey Guys...

Thanks for all your replies.

I have used the Method Invoke of my main form
to Add AND Remove controls on it and it does work fine.

Bati
 
Thanks Chip


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top