Thread and object scope

  • Thread starter Thread starter R.A.
  • Start date Start date
R

R.A.

private void ArchiveTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)

{

Thread thethread = new Thread (new ThreadStart
(processFile.ClearArchiveFilesThreadProc));

thethread.Start ();

}



Is this code ok, or will there be some issues after ArchiveTimer_Elapsed
function goes out of scope?





Thanks
 
Well, assuming you've considered everything else that goes with threading,
this will work. It doesn't matter that you go out of scope in the timer
event, you're creating the thread there.
 
Hi R.A.

There is no problem with the scope. As long as the thread is alive the all
objects involved are referenced and GC won't touch them.

The only think to consider is that Elapsed event handler is alreadt executed
in a worker thread so you might not need to start a new one.
 
If the SynchronizingObject property of the Systems.Timers.Timer component is
set, the Elapsed event will be fired on the thread that owns the
SynchronizingObject (typically the user interface thread). When a
Systems.Timers.Timer component is placed on a form or usercontrol designer,
the SynchronizingObject property is automatically assigned. Oddly, it seems
you can't "unset" the SynchronizingObject property in the Properties Window,
but you can do it in code, of course.

Regards,
Daniel
 
Yes, you are right in tnis case.

--

Stoitcho Goutsev (100) [C# MVP]


Daniel Pratt said:
If the SynchronizingObject property of the Systems.Timers.Timer component is
set, the Elapsed event will be fired on the thread that owns the
SynchronizingObject (typically the user interface thread). When a
Systems.Timers.Timer component is placed on a form or usercontrol designer,
the SynchronizingObject property is automatically assigned. Oddly, it seems
you can't "unset" the SynchronizingObject property in the Properties Window,
but you can do it in code, of course.

Regards,
Daniel
 
Back
Top