FormClosing,OnFormClosed,ETC ......不在(X)C#上触发

时间:2017-10-12 17:25:40

标签: c# events

好吧,所以我有这个程序,我正在努力写。没有公司会依赖它或任何东西,所以我不太关心这里的线程几乎是非常糟糕的做法。

我的问题是,我在本网站和MSDN上找到的“表格结束事件”都没有为我开火。

在我的代码中,我有非常有希望的控制台输出,如果代码已经运行会通知我,但我发现的事件都没有。

有人能够让我接受实现这一目标的现实吗?我知道我必须在这里错过一些简单的东西。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace CMDRunner
{
    public partial class Form1 : Form
    {
        IDataObject clippy = Clipboard.GetDataObject();
        String TClippy;
        bool isRunning = true;
        //Thread worker;
        public Form1()
        {
            InitializeComponent();

            try
            {
                TClippy = Clipboard.GetText();
            }
            catch (Exception e)
            {
                richTextBox1.Text += e.ToString() + "\n";
            }
            System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        timer.Tick += OnTimerTick;
        timer.Interval = 500;
        timer.Start();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //worker = new Thread(KeyChecker);
        //worker.SetApartmentState(ApartmentState.STA);
        CheckForIllegalCrossThreadCalls = false;
        //worker.Start();

        System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
        ToolTip1.SetToolTip(this.checkBox1, "Hides anything you run.");
        richTextBox2.Text += Clipboard.GetText();
    }



    void KeyChecker()
    {
        while (isRunning)
        {
            Thread.Sleep(40);
            if ((Keyboard.GetKeyStates(Key.Return) & KeyStates.Down) > 0)
            {
                richTextBox1.Text += "Enter Key Clicked!!!\n";
                Console.WriteLine(isRunning);
            }
        }
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
        // I will fire the events here

        if (Clipboard.ContainsText() == true && TClippy != Clipboard.GetText())
        {
            try
            {
                TClippy = Clipboard.GetText();
                Clipboard.GetImage();
                richTextBox2.Text = TClippy;
                richTextBox3.Text = TClippy + "\n***************************************\n^^^End Clipboard item above^^^\n***************************************\n" + richTextBox3.Text;
            }
            catch (Exception er)
            {
                richTextBox1.Text += er.ToString() + "\n";
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        sendCommand();
    }

    public void sendCommand()
    {
        try
        {
            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

            if (checkBox1.Checked)
            {
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            }
            else
            {
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            }
            startInfo.FileName = textBox1.Text;
            startInfo.Arguments = textBox2.Text;
            process.StartInfo = startInfo;
            process.Start();

            richTextBox1.Text += textBox1.Text + " \"" + textBox2.Text + "\"\n";
        }
        catch (System.ComponentModel.Win32Exception e)
        {
            Console.WriteLine(e.ToString());
            richTextBox1.Text += "ERROR: Could not find: " + textBox1.Text + "\n";
        }
        catch (System.InvalidOperationException)
        {
            richTextBox1.Text += "No input provided\n" ;
        }
        catch (Exception e)
        {
            richTextBox1.Text += e.ToString() + "\n";
        }
    }

    //UserActivityHook actHook;


    /*private void Checker(object arg)
    {
        // Worker thread loop
        for (;;)
        {
            if (StopRequest.WaitOne(300)) return;
            richTextBox1.Text += Clipboard.GetText() + "\n";
        }
    }*/


    //******************************************************************************************************************************
    //Nothing below ever runs.....
    //******************************************************************************************************************************
    private void Form1_Closing(object sender, FormClosingEventArgs e)
    {
        isRunning = false;
        Console.WriteLine(isRunning);
        Console.WriteLine("CLOSING");
        //worker.Join();
        //e.Cancel = true;
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        isRunning = false;
        Console.WriteLine(isRunning);
        Console.WriteLine("CLOSING");
        //worker.Join();
        //e.Cancel = true;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (string.Equals((sender as Button).Name, @"CloseButton"))
        {
            // Do something proper to CloseButton.
            isRunning = false;
            Console.WriteLine(isRunning);
            Console.WriteLine("CLOSING");
            //worker.Join();
        }
        else
        {
            isRunning = false;
            Console.WriteLine(isRunning);
            Console.WriteLine("CLOSING");
            // Then assume that X has been clicked and act accordingly.
        }
    }

    private void Form1_OnFormClosed(object sender, System.ComponentModel.CancelEventArgs e)
    {
        isRunning = false;
        Console.WriteLine(isRunning);
        Console.WriteLine("CLOSING");
        //worker.Join();
        //e.Cancel = true;
    }
}
}

0 个答案:

没有答案
相关问题