Huster Huster - 25 days ago 31
C# Question

c# SetCompatibleTextRenderingDefault must be called before the first

I tried to search for this exception but i couldn't find any solution on my case

I'am using the code Below to invoke a .NET Application :

Assembly assem = Assembly.Load(Data);
MethodInfo method = assem.EntryPoint;
var o = Activator.CreateInstance(method.DeclaringType);
method.Invoke(o, null);


the Application that will be invoked has a Form and in the EntryPoint of the Application :

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); //Exception
Application.Run(new Form1());
}


SetCompatibleTextRenderingDefault must be called before the first IWin32Window object is created in the application.

thanks in advance

Answer

You should create a new method that skips the initialization.

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false); //Exception
    Run();
}

static void Run()
{
    Application.Run(new Form1());
}

And look with reflection for the Run method. But the Application.Run will block the current thread. If you don't want to start a new message-pump, you should try to lookup the Form class with reflection.

Comments