Created
June 5, 2021 16:10
-
-
Save rajibchy/62c8f1c27a265b5a69b3002bec479978 to your computer and use it in GitHub Desktop.
Thread-safe Utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <![CDATA[copyright]]> | |
| /// Copyright (c) 2018, Sow ( https://safeonline.world, https://www.facebook.com/safeonlineworld). (https://github.com/RKTUXYN) All rights reserved. | |
| /// Copyrights licensed under the New BSD License. | |
| /// See the accompanying LICENSE file for terms. | |
| /// <![CDATA[copyright]]> | |
| /// <![CDATA[Author]]> | |
| /// By Rajib Chy | |
| /// On 6/3/2021 10:43:14 PM | |
| /// <![CDATA[Author]]> | |
| using System; | |
| using System.Linq; | |
| using System.Threading; | |
| using System.Windows.Forms; | |
| namespace Sow.Framework { | |
| public class ThreadSafe { | |
| public static void ExitThread( Thread th ) { | |
| if ( th == null ) return; | |
| try { th.Abort( ); } catch { } | |
| try { th.Join( TimeSpan.FromMilliseconds(500) ); } catch { } | |
| } | |
| public static T TryGetValue<T>( T[] data, int index ) { | |
| return data.ElementAtOrDefault( index ); | |
| //return data[ index ]; | |
| } | |
| public static void Dispose<T>(T instance) where T : IDisposable { | |
| if ( instance == null ) return; | |
| try { | |
| instance.Dispose( ); | |
| } catch(Exception e ) { | |
| Console.Error.WriteLine( $"{e.Message}\r\n{e.StackTrace}" ); | |
| } | |
| } | |
| public static void Invoke<T>( T ctrl, Action next ) where T: Control { | |
| if ( ctrl == null ) return; | |
| if ( ctrl.InvokeRequired ) { | |
| ctrl.Invoke( next, new object[] { } ); | |
| } else { | |
| next?.Invoke( ); | |
| } | |
| } | |
| public static void Invoke<T, T1>( T ctrl, Action<T1> next, T1 arg1 ) where T : Control { | |
| if ( ctrl == null ) return; | |
| if ( ctrl.InvokeRequired ) { | |
| ctrl.Invoke( next, new object[] { arg1 } ); | |
| } else { | |
| next?.Invoke( arg1 ); | |
| } | |
| } | |
| public static void Invoke<T, T1, T2>( T ctrl, Action<T1, T2> next, T1 arg1, T2 arg2 ) where T : Control { | |
| if ( ctrl == null ) return; | |
| if ( ctrl.InvokeRequired ) { | |
| ctrl.Invoke( next, new object[] { arg1, arg2 } ); | |
| } else { | |
| next?.Invoke( arg1, arg2 ); | |
| } | |
| } | |
| public static void Invoke<T, T1, T2, T3>( T ctrl, Action<T1, T2, T3> next, T1 arg1, T2 arg2, T3 arg3 ) where T : Control { | |
| if ( ctrl == null ) return; | |
| if ( ctrl.InvokeRequired ) { | |
| ctrl.Invoke( next, new object[] { arg1, arg2, arg3 } ); | |
| } else { | |
| next?.Invoke( arg1, arg2, arg3 ); | |
| } | |
| } | |
| public static void Exchange<TInstance>( ref TInstance instance, Action<TInstance> next ) where TInstance : class { | |
| if ( instance == null && next == null ) return; | |
| TInstance obj = Interlocked.Exchange( ref instance, null ); | |
| next?.Invoke( obj ); | |
| } | |
| public static TInstance Exchange<TInstance>( ref TInstance instance, TInstance transFrom = null ) where TInstance : class { | |
| if ( instance == null && transFrom == null ) return instance; | |
| return Interlocked.Exchange( ref instance, transFrom ); | |
| } | |
| public static string Exchange( ref string oldValue, string newValue = null ) { | |
| if ( oldValue == null && newValue == null ) return oldValue; | |
| return Interlocked.Exchange( ref oldValue, newValue ); | |
| } | |
| public static int Exchange( ref int oldValue, int newValue = 0 ) { | |
| return Interlocked.Exchange( ref oldValue, newValue ); | |
| } | |
| public static void Exchange<TInstance>( ref TInstance instance, TInstance transFrom, Action<TInstance> next ) where TInstance : class { | |
| if ( instance == null && transFrom == null ) return; | |
| TInstance obj = Interlocked.Exchange( ref instance, transFrom ); | |
| if ( obj == null ) return; | |
| next?.Invoke( obj ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment