0

I keep getting 'A callback was made on a garbage collected delegate' when pressing my keyhook too many times, I get it in my program.cs and not in my form.cs, which the keyhook is. Here is the 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());
    }
}

And here is the form:

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;
    }
}

I use GlobalKeyboardHook for the keyhook by the way.

Jason Watkins
  • 3,766
  • 1
  • 25
  • 39
dvs
  • 97
  • 1
  • 8

0 Answers0