0

I develop Forms Application using C# to collect data from a spectrometer device. When I set continuous acquisition, I am not able to perform other operations with UI during the acquisition happens. I am thinking to use multithreading. I am from Science background and not much familiar with C#. Kindly help me with some code also.

Please see part of code which has a button click to start acquisition and another button to save the acquired data. I want to save data, in between acquisition happens.

private void button2_Click(object sender, EventArgs e)
    {
        while (true)
        {
            this.Refresh();
            int numberOfPixels; // number of CCD elements
            double[] spectrum;
            spectrum = null;   // spectrometerIndex = 0;

            if (spectrometerIndex == -1)
                return; // no available spectrometer

            numberOfPixels = wrapper.getNumberOfPixels(spectrometerIndex);
            wrapper.setBoxcarWidth(spectrometerIndex, 0);
            wrapper.setCorrectForElectricalDark(spectrometerIndex, 1);
            wrapper.setIntegrationTime(spectrometerIndex, 1000); // acquisition time in microsecs

            int acquiretime = 100;
            if (textBoxin.Text != "")

            {
                int.TryParse(textBoxin.Text, out acquiretime); //arbitrary acquiretime

            }

            Stopwatch integrate = new Stopwatch();
            integrate.Start();
            while (integrate.Elapsed < TimeSpan.FromMilliseconds(acquiretime))
            {
                this.Refresh();
                spectrum = (double[])wrapper.getSpectrum(spectrometerIndex); data from spectrometer

            }

            integrate.Stop();
        }
    }

private void button7_Click(object sender, EventArgs e)
        {
            File.WriteAllText((@"D:\ExecRonefile\abcd.csv"), csv.ToString());
            MessageBox.Show("Data saved into csv format succesfully !!");
            
        }
  • This code is incomplete and does not compile. I could not suggest an improvement without you posting real code - and preferably a [mcve]. Can you please read [ask] and then improve your question? – Enigmativity Feb 16 '21 at 09:53
  • Multithreading is the correct approach to go for. But that is a to large and complex topic to provide a good overview here. I would suggest reading about [async/await](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/), [tasks](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.run?view=net-5.0) and [best practices](https://learn.microsoft.com/en-us/dotnet/standard/threading/managed-threading-best-practices). It would also be useful to read about some of the common hazards with multithreading. – JonasH Feb 16 '21 at 13:58

1 Answers1

0

If you are not familiar with multithreading, you can start by using a BackgroundWorker as it provides events to update the UI easily.

So your button will start the worker and periodically the worker will call ReportProgress to update the UI

my favorite ressource related to threading http://www.albahari.com/threading/

Alex R
  • 347
  • 2
  • 10