473,396 Members | 1,799 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.

worker thread shutdown and Form.Invoke

I have a background worker thread which I start from a form's HandleCreated
event that makes calls back to the form using Invoke. During shutdown the
form is disposed and the background worker thread is aborted by the system.

How is one to keep the background thread from calling form.Invoke after the
form's window handle has been destroyed? This is definitely happening in my
application. I haven't read anything about this problem in old postings or
on the web. If this shouldn't happen and I'm doing something out of the
ordinary to cause this, could someone please explain why it doesn't happen
in the normal case. Thanks in advance.
If this helps, the worker thread is reading data from a TCPClient object
using the blocking Read method. The worker thread may also execute unsafe
code to process the data from the TCPClient object. Updating the UI with
information read off the TCPClient object is the worker threads only job.
So, it can abort as soon as the form goes away. The main UI thread is not
the thread which spawned the application but it is the one and only
non-background thread. The main UI thread is created before the worker
thread.

If no one has seen or heard of this problem, I'll try to write a minimal
demonstration of the problem and post the code.

Thanks,
Steve
Nov 16 '05 #1
5 8081
Stephen Lamb <sl***@easystreet.com> wrote:
I have a background worker thread which I start from a form's HandleCreated
event that makes calls back to the form using Invoke. During shutdown the
form is disposed and the background worker thread is aborted by the system.

How is one to keep the background thread from calling form.Invoke after the
form's window handle has been destroyed?


With care... it's something that I've noticed being entirely missed by
every article on threading that I've seen, and I haven't found a decent
answer to it yet, to be honest.

For a "most of the way" answer, you can give the form thread-safe
methods which do the invocation internally, and which check for a flag
before doing the invoke, returning without doing it if the form has
been closed. The flag is set in Form.Closing. I believe that still
leaves a race condition, however, and I don't know enough about the
details of the underlying message queue to know how best to sort it :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hi Stephen,
The best is to stop the thread yourself when the form closes. However
Control.Invoke won't file if the control has handle created. So
Control.IsHandleCreated gives us that piece of info, but we may go into a
racing situations. The handle is destroyed during disposal of the form. So
overriding Dispose method and using some flag should be possible to solve
that problem.The other possible points where the handle can be destoryed are
DestroyHandle (which is virtual and can be overriden) and RecreateHandle
(which unfortuanately is not virtual), but those two are protected so they
are not supposed to be called from outside.
--
Stoitcho Goutsev (100) [C# MVP]
"Stephen Lamb" <sl***@easystreet.com> wrote in message
news:10*************@corp.supernews.com...
I have a background worker thread which I start from a form's HandleCreated
event that makes calls back to the form using Invoke. During shutdown the
form is disposed and the background worker thread is aborted by the
system.

How is one to keep the background thread from calling form.Invoke after
the
form's window handle has been destroyed? This is definitely happening in
my
application. I haven't read anything about this problem in old postings
or
on the web. If this shouldn't happen and I'm doing something out of the
ordinary to cause this, could someone please explain why it doesn't happen
in the normal case. Thanks in advance.
If this helps, the worker thread is reading data from a TCPClient object
using the blocking Read method. The worker thread may also execute unsafe
code to process the data from the TCPClient object. Updating the UI with
information read off the TCPClient object is the worker threads only job.
So, it can abort as soon as the form goes away. The main UI thread is not
the thread which spawned the application but it is the one and only
non-background thread. The main UI thread is created before the worker
thread.

If no one has seen or heard of this problem, I'll try to write a minimal
demonstration of the problem and post the code.

Thanks,
Steve

Nov 16 '05 #3
Make sure you close the thread down before closing the form (i.e. in the
Closing event, etc.), and the Worker.Stop() method should handle socket
shutdown, etc. You should not leave closing event method until the
thread(s) are stopped.

--
William Stacey, MVP

"Stephen Lamb" <sl***@easystreet.com> wrote in message
news:10*************@corp.supernews.com...
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the
form is disposed and the background worker thread is aborted by the system.
How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed? This is definitely happening in my application. I haven't read anything about this problem in old postings or on the web. If this shouldn't happen and I'm doing something out of the
ordinary to cause this, could someone please explain why it doesn't happen
in the normal case. Thanks in advance.
If this helps, the worker thread is reading data from a TCPClient object
using the blocking Read method. The worker thread may also execute unsafe
code to process the data from the TCPClient object. Updating the UI with
information read off the TCPClient object is the worker threads only job.
So, it can abort as soon as the form goes away. The main UI thread is not
the thread which spawned the application but it is the one and only
non-background thread. The main UI thread is created before the worker
thread.

If no one has seen or heard of this problem, I'll try to write a minimal
demonstration of the problem and post the code.

Thanks,
Steve


Nov 16 '05 #4
"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Make sure you close the thread down before closing the form (i.e. in the
Closing event, etc.), and the Worker.Stop() method should handle socket
shutdown, etc. You should not leave closing event method until the
thread(s) are stopped.

Your solution seems simple but when I think of implementing it, I always get
either a race condition or deadlock.

My worker thread calls back to the UI thread when errors occur, if this
makes a difference. If this is not the right way to do it, could you point
me that way?

Any chance you could flesh out the detail of your solution?

Thanks much for the input.
Nov 16 '05 #5
We would be happy to help. I think the easiest way to do that is if you
post some code. Can you produce a small but complete sample the shows the
issues?

--
William Stacey, MVP

"Stephen Lamb" <sl***@easystreet.com> wrote in message
news:10*************@corp.supernews.com...
"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Make sure you close the thread down before closing the form (i.e. in the
Closing event, etc.), and the Worker.Stop() method should handle socket
shutdown, etc. You should not leave closing event method until the
thread(s) are stopped.
Your solution seems simple but when I think of implementing it, I always

get either a race condition or deadlock.

My worker thread calls back to the UI thread when errors occur, if this
makes a difference. If this is not the right way to do it, could you point me that way?

Any chance you could flesh out the detail of your solution?

Thanks much for the input.


Nov 16 '05 #6

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

Similar topics

10
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker...
12
by: Joey Powell | last post by:
Re: Original post = Windows forms - how do I get them to render/update properly? from August 22. Okay I am making some progress with being able to use delegates to run my shelled processes on...
3
by: Jacob | last post by:
I'm working on a class that needs to be called from a windows form, do it's work, and then, show progress back to the main form. I'm well aware that worker threads need to call Invoke for updates...
7
by: Brett Robichaud | last post by:
I'm trying to decide on the right approach for communication between the UI and a worker thread in a WinForms app. I am very familiar with threads in the unmanaged C++ world and in the past have...
7
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it...
2
by: joey.powell | last post by:
I have a windows forms app with a statusbar. In the form's code, I use a delegates and spawn a worker thread to get processing off of the GUI thread. Then in the worker thread I access a function...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
5
by: archana | last post by:
Hi all, I am having one confusion regarding invoking method through threading. I have delcared one delegate. and i have one class which has parameter of type delegate which i declared. I...
1
by: nicerun | last post by:
I'm using the Application_Start event at Global.asax.cs to invoke thread that do some job. I know that Application_Start event occurs when the very first request to Web Application received. -...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.