'A type or generic instantiation needed to evaluate this expression does not exist in the process being debugged

I want to add two music files into my C# game. One is used for background, the other is for response while pressing the keyboard. To add the background music, I applied the System.Media.SoundPlayer Class; to add the other, I tried to use Microsoft.DirectX and Micorsoft.DirectX.DirectSound Class. Below is my code:

    private void PressTick()
    {
        SecondaryBuffer secBuffer; 
        Device secDev;//设备对象    
        secDev = new Device();
        secDev.SetCooperativeLevel(this, CooperativeLevel.Normal);
        secBuffer = new SecondaryBuffer(Properties.Resources.PressTick, secDev);
        secBuffer.Play(0, BufferPlayFlags.Default);
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Left || keyData == Keys.Right ||
            keyData == Keys.Down || keyData == Keys.Space)
        {
            object sender = Control.FromHandle(msg.HWnd);
            KeyEventArgs e = new KeyEventArgs(keyData);
            Form1_KeyDown(sender, e);
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.Space)
        {
           PressTick();
        }          
    }

However, everytime I ran my program and pressed the Space, the project always stoped. Then I found some messages when debugging.

secBuffer: A type or generic instantiation needed to evaluate this expression does not exist in the process being debugged.(the value for this item is stale due to a problem that occurred while evaluating it)

But I still don't understand waht had happened.



Solution 1:[1]

Cleaning the solution and rebuilding it fixed my issue. I got that issue when I changed the code multiple times while my code was stopped by debugger.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sampanna Pokhrel