private void buttonConstellation_Click(object sender, EventArgs e) { if (buttonConstellation.Text == "Constellation") { lock (constellationLock) { if (constellation == null) { constellation = new Constellation(); // First time only, check that matlab folder exists if (!File.Exists(Constellation.constellationExecutable )) { MessageBox.Show("The Matlab program is not present, at: " + Constellation.constellationExecutable + "\n\nUse Properties / Constellation page to select a different Matlab executable or location.", "DAN PTP - Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); constellation = null; return; } } buttonConstellation.Text = "Close Plot"; } // unlocked // Get constellation data, write files and launch Matlab on a worker thread. // It's possible (try double-clicking "Constellation" button) that the worker // has already been started, so only invoke RunWorker when not busy: if (!bgWrkConstellation.IsBusy) { bgWrkConstellation.RunWorkerAsync(); } } else { stopConstellation(); } }
/* * Must be reentrant, can be called by the gui or the background worker * at the RunWorkerCompleted method. */ private void stopConstellation() { // Sometimes, will block processing of the button from 'off' to 'on', // but if we don't block here, constellation.shutDown can happen at // the same time that the bgWrk thread is iterating PhyConstellationData // objects, registering with pcap, etc. lock (constellationLock) { if (constellation != null) { constellation.shutDown(); } buttonConstellation.Text = "Constellation"; constellation = null; // Don't need to cancel the background task; just let // it finish. It will detect null constellation // during RunWorkerCompleted. } }