Can a timer be abandoned safely - c#

Are Timers in .NET safe to abandon without calling Dispose() or Close()?
static System.Timers.Timer timer = new Timer();
void Main()
{
timer.Elapsed += LogTimer_Elapsed(object, System.Timers.ElapsedEventArgs);
timer.Start();
Thread.Sleep(10000); // Simulate doing something on main thread
}
static void LogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
DoStuff();
}
Anyone see any problems with this solution?
static QueueLogger()
{
LogQueue = new Queue<KeyValuePair<Logger, LogEntry>>(50);
LogTimer = new Timer();
LogTimer.Elapsed +=new System.Timers.ElapsedEventHandler(LogTimer_Elapsed);
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}
static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
LogTimer.Stop();
LogTimer.Dispose();
LogTimer_Elapsed(sender, null); // This is to process any remaining messages in the queue
}
static void LogTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
lock (_locker)
{
while (LogQueue.Count > 0)
{
var queuedLogger = LogQueue.Dequeue();
try
{
if (e != null) queuedLogger.Value.Message += " From " + sender.ToString();
queuedLogger.Key.Log(queuedLogger.Value);
}
catch (Exception ex)
{
OnLoggingError(queuedLogger.Key, "Async Logging error", ex);
}
}
}
}

Galford,
Yes, you can create new event which will fire if your application is about to exit e.g. OnExit and listen to the event when you application's main thread is about to close. Once that is done you can create event logic and do your timer, business or clean up logic in OnExit event. Hope this helps.
Regards

If you do not dispose of it explicitly it will be cleaned up on the garbage collector's finalizer queue. Although this is "safe" you will incur a performance penalty. It is best to dispose your timer.

From http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx :
"When a timer is no longer needed, use the Dispose method to free the resources held by the timer."
So if you want your timer to run until the end of the program, then you do not need to worry about it.

Garbage collection will cleanup up the Timer if you do not dispose of it. If the timer is in in .exe and the process will just be exited when the timer ends you really don't have to worry about. If your writing a library where the timer will be referenced you would want to dispose of it to free resources to make your library more efficient.

Calling Dispose() allows the cleanup of unmanaged resources used by your managed object before the garbage collector decides to cleanup the managed object, during which operation the unmanaged resources are also cleaned.
Calling Dispose() is usually about memory usage optimization, however, in some circumstances, not calling Dispose() will actually cause your software to not function properly. Eg: Because the number of ethernet ports is limited, not releasing them after usage may cause the system to run out of network ports. This is commonly known as "TCP/IP Port Exhaustion", and can occur when you do not call Dispose() on managed objects that use network resources (eg. WCF Clients).
As a general rule, it is always a smart idea to call Dispose() when you do no longer need any object which class implements IDisposable. (or use it in a using{} block).
In the example you provided, it seems like your timer variable is static, and bound to the main thread. It also seems like you are using your timer to the end of your program. So in this specific case, it really doesn't matter.

Related

how to close a thread containing a blocking method in C#

I create a thread to handle a blocking method in my code. this way, my code can do other things beside running that blocking method.
question: how can I terminate the thread properly? do I have to unblock the blocking method, then terminate the thread. or can I just terminate the thread without worrying about any ugly crashing?
Call yourthread.Abort(). It's not like in the old days where this would cause you to break everything when resources and locks weren't released, now this causes quite a nice exception to be raised that can be handled in the normal way...
http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx
When I say "the normal way" it seems that thread abort exception auto re-raises (quite a nice trick)
http://msdn.microsoft.com/en-us/library/system.threading.threadabortexception.aspx
but that wouldn't stop you being a douche and kicking off another big thing in the catch block...
You have some options. If you don't care if the operation completes when the application is going down you might be better off using a ThreadPool thread via QueueUserWorkItem or (as Servy suggests in comments) set the IsBackground property of your thread to true, which will allow the process to exit without the thread exiting.
If you do care about the operation completing and/or have cleanup logic that needs to be run on shutdown you probably don't really want to use Thread.Abort, at least not as your goto strategy. What I use is something similar to this:
public abstract class DisposableThread : IDisposable
{
private ManualResetEvent exiting = new ManualResetEvent(false);
private Thread theThread;
private TimeSpan abortTimeout;
public DisposableThread():
this(TimeSpan.FromMilliseconds(100))
{
}
public DisposableThread(TimeSpan abortTimeout)
{
this.abortTimeout = abortTimeout;
theThread = new Thread((_) => ThreadProc());
}
protected virtual void ThreadProc()
{
while(!exiting.WaitOne(0))
{
WorkUnit(exiting);
}
ThreadCleanup();
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
exiting.Set();
if (!theThread.Join(abortTimeout))
{
// logme -- the thread didn't shutdown gracefully
theThread.Abort();
while (!theThread.Join(1000))
{
// logme -- the thread is doing something dumb in an exception handler
}
}
exiting.Dispose();
}
// WorkUnit should return as quickly as safe if the exiting handle is set
// If it doesn't the thread will be aborted if it takes longer than abortTimeout
protected abstract void WorkUnit(WaitHandle exiting);
// override if you need to cleanup on exit
protected virtual void ThreadCleanup() { }
}
Which gives your thread a chance to exit gracefully and only aborts if a graceful exit fails.
OK,
found my answer. I gotta declare the threads so that references can be made to them. then, I end the nested thread (which has HandleClientComm()) first, then close the TCP_Client (if not null) and the TCP_Listener. then I end the ListenThread(). also, the TCP_Listener.Pending() method mentioned here Proper way to stop TcpListener must be implemented.

Dispose Unmanaged Resources in Thread

I have a class that uses unmanaged resources in a thread, it can also go to sleep when not in use. I am implementing dispose for it, please see example code below (noting it is a dumbed down version of my app). I added while(TheThread.IsAlive()); as disposed could be set to true before DestroySomeUnmangedResouces() has executed. I don't think what I have done is correct so would be grateful if someone could suggest a better model.
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//managed
}
//unmanged
_stopTheThread = true;
startTheThreadEvent.Set();
while(TheThread.IsAlive());
}
disposed = true;
}
private void TheThread()
{
while (!_stopTheThread)
{
if (state == State.Stopped)
{
// wait till a start occurs
startTheThreadEvent.WaitOne();
}
switch (state)
{
case Init:
CreateSomeUnmangedResouces();
break;
case Run:
DoStuffWithUnmangedResouces();
break;
case Stop:
DestroySomeUnmangedResouces();
break;
} // switch
}
// Release unmanaged resources when component is disposed
DestroySomeUnmangedResouces();
}
You seem to want to wait until your worker thread has exited. For this you can simply use Thread.Join() which will block until your thread has exited.
Currently you are eating 100% CPU on your wait thread because you do poll if the worker thread is still alive. A less resource intensive variant is a throttled polling where you sleep between your checks at least a timeslice (15ms).
But the by far best approach is to wait for a synchronisation primitive which gets signaled and wakes up your thread when a condtion becomes true. Thead.Join is therefore the way to go.
private readonly ManualResetEvent _stopEvent = new ManualResetEvent(false);
private readonly ManualResetEvent _threadStoppedEvent = new ManualResetEvent(false);
private bool disposed;
private int checkInterval = 10;//ms
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//managed
}
//unmanged
_stopEvent.Set();
_threadStoppedEvent.WaitOne();
}
disposed = true;
}
private void TheThread()
{
CreateSomeUnmangedResouces();
while (!_stopEvent.WaitOne(checkInterval))
{
DoStuffWithUnmangedResouces();
}
DestroySomeUnmangedResouces();
_threadStoppedEvent.Set();
}
Or you can use Thread.Join() instead of _threadStoppedEvent if your thread isn't background
The caller calling dispose should mop up the thread - the best way is to call Join on it as Alois has suggested. Once the thread has joined, then you can destroy the unmanaged resources which will now happen on the callers thread. E.g.:
protected virtual void
Dispose
(bool disposing)
{
if (!disposed)
{
if (disposing)
{
if(TheThread != null)
{
// send a signal to stop the thread.
_stopTheThread = true;
startTheThreadEvent.Set();
// Join the thread - we could timeout here but it should be the
// responsibility of the thread owner to ensure it exits
// If this is hanging then the owning object hasn't terminated
// its thread
TheThread.Join();
TheThread = null;
}
}
// Now deal with unmanaged resources!
DestroySomeUnmangedResouces();
}
disposed = true;
}
One drawback of this approach is that we are assuming the thread will eventually exit. It could hang, meaning the signals to stop the thread was not enough. There are overloads for Join which include timeouts, which could be used to prevent hanging the calling thread (see comment in code sample above).
If a running thread holds a direct or indirect strong reference to an object, such reference will prevent the object from becoming eligible for garbage collection. There's thus not really any reason to have a finalizer on such an object.
If, however, the thread will only be relevant as long as a reference some other particular object is held by something other than the thread, it may be useful for the thread to hold a WeakReference to that other object, and shut itself down if that other object goes out of scope. This shutdown could be accomplished either by having the thread periodically check the IsAlive property of the WeakReference, or by having the other object include a finalizer which would signal the thread to shut down. Although periodic polling for such things is in some sense icky, and using a finalizer could somewhat hasten the shutdown of the thread, I think polling is probably still better. While it's possible for a finalizer to notify a thread that it should do something, and there are times when doing so may be appropriate, in general the fact that an object was finalized meant that nobody was overly concerned about prompt cleanup. Adding another few seconds' delay before the thread shuts down probably won't hurt anything.

'Deadlock' with only one locked object?

I am having a problem with multi threading in C#.
I use an event to update a label in a form from another thread, for which I need to use the Invoke() command of course.
That part is also working fine.
However, the user can close the form and here the program can crash if the event is sent at an unfortunate time.
So, I thought I would simply override the Dispose() method of the form, set a boolean to true within locked code, and also check that boolean and invoke the event in locked code.
However, every time I close the form the program freezes completely.
Here are the mentioned parts of the code:
private object dispose_lock = new object();
private bool _disposed = false;
private void update(object sender, EventArgs e)
{
if (InvokeRequired)
{
EventHandler handler = new EventHandler(update);
lock (dispose_lock)
{
if (_disposed) return;
Invoke(handler); // this is where it crashes without using the lock
}
return;
}
label.Text = "blah";
}
protected override void Dispose(bool disposing)
{
eventfullObject.OnUpdate -= update;
lock (dispose_lock) // this is where it seems to freeze
{
_disposed = true; // this is never called
}
base.Dispose(disposing);
}
I hope anyone here has any idea what is wrong with this code.
Thank you in advance!
What you're not taking into account is that delegate passed to Invoke is called asynchronously on the UI thread. Calling Invoke posts a message to the forms message queue and is picked up some time later.
What happens is not:
UI Thread Background Thread
Call update()
take lock
Call Invoke()
Call update()
release lock
Call Dispose()
take lock
release lock
But instead:
UI Thread Background Thread
Call update()
take lock
Call Invoke()
block until UI Thread processes the message
Process messages
...
Dispose()
wait for lock ****** Deadlock! *****
...
Call update()
release lock
Because of this, the background thread can hold be holding the lock while the UI thread is trying to run Dispose
The solution is much simpler than what you tried. Because of the Invoke is posted asynchronously there is no need for a lock.
private bool _disposed = false;
private void update(object sender, EventArgs e)
{
if (InvokeRequired)
{
EventHandler handler = new EventHandler(update);
Invoke(handler);
return;
}
if (_disposed) return;
label.Text = "blah";
}
protected override void Dispose(bool disposing)
{
eventfullObject.OnUpdate -= update;
_disposed = true; // this is never called
base.Dispose(disposing);
}
The _disposed flag is only read or written on the UI thread so there is no need for locking. Now you call stack looks like:
UI Thread Background Thread
Call update()
take lock
Call Invoke()
block until UI Thread processes the message
Process messages
...
Dispose()
_disposed = true;
...
Call update()
_disposed is true so do nothing
One of the dangers of using Control.Invoke is that it could be disposed on the UI thread at an unfortunate time as you suggested. The most common way this occurs is when you have the following order of events
Background Thread: Queues a call back with Invoke
Foreground Thread: Dispose the Control on which the background called Invoke
Foreground Thread: Dequeues the call back on a disposed Control
In this scenario the Invoke will fail and cause an exception to be raised on the background thread. This is likely what was causing your application to crash in the first place.
With the new code though this causes a dead lock. The code will take the lock in step #1. Then the dispose happens in the UI at step #2 and it's waiting for the lock which won't be freed until after step #3 completes.
The easiest way to deal with this problem is to accept that Invoke is an operation that can and will fail hence needs a try / catch
private void update(object sender, EventArgs e)
{
if (InvokeRequired)
{
EventHandler handler = new EventHandler(update);
try
{
Invoke(handler);
}
catch (Exception)
{
// Control disposed while invoking. Nothing to do
}
return;
}
label.Text = "blah";
}
I really would go simple here. Instead of implementing tricky thread-safe code, I would simply catch the exception and do nothing if it fails.
Assuming it's a ObjectDisposedException:
try
{
this.Invoke(Invoke(handler));
}
catch (ObjectDisposedException)
{
// Won't do anything here as
// the object is not in the good state (diposed when closed)
// so we can't invoke.
}
It's simpler and pretty straightforward. If a comment specifies why you catch the exception, I think it's OK.
Why don't you just use BeginInvoke rather than Invoke - this won't block the background thread. It doesn't look like there is any specific reason why the background thread needs to wait for the UI update to occur from what you have shown
Another deadlocking scenario arises when calling Dispatcher.Invoke (in
a WPF application) or Control.Invoke (in a Windows Forms application)
while in possession of a lock. If the UI happens to be running another
method that’s waiting on the same lock, a deadlock will happen right
there. This can often be fixed simply by calling BeginInvoke instead
of Invoke. Alternatively, you can release your lock before calling
Invoke, although this won't work if your caller took out the lock. We
explain Invoke and BeginInvoke in Rich Client Applications and Thread
Affinity.
source: http://www.albahari.com/threading/part2.aspx
Just incase none of the other answers are the culprit, is there other code that terminates the thread that isn't posted? I'm thinking you might be using plain Threads and not a BackgroundWorker, and might have forgotten to set Thread.isBackround to true
IMO Dispose is too late...
I would recommend putting some code into FormClosing which is called before Dispose happens AFAIK.
For such a case I usually tend to use a different (atomic) pattern for your check - for example via the Interlocked class.
private long _runnable = 1;
private void update(object sender, EventArgs e)
{
if (InvokeRequired)
{
EventHandler handler = new EventHandler(update);
if (Interlocked.Read (ref _runnable) == 1) Invoke(handler);
return;
}
label.Text = "blah";
}
In FormClosing you just call Interlocked.Increment (ref _runnable) .

Spinning up a new Thread - do I need to care about garbage collection

I'm having a bit of a brain-freeze so I thought I'd throw this out there to the collective genius of SO...
I have an event that is raised (this will be on the thread of the "raiser") and I consume it.
However, once I am handling this event, I need to fire off another thread to perform the workload that the event signified. So, I'm using:
private void MyEventHandler(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(MyHandler));
t.Start();
}
private void MyHandler()
{
DoStuff(); // takes a long time
}
My question is: Do I need to be concerned about the lifecycle of the variable t? i.e. Can t be garbage collected, thus aborting the work being performed in DoStuff()?
There's no particular reason that you need to keep track of the Thread variable. The GC won't kill the thread when t goes out of scope.
I don't know how long "a long time" is, but you might be better off using something like ThreadPool.QueueUserWorkItem. That is:
private void MyEventHandler(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(MyHandler);
}
private void MyHandler(object state)
{
DoStuff();
}
If I understand correctly you are afraind that the Thread may be Garbage Collected while it is still working. And that would happen because after the method exits there is no reference to t.
But you need not to be afraid. As long as the thread is running there will be a reference to it and the thread will not be garbage collected.
Can the thread work and objects be GCed? Yes.
Will it happen automatically? Once the thread is complete.
But if this is a really long process that does not indicate failure back to the thread, it could potentially lock up resources indefinitely.
In these cases, a callback mechanism may be a better solution to leaving a thread open for an indefinte period.

How to avoid leaking handles when invoking in UI from System.Threading.Timer?

It seems like calling Invoke on a winforms control in a callback from a System.Threading.Timer leaks handles until the timer is disposed. Does anyone have an idea of how to work around this? I need to poll for a value every second and update the UI accordingly.
I tried it in a test project to make sure that that was indeed the cause of the leak, which is simply the following:
System.Threading.Timer timer;
public Form1()
{
InitializeComponent();
timer = new System.Threading.Timer(new System.Threading.TimerCallback(DoStuff), null, 0, 500);
}
void DoStuff(object o)
{
this.Invoke(new Action(() => this.Text = "hello world"));
}
This will leak 2 handles/second if you watch in the windows task manager.
Invoke acts like a BeginInvoke/EndInvoke pair in that it posts the message to the UI thread, creates a handle, and waits on that handle to determine when the Invoked method is complete. It is this handle that is "leaking." You can see that these are unnamed events using Process Explorer to monitor the handles while the application is running.
If IASyncResult was IDisposable, disposal of the object would take care of cleaning up the handle. As it is not, the handles get cleaned up when the garbage collector runs and calls the finalizer of the IASyncResult object. You can see this by adding a GC.Collect() after every 20 calls to DoStuff -- the handle count drops every 20 seconds. Of course, "solving" the problem by adding calls to GC.Collect() is the wrong way to address the issue; let the garbage collector do its own job.
If you do not need the Invoke call to be synchronous, use BeginInvoke instead of Invoke and do not call EndInvoke; the end-result will do the same thing but no handles will be created or "leaked."
Is there some reason you cannot use a System.Windows.Forms.Timer here? If the timer is bound to that form you won't even need to invoke.
Okay, I gave it a little more time and it looks like it's actually not leaking handles, it's just the indeterminate nature of the garbage collector. I bumped it way up to 10ms per tick and it'd climb up really fast and 30 seconds later would drop back down.
TO confirm the theory I manually called GC.Collect() on each callback (Don't do this in real projects, this was just to test, there's numerous articles out there about why it's a bad idea) and the handle count was stable.
Interesting - This is not an answer but based on Andrei's comment I would have thought this would not leak handles the same way however it does leak handles at the same rate the OP mentioned.
System.Threading.Timer timer;
public Form2()
{
InitializeComponent();
}
private void UpdateFormTextCallback()
{
this.Text = "Hello World!";
}
private Action UpdateFormText;
private void DoStuff(object value)
{
this.Invoke(UpdateFormText);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
timer = new System.Threading.Timer(new TimerCallback(DoStuff), null, 0, 500);
UpdateFormText = new Action(UpdateFormTextCallback);
}

Categories

Resources