soujanya

Hi..

I'm using Visual Studio 2005. In Main() before running my main form of the App, I'm calling a function that checks for some mandatory DLLs and OCXs for the App. Depending of the bool value returned by that function, the main form of the App starts.

something like this....

static void Main()

{

try

{

if (! MyClass.MyStaticFunction() ) return;

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run( new MyMainForm());

}

catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

}

At times, when I run my App the Following Message is shown.

"Type initializer for MyClass threw an Exception"

"MyClass" is my own class which is not derived from any other class. and "MyStaticFunction()" is a static function within that class checks for the mandatory files and returns a boolean value accordingly.

What could have gone wrong and how can I avoid this error

Pls help me out. The error occurs at times, not always. How can i fix this

Thank you.




Re: Visual C# General Type Initializer Exception !

Thomas Danecker

Check the type initializer (static constructor) and the initialization of static fields of MyClass. Something is going wrong there.




Re: Visual C# General Type Initializer Exception !

soujanya

Hi Thomas...

There is no Static c'tor for MyClass. and I'm initializing the static fields where they are declared.

Shouldn't I do this






Re: Visual C# General Type Initializer Exception !

Peter Ritchie

There's nothing wrong with initializing static fields where they are declared; and this is in fact the preferred method. This essentially creates a type initializer for you (static c'tor) with the code that initializes all the static fields at the start of it.

What could be happening is one of the field initializations doesn't play nice within a type inititilizer. Try removing all the initializations of your static fields and re-add them back in one-by-one until you get the problem again. (or the other way around, remove them one by one until the problem goes away).

Otherwise, if you can post the code that intiailizes all your static fields there might be something obvious to somoeone.






Re: Visual C# General Type Initializer Exception !

soujanya

Hi Peter,

I still have the prob even after trying remove all the static fields and add them back one- after the other.

This is how the MyStaticFunction() ...

public static bool MyStaticFunction()

{

mutex = new Mutex(true, "MyApp", out App);

if( ! App) return false; // This is for checking whether the application is already running or not.

//........here goes checking the presence of all the mandatory dll and ocx files.....//

return true;

}

and....mutex is a private static variable of type Mutex and App is a private static bool type variable.

Is there anything i need to change to rectify my error First of all i don't understand when this error may appea. it's occuring at times, but not always. Pls suggest a solution.

Thank you.