将输出类型设置为Windows应用程序时,阻止关闭控制台应用程序

时间:2014-01-19 16:26:24

标签: c# console-application

我有一个简单的服务器应用程序作为控制台应用程序运行,效果很好!但现在我想隐藏控制台窗口并让应用程序在后台运行。

如果我将应用程序输出类型设置为Windows应用程序,程序似乎运行然后立即关闭。这非常令人沮丧......我如何停止控制台应用程序关闭但仍隐藏它?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace ConsoleApplication1
{


public class MyRequest
{
    public string Text { get; set; }
}

public class MyResponse
{
    public string Text { get; set; }
}

class Program
{
    private static IDuplexTypedMessageReceiver<MyResponse, MyRequest> myReceiver;


    [STAThread]
    static void Main(string[] args)
    {
        IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
        myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
        myReceiver.MessageReceived += OnMessageReceived;

        IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
        IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("IP ADDRESS:8060/");

        myReceiver.AttachDuplexInputChannel(anInputChannel);

        Console.WriteLine("The service is running. To stop press enter.");
        Console.ReadLine();

        myReceiver.DetachDuplexInputChannel();
    }

    private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<MyRequest> e)
    {
        Console.WriteLine("Received: " + e.RequestMessage.Text);

        if (e.RequestMessage.Text == "Connect")
        {
            String computerName = System.Environment.MachineName.ToString();
            MyResponse aResponse = new MyResponse();
            aResponse.Text = "Connected to:\n" + computerName;
            myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
            ExecuteCommandSync("Do My CMD");
            //Display connection notification
            new System.Threading.Thread(() => { System.Windows.Forms.Application.Run(new Form1()); }).Start();
        }
        else
        {
            MyResponse aResponse = new MyResponse();
            aResponse.Text = "Server Not Connected";
            myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
            ExecuteCommandSync("Stop My Command");
        }        
    }

    public static void ExecuteCommandSync(object command)
    {
        try
        {
            var procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;
            procStartInfo.CreateNoWindow = false;
            var proc = new System.Diagnostics.Process();
            proc.OutputDataReceived += (s, e) => { Console.WriteLine(e.Data); };
            proc.StartInfo = procStartInfo;
            proc.Start();
            proc.BeginOutputReadLine();
            proc.WaitForExit();
        }
        catch (Exception objException)
        {
            Console.WriteLine("Error: " + objException.Message);
        }
    }

}

解决了:

class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

static void Main(string[] args)
    {
     IntPtr h = Process.GetCurrentProcess().MainWindowHandle;
     ShowWindow(h, 0);

        while (true)
        {
            System.Threading.Thread.Sleep(1);
            IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory();
            myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
            myReceiver.MessageReceived += OnMessageReceived;

            IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
            IDuplexInputChannel anInputChannel = aMessaging.CreateDuplexInputChannel("tcp://192.168.1.74:8060/");

            myReceiver.AttachDuplexInputChannel(anInputChannel);

            Console.WriteLine("The service is running. To stop press enter.");
            Console.ReadLine();

            myReceiver.DetachDuplexInputChannel();
         }
     }
}

1 个答案:

答案 0 :(得分:0)

只是将您的应用转换为Windows应用无法实现目标。

我建议您只是将应用程序最小化到任务栏,它将继续在后台运行,或者将应用程序转换为Windows服务。