0

I using ModuleInit.Fody for create code that should be invoke on load of assembly. In this code I need do some operation in task. Unfortunately there is problem with this code... code inside the task is not fired. when i used this same code after module initialization work like a charm.

Some testable code

public static class ModuleInitializer
    {
        public static AsyncCallback callback;

        public static void Initialize()
        {
            callback = ar => Console.WriteLine("Result");
            ConsoleThread(() => ModuleInitializer.RunThread(1));
            Console.WriteLine("Compleate");
            Console.ReadKey();
        }

        public static void ConsoleThread(Action inputFunct)
        {
            IAsyncResult result = inputFunct.BeginInvoke(callback, null);
        }

        public static void RunThread(int i)
        {
            Console.WriteLine($"Thread {i}");
        }

        public static void Test()
        {

        }
    }

and program.cs

    class Program
    {

        static void Main(string[] args)
        {
            ModuleInitializer.Test();
        }
    }

Do anyone know how to create code, that will be run in second thread or it is impossible? Maybe someone known any other way to create init code that could be run in thread?

Regards Szymon Szczepański

  • Multiple possible failure modes, timing is unpredictable. The most obvious one in this snippet is that the program terminates before the threadpool thread can run. – Hans Passant Nov 07 '19 at 12:49
  • @HansPassant Yes, I know that code was write as a problem example. Main problem for me is should we use other thread in moduleInitializer. Example code is crappy and I know it. But if I understand correctly what you want to say is I shouldn't try to use thread when module is loaded because of variety of possible errors? – Szymon Szczepanski Nov 08 '19 at 09:48

0 Answers0