Save to output file corrupt

I am developing a UWP app. Within the app I try to save a effect to a wave file, but the file is not fully created until I close the debugger (Visual Studio 2017), and ends up being a 29 MB file when it was created from 79.5 KB file. Here is the code I am using to create the file.

        result = Factory.System_Create(out system);
        if (result == RESULT.OK)
        {
            StorageFolder folder;
            StorageFile file;
            Channel channel;
            Sound sound;
            EffectInfoModel data;
            CREATESOUNDEXINFO info;
            Image img;
            string filename;

            img = sender as Image;
            data = (EffectInfoModel)img.Tag;

            filename = string.Format("{0}_{1}.wav", data.Name, DateTime.Now.ToString("yyyyMMddhhmmss"));
            folder = await ((App)Application.Current).GetRecordingsFolderAsync();
            file = await folder.CreateFileAsync(filename);
            result = system.setOutput(OUTPUTTYPE.WAVWRITER);
            result = system.init(2, INITFLAGS.NORMAL, Marshal.StringToHGlobalAnsi(file.Path));

            _effects = new EffectsMasterModel();
            _effects.EffectName = data.Name;
            _effects.CanProcessAudio = true;
            Factory.System_Create(out system);
            system.init(32, INITFLAGS.NORMAL, IntPtr.Zero);
            info = new CREATESOUNDEXINFO();
            system.createSound(_audioFile.Path, MODE.DEFAULT, ref info, out sound);
            system.playSound(sound, null, true, out channel);

            _effects.FmodSystem = system;
            _effects.FmodChannel = channel;
            _effects.FmodSound = sound;
            if (SetSoundParameters(_effects))
            {
                ShowSaving = true;
                system.update();
                channel.setPaused(false);
                _watchdog.Start();
            }

        }
    }

    private bool SetSoundParameters(EffectsMasterModel effects)
    {
        FMOD.System system;
        ChannelGroup masterGroup;
        Channel channel;
        Sound sound;

        system = effects.FmodSystem;
        system.getMasterChannelGroup(out masterGroup);
        channel = effects.FmodChannel;
        sound = effects.FmodSound;
        effects.CanProcessAudio = true;
        switch (effects.EffectName)
        {
            case "cave":
                {
                    REVERB_PROPERTIES props;
                    DSP dspEcho;

                    system.createDSPByType(DSP_TYPE.ECHO, out dspEcho);
                    masterGroup.addDSP(0, dspEcho);
                    props = PRESET.CAVE();
                    system.setReverbProperties(0, ref props);
                    dspEcho.setBypass(false);
                    dspEcho.setParameterFloat((int)DSP_ECHO.DELAY, 900);
                }
                break;

           // More case statements that create different effects
           ....
        }
        return (effects.CanProcessAudio);
    }

    private void HandleSoundStop()
    {
        if (ShowSaving)
            ShowSaving = false;

        else
            _playImageControl.Source = _playImage;

        _pitchShift = 0;
        _watchdog.Stop();
        _effects.FmodChannel.stop();
        _effects.Release();
    }

    public void Release()
    {
        RelaseDSP();
        if (FmodSound != null)
        {
            FmodSound.release();
            FmodSound = null;
        }

        if (FmodSystem != null)
        {
            FmodSystem.close();
            FmodSystem.release();
            FmodSystem = null;
        }
    }

    public void RelaseDSP()
    {
        if (FmodSystem != null)
        {
            ChannelGroup group;

            FmodSystem.getMasterChannelGroup(out group);
            if (group != null)
            {
                DSP dsp;
                int count;

                count = 0;
                group.getNumDSPs(out count);
                for (; count > 0; --count)
                {
                    group.getDSP(count - 1, out dsp);
                    group.removeDSP(dsp);
                    dsp.release();
                }
            }
        }
    }

The HandleSoundStop() method is called from a 250ms watchdog timer that checks the isPlaying property of the Channel created from the playSound call.

Release() and ReleaseDSP() are methods in _effects.

Also, while the effect is being saved I can also hear it through the speakers. Is there any way to not route to speakers during file save?

That’s what happens when you copy and paste things. Thanks for the quick response.