Patrick8

I'm writing a program that launches another application (among other things), and it was doing fine until the developers of the other application changed Application.Run(ParentForm) to ParentForm.Show(). After reading quite a bit of documentation on these two functions, I'm still at a loss as to why it would cause my program to break. However, it breaks quite dramatically, with a LoderLock error that says I'm trying to run managed code inside DllMain. This code runs fine:

t = new Thread(new ThreadStart(StartThread));
t.ApartmentState = ApartmentState.STA;
t.Start();

but the error occurs in StartThread(), which is a function that simply instantiates the program's startup class and then calls the Main() function within that object.

Can anyone advise how such a small change could cause something that was working fine to break completely



Re: Windows Forms General Application.Run vs Form.Show?

Christopher Payne

That's an interesting change to make in an application. Application.Run starts a message loop, which enables the application to continue running after the Main() method completes. Since they're still showing a form, they must be handling their message loop in some other way.

Perhaps they made some other change that makes it not compatible with an STA thread





Re: Windows Forms General Application.Run vs Form.Show?

Patrick8

One would think so, but when that was checked into source control, nothing was checked in that would handle messages. A lot of project settings were changed when they changed this. Could those settings have any bearing on this If so, I'll go through and document the changes, then post the information here. thanks!





Re: Windows Forms General Application.Run vs Form.Show?

Patrick8

Here's a little more info on the application I'm trying to launch:

The application in question has Public Sub Main in it's startup class. It's a windows application. In Main, it:

  1. makes a function call to an instance of another class
  2. instantiates yet another class
  3. initializes the object it just instantiated
  4. instantiates the form using "Dim" and "New"
  5. member variable = form (using "Me." -- perhaps this is to keep the form from being destoryed when Main finishes )
  6. form.Show() (this used to be Application.Run(form))




Re: Windows Forms General Application.Run vs Form.Show?

Christopher Payne

Oh, so they are using Application.Run, not form.Show That should be correct.

You'll have to do some error trapping and perhaps stepping through the code to see exactly where the problem is. Put a breakpoing in your StartThread method, and step through it to see where the problem is. It could be in StartThread, or it could be in the startup of the application you're launching.





Re: Windows Forms General Application.Run vs Form.Show?

Patrick8

No, I had written it backwards. They used to use Application.run(form), but are now using form.Show(). I have corrected my previous post.

Yes, once I can get it to run (I have broken some other things), I'll have to do the breakpoint/debugger things again (since I didn't take notes last time) and if I still can't figure it out, I'll have to post the info and see if anyone can make sense of it.





Re: Windows Forms General Application.Run vs Form.Show?

Patrick8

I did the debugging, and it looks like the error comes after a certain amount of time, rather than on execution of a particular line of code. I had traced the error to a particular line of code, then I tried adding Thread.Sleep(15000) a few lines before the line of code I had traced it to. I thought maybe this would give the other thread time to finish with the loader while this thread was sleeping, but instead the error came while this thread was sleeping. The error reads as follows:

LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

If anyone can shed some light on this, it would be much appreciated. I received a comment somewhere that it might be an issue with the other application. If so, I could use anything that would help me show/prove to development that there's a problem with their application. Thanks!