0

我做了一个非常简单的程序,但是当我使用右上角的红色 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;
        }


    }
}

}

4

2 回答 2

4

请务必关闭该进程的所有线程...有时,尽管窗体已关闭,但线程仍保持进程继续执行...您可以在OnClosingwindows 窗体的方法中执行此操作......

于 2012-07-10T17:57:12.267 回答
3

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

于 2012-07-10T17:58:00.597 回答