使用GC的处理程序引发事件时的异常

时间:2016-03-31 20:34:31

标签: c# .net winforms garbage-collection

当我多次按下我的keyhook时,我一直得到'对垃圾收集的委托进行回调',我在program.cs中得到它,而不是在我的form.cs中,keyhook是。 这是program.cs:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

以下是表格:

public partial class Form1 : Form
{
    AudioFileReader fileReader = new AudioFileReader("medCalloutReady.mp3");
    IWavePlayer waveOutDevice = new WaveOut();
    globalKeyboardHook gkh = new globalKeyboardHook();
    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        waveOutDevice.Volume = (float)0.7;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        gkh.HookedKeys.Add(Keys.Z);
        gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
    }

    void gkh_KeyDown(object sender, KeyEventArgs e)
    {
        BarTimer.Value = 0;
        timer1.Start();
        timer1.Interval = 400;
        BarTimer.Minimum = 0;
        BarTimer.Maximum = 100;
        e.Handled = true;
    }
    //button that starts the timer and resets the progress bar
    private void Butt_startTimer_Click(object sender, EventArgs e)
    {
        BarTimer.Value = 0;
        timer1.Start();
        timer1.Interval = 400;
        BarTimer.Minimum = 0;
        BarTimer.Maximum = 100;
    }

    //timer and progress bar progression
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (BarTimer.Value < BarTimer.Maximum)
        {
            BarTimer.Value = BarTimer.Value + 1;
            percentlabel.Text = BarTimer.Value.ToString() + "%";
        }
        if(BarTimer.Value==75)
        {
            AudioPlaybackEngine.Instance.PlaySound("medCalloutReady.mp3");
        }
        if(BarTimer.Value==BarTimer.Maximum)
        {
            timer1.Stop();
            AudioPlaybackEngine.Instance.PlaySound("medCalloutReady.mp3");
        }

    }

    private void volumeNumSlider_ValueChanged(object sender, EventArgs e)
    {
        changevol(volumeNumSlider.Value.ToString());
    }
    private void changevol(string volnum)
    {
        float newnum = float.Parse(volnum)/10;
        waveOutDevice.Volume = newnum;
    }
}

顺便说一句,我使用GlobalKeyboardHook作为钥匙钩。

0 个答案:

没有答案