LitDev.LDCall.CallAsync C# (CSharp) Method

CallAsync() public static method

Call any extension method asynchronously. See example LDCallAsync. If dll, extension, obj and arguments are all "", then method may be a subroutine in your SmallBasic program.
public static CallAsync ( Primitive dll, Primitive extension, Primitive obj, Primitive method, Primitive arguments ) : Primitive
dll Primitive The extension dll (e.g. "LitDev.dll" or "SmallBasicLibrary.dll").
extension Primitive The extension namespace (usually the same as the dll name, e.g. "LitDev" or "MicroSoft.SmallBasic.Library" for SmallBasicLibrary.dll).
obj Primitive The extension object name.
method Primitive The extension method name.
arguments Primitive An array of arguments or "" for none. A single argument doesn't have to be in an array.
return Primitive
        public static Primitive CallAsync(Primitive dll, Primitive extension, Primitive obj, Primitive method, Primitive arguments)
        {
            try
            {
                Type type;
                string callName = "";
                if (dll == "" && extension == "" && obj == "" && arguments == "")
                {
                    type = mainModule;
                }
                else
                {
                    string path = Path.GetDirectoryName(entryAssembly.Location) + "\\" + dll;
                    if (!System.IO.File.Exists(path)) return dll + " dll not found";
                    Assembly assembly = Assembly.LoadFrom(path);
                    type = assembly.GetType(extension + "." + obj, false, true);
                    if (null == type) return extension + "." + obj + " extension not found";
                    callName += extension + "." + obj + ".";
                }
                MethodInfo methodInfo = type.GetMethod((string)method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.IgnoreCase);
                if (null == methodInfo) return method + " method not found";

                int numArgs = 0;
                Object[] args = null;
                callName += method + "(";
                if (SBArray.IsArray(arguments))
                {
                    numArgs = SBArray.GetItemCount(arguments);
                    args = new Object[numArgs];
                    Primitive indices = SBArray.GetAllIndices(arguments);
                    for (int i = 1; i <= numArgs; i++)
                    {
                        args[i - 1] = arguments[indices[i]];
                        callName += arguments[indices[i]];
                        if (i < numArgs) callName += ",";
                    }
                }
                else if (arguments != "")
                {
                    args = new Object[1] { arguments };
                    callName += arguments;
                }
                callName += ")";

                Thread thread = new Thread(new ParameterizedThreadStart(DoCall));
                thread.Start(new Object[] { methodInfo, args, callName });
                //while (!thread.IsAlive) Thread.Sleep(1); //delay to let async call get started
                return "PENDING";
            }
            catch (Exception ex)
            {
                Utilities.OnError(Utilities.GetCurrentMethod(), ex);
                return ex.Message;
            }
        }