public void Terminate(string ID) { if (!TDSThreads.ContainsKey(ID)) { return; } lock (this) { do { while (TDSThreads[ID] != null && TDSThreads[ID].IsAlive) { TDSThreads[ID].Interrupt(); TDSThreads[ID].Abort(); } TDSThreads[ID] = null; bool removed = TDSThreads.Remove(ID); if (!removed) { } } while (TDSThreads.ContainsKey(ID)); } }
public bool IsAlive(string ID) { if (ID.IsNullOrEmpty() || TDSThreads.IsNullOrEmpty()) { return(false); } try { if (!LockerRun.EnterWait()) { return(true); } var thread = TDSThreads.GetValue(ID); if (thread == null) { return(false); } return(thread.IsAlive); } catch { return(false); } finally { LockerRun.Exit(); } }
public bool IsAlive(string ID) {// try { if (TDSThreads != null && !string.IsNullOrEmpty(ID) && TDSThreads.ContainsKey(ID) && TDSThreads[ID] != null && TDSThreads[ID].IsAlive) { return(true); } else { return(false); } } catch (Exception e) { return(false); } }
public void Terminate(string ID) { if ((TDSThreads == null || !TDSThreads.ContainsKey(ID)) && (TDSTMFlags == null || !TDSTMFlags.ContainsKey(ID))) { return; } if (TDSTMFlags.ContainsKey(ID)) { TDSTMFlags[ID].IsAborting = true; } do { TDSThreads.GetValue(ID).KillInstantly(); /* * try * { * if (TDSThreads.ContainsKey(ID) && TDSThreads[ID] != null && TDSThreads[ID].IsAlive) * { * TDSThreads[ID].Interrupt(); * //TDSThreads[ID].Join(0); * TDSThreads[ID].Abort(); * } * } * catch(Exception ex) * { * Exceptions.Write(ex); * }*/ //TDSThreads.Update(ID, null); //TDSTMFlags.Update(ID, null); TDSThreads.Remove(ID); TDSTMFlags.Remove(ID); Thread.Sleep(1); } while (TDSThreads.ContainsKey(ID) || TDSTMFlags.ContainsKey(ID)); }
public bool RunF <TResult>(Expression <Func <TResult> > expression, string ID, int delay, bool Exceptions, bool waitForAccess, bool invoke) { if (expression == null) { return(false); } while (MaxThreadsCount < TDSThreads.Count) { if (!waitForAccess) { return(false); } ; Thread.Sleep(AccessWait); this.TerminateAllCompleated(); } if (System.String.IsNullOrEmpty(ID)) { ID = Expressions.nameofFull <TResult>(expression); } if (IsAlive(ID)) { return(false); } try { lock (Locker.Get("Run")) { this.Terminate(ID); Func <TResult> Function = expression.Compile(); Results.Add(ID, default(TResult)); if (Exceptions) { if (invoke) { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function.Invoke(); Results[ID] = result; })); } else { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function(); Results[ID] = result; })); } } else { if (invoke) { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function.Invoke(); Results[ID] = result; } catch { } })); } else { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function(); Results[ID] = result; } catch { } })); } } TDSThreads[ID].Priority = Priority; TDSThreads[ID].IsBackground = true; TDSThreads[ID].Start(); } } finally { } return(true); }
public bool Run(Expression <Action> EAMethod, string ID, int delay, bool Exceptions, bool waitForAccess, bool invoke) { if (EAMethod == null) { return(false); } while (MaxThreadsCount < TDSThreads.Count) { if (!waitForAccess) { return(false); } Thread.Sleep(AccessWait); this.TerminateAllCompleated(); } if (System.String.IsNullOrEmpty(ID)) { ID = Expressions.nameofFull(EAMethod); } if (IsAlive(ID)) { return(false); } try { lock (Locker.Get("Run")) { this.Terminate(ID); Action Action = EAMethod.Compile(); if (Exceptions) { if (invoke) { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } Action.Invoke(); })); } else { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } Action(); })); } } else { if (invoke) { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } Action.Invoke(); } catch { } })); } else { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } Action(); } catch { } })); } } TDSThreads[ID].Priority = Priority; TDSThreads[ID].IsBackground = true; TDSThreads[ID].Start(); } } catch (Exception e) { if (e.Message != null) { return(false); } } return(true); }
public bool RunF <TResult>(Expression <Func <TResult> > expression, string ID, int delay, bool Exceptions, bool waitForAccess) { if (expression == null) { return(false); } while (MaxThreadsCount < TDSThreads.Count) { if (!waitForAccess) { return(false); } Thread.Sleep(AccessWait); this.TerminateAllCompleated(); } if (ID.IsNullOrEmpty()) { ID = Expressions.nameofFull <TResult>(expression); } if (IsAlive(ID)) { return(false); } try { if (!LockerRun.EnterWait()) { return(false); } this.Terminate(ID); Func <TResult> Function = expression.Compile(); Results.Add(ID, default(TResult), true); if (Exceptions) { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function(); Results[ID] = result; }), true); } else { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } TResult result = Function(); Results[ID] = result; } catch { } }), true); } TDSTMFlags.Add(ID, new ThreadedMethodFlags { IsAborting = false }, true); TDSThreads[ID].Priority = Priority; TDSThreads[ID].IsBackground = true; TDSThreads[ID].Start(); } finally { LockerRun.Exit(); } return(true); }
public bool Run(Expression <Action> EAMethod, string ID, int delay, bool Exceptions, bool waitForAccess) { if (EAMethod == null) { return(false); } while (MaxThreadsCount < TDSThreads.Count) { if (!waitForAccess) { return(false); } Thread.Sleep(AccessWait); this.TerminateAllCompleated(); } if (ID.IsNullOrEmpty()) { ID = Expressions.nameofFull(EAMethod); } if (IsAlive(ID)) { return(false); } try { if (!LockerRun.EnterWait()) { return(false); } this.Terminate(ID); Action Action = EAMethod.Compile(); if (Exceptions) { TDSThreads.Add(ID, new Thread(() => { if (delay > 0) { Thread.Sleep(delay); } Action(); }), true); } else { TDSThreads.Add(ID, new Thread(() => { try { if (delay > 0) { Thread.Sleep(delay); } Action(); } catch { } }), true); } TDSTMFlags.Add(ID, new ThreadedMethodFlags { IsAborting = false }, true); TDSThreads[ID].Priority = Priority; TDSThreads[ID].IsBackground = true; TDSThreads[ID].Start(); } catch (Exception e) { if (e.Message != null) { return(false); } } finally { LockerRun.Exit(); } return(true); }