问题
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 :
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.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 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.
回答1:
try this code:
Environment.Exit(0);
回答2:
Are you putting you .exe file in Arguments
property of ProcessStartInfo
?
If you must do that, I believe that you're using CMD in FileName
property, so you must use /K
before your .exe name.
Or just put in FileName
the .exe path.
You can clarify a lot if you put the code that calls your application.
回答3:
Try calling Application.Exit
This works in windowed applications, and may force the console window to close.
回答4:
Try this:
Tools > Options > Debugging > Automatically Close the Console When Debugging Stops
回答5:
I had exactly the same problem. Running console app on Beckhoff PLC which never closed.
Instead of creating Console app I created Windows App. My code stayed the same as for console app. I just commented out:
// Application.Run(new Form1());
Seems now codes run without opening a form.
来源:https://stackoverflow.com/questions/31671893/console-application-not-closing