I'm having some troubles with ending a group of dynamically created threads. The reason i need to end all these at any given point is so that i can refresh certain parts of my form and create new ones. Heres a simple scenario to show what happens in my threads.
A number of threads are created dynamically based upon certain variables inplace:
for (int i = 0; i <= mDevices.Count; i++)
{
ThreadStart workerThread = delegate { pollDevices(mDevices[i - 2], this); };
new Thread(workerThread).Start();
}
public void pollDevices(IDeviceInterface device, DeviceManager mainUI)
{
System.Timers.Timer timer = null;
if (timer == null)
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += delegate(object sender, ElapsedEventArgs e) { timerElapsed(sender, e, device, mainUI); };
}
timer.Enabled = true;
public void timerElapsed(object sender, ElapsedEventArgs e, IDeviceInterface device, DeviceManager mainUI)
{
device.connect(device, this);
//wait till thread finishes and destroy
Thread.CurrentThread.Abort();
}
these threads then work off of a timer, and proc an event, which handles UI updates and such. however, when i try and refresh the UI (for example if any more entries in the database need taking into account) i get errors from deleting buttons on the form (these are assigned to a thread) if a thread is still running, so before i call for the refresh i need to stop all current threads in this manner.
so my question is, how can i abort this group of threads before i refresh my UI?
You have several problems.
System.Timers.Timer
event handler will be executed on aThreadPool
thread (at least in this case) so trying to abort one of these threads is not going to end well.There are enough problems here that you are going to do some significant restructuring before we can answer your main question. One additional tip though...do not try to access or manipulate any UI elements from a thread other than the main UI thread.