Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console application not closing

I'm developing a console application that is supposed to run under WinCE 6.0 and WinCE 7.0. I'm using C#, Compact Framework 2.0 for different compatibility reasons.

My application is started by an external runtime called TwinCAT (from Beckhoff). Within this application, my teammate used a function block called nt_startProcess (documentation here) that is in charge of starting my application on demand.

My problem - Two different behaviors depending on the OS :

  1. When started manually (without TwinCAT) from a cmd line :

    My application behaves properly on both systems. It means that, the applications starts, displays "Hello World" and then returns to the cmd line.

  2. When started from TwinCAT :

    a) On WinCE 6.0, I can see a cmd line opening, displaying "Hello World" and shutting itself right after. Perfect behavior to me.

    b) On WinCE 7.0, I can see a cmd line opening, displaying "Hello World" but it remains open forever. This is my problem!

Code snippet :

using System;
using System.Collections.Generic;
using System.Text;

namespace MyBasicExample
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World");
    }
  }
}

Compilation information

In Visual Studio 2008, within the Project compilation's properties :

  • Plateform target : Any CPU

Additionnal note :

Please note that the computer who is running WinCE 6.0 is using a i486 processor while the one running WinCE 7.0 is using a Freescale ArmCortex process.

WinCE 6.0 :
WinCE 6.0

WinCE 7.0 :
WinCE 7.0

What I tried :

1) Using return 0; at the end of application.

Doesn't change anything on WinCE 7.0.

2) Using Environment.Exit(0);

Is not available in Compact Framework 2.0.

3) Using the property : IsBackground

Snippet :

// ... Same snippet as above except for the next line...
Thread.CurrentThread.IsBackground = true;
Console.WriteLine("Hello World");
// ...

4) From TwinCAT, calling a batch file (which calls my exe) instead of my exe.

Doesn't work with TwinCAT. I get an error of type "General Sub-Windows error".

5) Tested with the Compact Framework 3.5.

Same behavior.

6) Tested with another CX computer (model 2020) using Windows CE 7.0 and another processor architecture (Intel Pentium III Xeon Model A).

Same behavior.

like image 804
Andy M Avatar asked Jul 28 '15 09:07

Andy M


People also ask

How do I exit the console application?

Console applications will exit when the main function has finished running. A "return" will achieve this.

Why does my console application closes immediately?

Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes. This is expected behavior.

How do I clear the console in C#?

Use the Console. Clear() method to clear screen and the console buffer. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window.


2 Answers

try this code:

Environment.Exit(0);
like image 174
Fatikhan Gasimov Avatar answered Sep 22 '22 09:09

Fatikhan Gasimov


Try this:

Tools > Options > Debugging > Automatically Close the Console When Debugging Stops

like image 43
Kirill Avatar answered Sep 22 '22 09:09

Kirill