Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# SetCompatibleTextRenderingDefault must be called before the first

Tags:

c#

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.

EDIT :

        Assembly a = Assembly.Load(Data);
        MethodInfo method = a.GetType().GetMethod("Start");
        var o = Activator.CreateInstance(method.DeclaringType);            
        method.Invoke(o, null);
like image 812
Huster Avatar asked Nov 08 '16 15:11

Huster


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

You should create a new method that skips the initialization and look with reflection for the Start method. But the Application.Start 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.


class Program
{
    static void Main(string[] args)
    {
        var path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        var filename = Path.Combine(path, "WindowsFormsApplication1.exe");
        var assembly = Assembly.LoadFile(filename);
        var programType = assembly.GetTypes().FirstOrDefault(c => c.Name == "Program"); // <-- if you don't know the full namespace and when it is unique.
        var method = programType.GetMethod("Start", BindingFlags.Public | BindingFlags.Static);
        method.Invoke(null, new object[] { });
    }
}

And the loading assembly:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Start();
    }


    public static void Start()   // <-- must be marked public!
    {
        MessageBox.Show("Start");
        Application.Run(new Form1());
    }
}

This works here!

like image 185
Jeroen van Langen Avatar answered Nov 15 '22 12:11

Jeroen van Langen