7.0 KiB
Kill Process on Disconnect Feature - Implementation Summary
Overview
This document describes the implementation of the "Kill Process on Disconnect" feature for the Pianobar Helper application.
Feature Requirements
- Kill Process Checkbox (
chkkillpro): A checkbox in the Settings form that allows users to enable/disable the kill-on-disconnect behavior - Gating Mechanism: The checkbox must be disabled (non-interactive) unless the "Enable Profile Credentials and Custom Config Generation" checkbox (
chkOverrideCredentials) is checked - Disconnect Behavior: When the user clicks the Disconnect button in the main application:
- If
chkkillprois checked: Pianobar process is immediately killed via its PID - If
chkkillprois unchecked: Pianobar process is detached (continues running in background)
- If
Implementation Details
1. Data Model (ProfileSettings)
File: ProfileManager.cs
Added KillProcessOnDisconnect boolean property to ProfileSettings:
public class ProfileSettings
{
// ... other properties ...
public bool KillProcessOnDisconnect { get; set; }
}
This property is saved/loaded with each profile in profiles.json.
2. Process Management (PianobarManager)
File: PianobarManager.cs
Implemented two disconnect methods:
Disconnect() - Detach mode
- Cleans up streams and event handlers
- Sets
IsConnected = false - Leaves the Pianobar process running in the background
DisconnectAndKill() - Kill mode
- Checks if the process exists and is not exited
- Calls
Process.Kill()to terminate immediately - Reports the PID that was killed
- Cleans up and sets
IsConnected = false
3. Settings UI (SettingsForm)
File: Settings.cs and Settings.Designer.cs
Control Initialization
chkkillprocheckbox starts disabled (set in Designer)- All credential-related fields start disabled:
txtConfigFilePathbtnBrowseConfigFiletxtPianobarUsertxtPianobarPasschkkillpro
Event Handling
chkOverrideCredentials_CheckedChanged:
- Wired in Designer (line 352)
- Calls
ToggleCredentialFields(checked)method
ToggleCredentialFields(bool enabled):
private void ToggleCredentialFields(bool enabled)
{
if (txtConfigFilePath != null)
txtConfigFilePath.Enabled = enabled;
if (btnBrowseConfigFile != null)
btnBrowseConfigFile.Enabled = enabled;
if (txtPianobarUser != null)
txtPianobarUser.Enabled = enabled;
if (txtPianobarPass != null)
txtPianobarPass.Enabled = enabled;
if (chkkillpro != null)
chkkillpro.Enabled = enabled; // <-- Critical: Kill checkbox is gated here
}
Profile Save/Load
LoadSettingsToControls()loadsKillProcessOnDisconnectintochkkillproSaveSettingsFromControls()saveschkkillpro.Checkedto profile settings- When saving, the profile is set as active via
SetActiveProfile()
4. Main Application Logic (Main.cs)
File: Main.cs
btnDisconnect_Click handler:
public void btnDisconnect_Click(object? sender, EventArgs e)
{
if (!_pianobarManager.IsConnected)
{
MessageBox.Show("Not connected to Pianobar.", "Not Connected",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (statusLabelRight != null)
statusLabelRight.Text = "Disconnecting...";
// Check if we should kill the process or just detach
var activeProfile = _profileManager.GetActiveProfile();
bool killProcess = activeProfile?.Settings.KillProcessOnDisconnect ?? false;
if (killProcess)
{
_pianobarManager.DisconnectAndKill(); // <-- Terminates process
}
else
{
_pianobarManager.Disconnect(); // <-- Detaches, leaves running
}
_outputManager?.Stop();
}
5. Output Folder Browse Button
File: Settings.cs and Settings.Designer.cs
output_folder_browse_button (ID as requested):
- Wired in Designer to
output_folder_browse_button_Click(line 237) - Opens
SaveFileDialogto allow user to select output file location - Default filename:
now_playing.txt - Selected path is saved to
output_folder_text_box(maps toTextFilePathin settings)
User Workflow
Enabling Kill-on-Disconnect
- Open File ? Settings
- Navigate to "Pianobar Config & Auth" tab
- Check "Enable Profile Credentials and Custom Config Generation" (
chkOverrideCredentials)- This enables all credential fields and the kill process checkbox
- Check "Kill process" (
chkkillpro) - Click Save
- Profile is updated and set as active
- Setting persists for future sessions
Using the Feature
- Connect: Click Connect button (Pianobar starts in background, PID shown in status)
- Disconnect with Kill: If
chkkillprowas checked:- Pianobar process is immediately terminated by PID
- Status shows "Pianobar process killed (PID: XXXX)"
- Disconnect without Kill: If
chkkillprowas unchecked:- Pianobar process continues running
- Application detaches and closes streams
- Status shows "Disconnected from Pianobar (process detached)"
Disabling Override
- If user unchecks
chkOverrideCredentials:- Kill process checkbox becomes disabled and cannot be checked
- All credential fields are disabled
- User cannot enable kill-on-disconnect without also enabling custom config generation
Testing Checklist
? Build Status: Project builds successfully without errors ? Designer Wiring: All event handlers properly wired in Designer files:
chkOverrideCredentials.CheckedChanged?chkOverrideCredentials_CheckedChangedoutput_folder_browse_button.Click?output_folder_browse_button_Clickchkkillproexists and is added topianobar_override_tab_page? Initial State: All credential fields andchkkillprostart disabled ? Gating Logic:ToggleCredentialFields()enables/disableschkkillprobased on override checkbox ? Profile Persistence:KillProcessOnDisconnectsaved/loaded with profile ? Disconnect Logic: Main form branches to kill or detach based on profile setting ? Process Management:DisconnectAndKill()terminates by PID;Disconnect()detaches only
Files Modified
- ProfileManager.cs - Added
KillProcessOnDisconnectto data model - PianobarManager.cs - Added
DisconnectAndKill()method - Settings.cs - Load/save logic and gating via
ToggleCredentialFields() - Settings.Designer.cs - Set initial
Enabled = falsefor credential fields andchkkillpro - Main.cs - Disconnect logic branches based on profile flag
Summary
The kill-on-disconnect feature is fully implemented and integrated with the existing profile system. The checkbox is properly gated by the override credentials checkbox, ensuring users cannot accidentally enable the feature without understanding its context within custom config generation. The feature provides clean process termination when desired while preserving the option to keep Pianobar running in the background.