关闭后Windows窗体不会关闭?

时间:2012-07-10 17:49:44

标签: c# winforms

我制作了一个非常简单的程序,但是当我使用右上角的红色X关闭它时,我仍然可以在进程下列出的Windows任务管理器中看到它。该程序仍在运行并消耗内存。我怎么能避免这个?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace PS3Controller
{
public partial class Form1 : Form
{
    GamePadState pad;

    Vector2 centroIzq = new Vector2(138, 132);
    Vector2 centroDer = new Vector2(139, 252);

    float distancia = 20f;

    float ledIzqAngulo = 0;
    float ledDerAngulo = 0;

    float ledIzqVelocidad = 0.0022f;
    float ledDerVelocidad = 0.0022f;


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        pad = GamePad.GetState(Microsoft.Xna.Framework.PlayerIndex.One);

        if (pad.Buttons.Y == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledTtriangulo.Visible = true;
        else
            ledTtriangulo.Visible = false;

        if (pad.Buttons.X == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledCuadrado.Visible = true;
        else
            ledCuadrado.Visible = false;

        if (pad.Buttons.A == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledX.Visible = true;
        else
            ledX.Visible = false;

        if (pad.Buttons.B == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledCirculo.Visible = true;
        else
            ledCirculo.Visible = false;

        if (pad.DPad.Left == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledIzq.Visible = true;
        else
            ledIzq.Visible = false;

        if (pad.DPad.Right == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledDer.Visible = true;
        else
            ledDer.Visible = false;

        if (pad.DPad.Up == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledArriba.Visible = true;
        else
            ledArriba.Visible = false;

        if (pad.DPad.Down == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledAbajo.Visible = true;
        else
            ledAbajo.Visible = false;

        if (pad.Buttons.Start == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledStart.Visible = true;
        else
            ledStart.Visible = false;

        if (pad.Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledSelect.Visible = true;
        else
            ledSelect.Visible = false;

        if (pad.Buttons.LeftShoulder == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledL1.Visible = true;
        else
            ledL1.Visible = false;

        if (pad.Buttons.RightShoulder == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledR1.Visible = true;
        else
            ledR1.Visible = false;

        if (pad.Buttons.BigButton == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
            ledHome.Visible = true;
        else
            ledHome.Visible = false;

        if (pad.Triggers.Left > 0)
            ledL2.Visible = true;
        else
            ledL2.Visible = false;

        if (pad.Triggers.Right > 0)
            ledR2.Visible = true;
        else
            ledR2.Visible = false;

        lblIzqX.Text = pad.ThumbSticks.Left.X.ToString();
        lblIzqY.Text = pad.ThumbSticks.Left.Y.ToString();
        lblDerX.Text = pad.ThumbSticks.Right.X.ToString();
        lblDerY.Text = pad.ThumbSticks.Right.Y.ToString();


        Vector2 ledIzqPos = new Vector2(
            (float)Math.Cos(ledIzqAngulo) * distancia,
            (float)Math.Sin(ledIzqAngulo) * distancia);

        Vector2 ledDerPos = new Vector2(
            (float)Math.Cos(ledDerAngulo) * distancia,
            (float)Math.Sin(ledDerAngulo) * distancia);

        if (pad.ThumbSticks.Right.X == 0 &&
            pad.ThumbSticks.Right.Y == 0)
        {
            ledPadDer.Top = 139;
            ledPadDer.Left = 252;
        }

        if (pad.ThumbSticks.Left.X == 0 &&
            pad.ThumbSticks.Left.Y == 0)
        {
            ledPadIzq.Top = 138;
            ledPadIzq.Left = 132;
        }


    }
}

}

2 个答案:

答案 0 :(得分:4)

请务必关闭该进程的所有线程...有时,线程会保持进程以便在表单关闭时继续执行...您可以在Windows窗体的OnClosing方法中执行此操作... ...

答案 1 :(得分:3)

您确定自己的应用仍在运行,还是YourApp.vshost.exe? vshost适用于Visual Studio,并且在项目打开时将始终运行。