1369

Application.Run() leads to application hanging

Question:

When I start application without specific form e.g.

Application.Run();

And then I create form, after user closes it, the process just hangs.

This happens outside of Visual Studio.

I tried to put Application.Exit() and/or Application.ExitThread() in form's Form_Closing event, but it still hangs.

Edit: Using custom ApplicationContext unfortunately doesn't work.

FYI, I'm not using any threads nor BackgroundWorkers.

Answer1:

Use Tools + Attach to Process to attach the debugger to the hung process. Debug + Break All. Debug + Windows + Threads, double-click the Main thread and look at its call stack to see what it is doing. Post the stack trace in your question if that doesn't help.

Answer2:

From the MSDN for the parameterless Application.Run():

<blockquote>

Most Windows Forms developers will not need to use this version of the method. You should use the Run(Form) overload to start an application with a main form, so that the application terminates when the main form is closed. For all other situations, use the Run(ApplicationContext) overload, which supports supplying an ApplicationContext object for better control over the lifetime of the application.

</blockquote>

So, short answer, try specifying an ApplicationContext object, or just Run() the form you are creating and showing. Maybe a more concrete example of why you're trying to open the message loop without tying it to a form would help. Are you opening several forms? Is this an introductory form like a splash screen or a login?

Answer3:

Try using the overload of Application.Run(new MyForm()) instead, though Application.Exit() should be working for you as described <a href="http://msdn.microsoft.com/en-us/library/ms157900.aspx" rel="nofollow">here</a>.

Recommend

  • Multi threading in windows phone C#?
  • How to make this ASP.NET Core Model Validation work on client-side?
  • Google Sheets content to email (lay out)
  • Spring Integration - transformer and header enricher
  • MDX Count over time without specific hierarchy
  • Plotting a 3d data read from a file using mayavi
  • Abstract classes, interfaces and Autowired
  • flex-shrink behaviour in different browsers [duplicate]
  • React-Redux componentWillReceiveProps not triggered using api data
  • Dynamic Stage name in EaselJS
  • Firefox addon(JPM) - How to pass message from panel's script to content script?
  • Attach fancybox to a link inside the fancybox
  • Rest API - NullPointerException in Broadleaf Commerce
  • Getting data from dataframe to MS-access DB Target Tables using R
  • Cocos2D set sprite position in relation to another sprite
  • Change .NET App's Assembly Resolve Location
  • Create new eloquent instance with relationships
  • How to mount dynamically single file component in Vue.js
  • Aspect not executed in Spring
  • LatLong falls within a given polygon in D3 + Leaflet
  • D3 v4 force layout disable animation on drag
  • Remove every nth element from swift array
  • Trouble Attaching Call Back to Unobtrusive Validation Show Error
  • In Akka, is ActorContext thread safe?
  • Object and struct member access and address offset calculation
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • How can I sort a a table with VBA with given text condition?
  • Record samples being played with OpenAL
  • Why is an OPTIONS request sent to the server?
  • Spray.io: When (not) to use non-blocking route handling?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Transpose CSV data with awk (pivot transformation)
  • Comma separated Values
  • Understanding cpu registers
  • How do I configure my settings file to work with unit tests?
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?