Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Windows. Forms;
Using System. Reflection;
Using System. Threading;
Namespace EbookToBBs
{
Public class Tool
{
Public static Type RtbType = typeof (RichTextBox );
/// <Summary>
/// Stop a thread. If no thread is stopped, the method does not return
/// </Summary>
/// <Param name = "thread"> </param>
Public static void StopThread (Thread thread)
{
If (thread! = Null & thread. ThreadState! = ThreadState. Stopped)
{
Thread. Abort ();
While (true)
{
If (thread. ThreadState = ThreadState. Stopped)
{
Return;
}
Thread. Sleep (10 );
}
}
}
/// <Summary>
/// Add content for RichTextBox
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "text"> </param>
Public static void AppendText (RichTextBox rtb, string text)
{
// If rtb is not operated on the UI thread
If (rtb. InvokeRequired)
{
Rtb. Invoke (new Action <string> (delegate (string txt)
{
Rtb. AppendText (txt );
Rtb. AppendText ("\ r \ n ");
}), Text );
}
Else
{
Rtb. AppendText (text );
}
}
/// <Summary>
/// Use reflection to call the RichTextBox Method
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "methodName"> </param>
Public static void Method (RichTextBox rtb, string methodName)
{
MethodInfo method = GetMethod (RtbType, methodName );
Invoke (rtb, method, null );
}
/// <Summary>
/// Use reflection to call the RichTextBox Method
/// </Summary>
/// <Param name = "rtb"> </param>
/// <Param name = "methodName"> </param>
Public static void Method (RichTextBox rtb, string methodName, Type [] typeArr, ParameterModifier [] paraModiferArr, params object [] args)
{
MethodInfo method = GetMethod (RtbType, methodName, typeArr, paraModiferArr );
If (method = null)
{
Return;
}
Invoke (rtb, method, args );
}
/// <Summary>
/// Call a method of an instance
/// </Summary>
/// <Param name = "rtb"> instance </param>
/// <Param name = "method"> method </param>
/// <Param name = "args"> parameter </param>
Public static void Invoke (RichTextBox rtb, MethodInfo method, params object [] args)
{
// If rtb is not operated on the UI thread
If (rtb. InvokeRequired)
{
Rtb. Invoke (new Action <object []> (delegate (object [] objArr)
{
Method. Invoke (rtb, objArr );
}), New object [] {args });
}
Else
{
Method. Invoke (rtb, args );
}
}
# Region method for obtaining a type
/// <Summary>
/// Obtain a method without Parameters
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "name"> </param>
/// <Returns> </returns>
Public static MethodInfo GetMethod (Type type, string name)
{
MethodInfo method = type. GetMethod (name, BindingFlags. Public | BindingFlags. IgnoreCase | BindingFlags. Instance );
Return method;
}
/// <Summary>
/// Obtain a method with Parameters
/// </Summary>
/// <Param name = "type"> </param>
/// <Param name = "name"> </param>
/// <Param name = "typeArr"> </param>
/// <Param name = "paraModiferArr"> </param>
/// <Returns> </returns>
Public static MethodInfo GetMethod (Type type, string name, Type [] typeArr, ParameterModifier [] paraModiferArr)
{
MethodInfo method = type. GetMethod (name, BindingFlags. Public | BindingFlags. IgnoreCase | BindingFlags. Instance, null, typeArr, paraModiferArr );
Return method;
}
# Endregion
}
}