none
New forms are causing my GUI to freeze up RRS feed

  • Question

  • So my GUI is freezing up because of this function:

     
              void callingHistogram_Form(  double[] TV, double[] SinusoidalCurveFitting)
               {
                   Histogram_Form f = new Histogram_Form();
                    double[] DL  = new double[1];
                DL[0] = BaseLine_Dark;
                   f.DarkLight_values = DL;
                   f.BrightLight_values = BaseLineBright.ToArray(); 
                   f.Raw_value  = Raw.ToArray();
                   f.Transmission_values =TV ;
                   f.SinusoidalCurveFitting = SinusoidalCurveFitting;
                   f.step_Pass = stepPass;
                   f.start_Step = startStep;
                   f.stop_Step = stopStep;
                   f.ShowDialog();
                   f.Hide();
                f.Close();
                f = null;
               }
    
    

    so what is happening is, I click a button and the software run this routine in a new thread and at the end of it, is calls that function to display the information in a new Form that is just a graph.  Now depending on what kind of routine you are doing, you can have multiple graphs  and the function is in a for loop :

                    for (int i = 0; i < stIRList.Count; i++)
                    {
    
          
                          
                        try
                        {
                            if (SinusoidalCurveCB.Checked)
                            {
                               callingHistogram_Form(Transmission[i].ToArray(), await SinusoidalCurveFitting(TP, TS));
                            }
                            else
                              callingHistogram_Form(Transmission[i].ToArray());
                            
                        }
                        catch (Exception err)
                        {
                            AppendDisplayBox(err.Message);
    
                        } 
    
    
                    }
    
     

    if I removed this function then the program run fine.

    but with that function it seem to work fine for the 1st run but if I display too many graphs the second run, the software will freeze up.

    Also, I might can do a few runs before it freezes up, it just  depending on how many graphs I display.

    so what I am doing wrong ?

            private void Histogram_Form_Load(object sender, EventArgs e)
            {
                Console.WriteLine("Load Forum");
                try
                {
    
                    PlotSpectrum();
    
                   
                }
                catch(Exception err)
                {
                    Console.WriteLine(err.Message);
                    MessageBox.Show(err.ToString());
                }
                Console.WriteLine("Load Forum");
            }
    
    
      private void closeToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    

     
    Tuesday, March 17, 2020 9:41 PM

All replies

  • When the app freezes if you let it go will it ever respond ?

    Have you tried running Visual Studio's performance profiler or setup IntelliTrace ?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, March 17, 2020 11:17 PM
    Moderator
  • Greetings Btb4198.

    For a start, the lines f.Hide, f.Close(), and f = null are all unnecessary, but that is probably not the cause of your problem.

    At a wild guess, you could try calling GC.Collect() to force a garbage collection of unused memory. It's very rare for a call to GC.Collect() to help, but this might be one of those rare times.

                    for (int i = 0; i < stIRList.Count; i++)
                    {
    
          
                          
                        try
                        {
                            if (SinusoidalCurveCB.Checked)
                            {
                               callingHistogram_Form(Transmission[i].ToArray(), await SinusoidalCurveFitting(TP, TS));
                            }
                            else
                              callingHistogram_Form(Transmission[i].ToArray());
    
                               // Try adding this line.
                               GC.Collect();
                            
                        }
                        catch (Exception err)
                        {
                            AppendDisplayBox(err.Message);
    
                        } 
    
    
                    }

    • Marked as answer by Btb4198 Wednesday, March 18, 2020 3:43 PM
    • Unmarked as answer by Btb4198 Wednesday, March 18, 2020 3:58 PM
    Wednesday, March 18, 2020 12:42 AM
  • what do you mean let it go ?

    I click the button and that is all, then is just runs

    never heard of  Visual Studio's performance profiler, what is it ?

    Wednesday, March 18, 2020 12:42 AM
  • Or maybe deadlock within synchronization state of the component when it P/Invoke to unmanaged libraries. (Normally you don't need support from unmanaged libraries to draw histogram on C#, but the property name "SinusoidalCurveFitting" makes me think this is a probable guess)

    Without knowing the behavior characteristics of it, it will be difficult to guess the actual cause.

    Wednesday, March 18, 2020 1:22 AM
    Answerer
  • Hi Btb4198,

    Thank you for posting here.

    Probably the most important thing now is to find out where the problem is, and to know why it happened.

    Because there are some custom methods in your code, we can't fully reproduce what you are now, so please check the following link to try to find the problem.

    7 Debugging Techniques for when your .NET application Freezes (hangs)

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Looking forward to your reply.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 18, 2020 2:13 AM
  • what do you mean let it go ?

    I click the button and that is all, then is just runs

    never heard of  Visual Studio's performance profiler, what is it ?

    When an application freezes it means the code is working without any time to update the UI. If the code was running in the same thread you could use task in tangent with await and async and rigged with a cancellation token or use a BackGroundWorker.

    Profiler https://docs.microsoft.com/en-us/visualstudio/profiling/?view=vs-2019


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 18, 2020 2:47 AM
    Moderator
  • Thanks!!! your garbage collection idea seem to be working!!!

    the software has not frozen and I have been running a lot!!!

      

    Wednesday, March 18, 2020 3:40 PM
  • Never mind, I spoke too soon...

    but It did least a lot longer than it every did before. 

    Wednesday, March 18, 2020 4:00 PM
  • I see some problems with your approach altogether but I'll limit it to your code freezing up.

    callingHistogram_Form(Transmission[i].ToArray(), await SinusoidalCurveFitting(TP, TS));

    This looks like the issue to me but setting a breakpoint would clarify. Firstly to be able await inside this code means this is an async method. If it is an async method I wonder if it is being called on a non-UI thread. If it is you could simply have a classic UI threading issue. You said you run code in a new thread that triggers this function call. Is that thread a background thread? Did you set the thread as an STA so Winforms will behave properly?

    But I suspect the most likely cause is that awaited function. Your code is going to block whatever thread it is running on until that method completes. Once it completes then you will be able to call the _Form method that shows your form. Setting a breakpoint inside this method and stepping through the code will reveal the issue really fast.

    To work around this I recommend that you don't await inside this call at all. Instead call your _Form method and pass it the function you want to call (as a delegate if needed). Then inside the OnLoad method for this child form disable the form UI (or whatever) while the data is loaded. Start the async method call here but don't wait for it. Add a continuation on the task to update your UI once the function completes. Note that you might be able to async/await the OnLoad method itself, I've never tried. Irrelevant the form will pop up quickly and then the user will get a "waiting"-like UI from your form while it loads the data behind the scenes.

    Note also that your _Form method is doing a lot of work after the form.ShowDialog method that it doesn't need to do. The form is already closed/hidden when the method returns. All the code in this method after ShowDialog can go away.



    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, March 18, 2020 6:02 PM
    Moderator
  • [...] I click a button and the software run this routine in a new thread [...]

    Are you aware that this is not allowed in a desktop application?

    The user interface can only be accessed from the tread that created it. If you execute something in a different thread, it cannot access the user interface. If it does, you experience random and unpredictable errors, such as freezing or corruption of the display.

    If you need to do something in the UI (such as showing a form) from a different thread, you first have to marshall execution into the main thread. This is done by means of the Invoke or BeginInvoke methods of any control, including the form itself. Of course, this will block if the main thread is busy at the time you try to do the Invoke, so you need to make sure that the main thread is not kept busy when you are doing these operations.

    Wednesday, March 18, 2020 6:37 PM
    Moderator
  • How do I make sure that the main thread is not kept busy when you are doing these operations?
    Thursday, March 19, 2020 3:44 PM
  • How do I make sure that the main thread is not kept busy when you are doing these operations?

    Simply, don't execute anything in that thread which is not very quick. Basically, only use the main thread to launch the secondary threads and to display their results. Don't execute in the main thread anything that takes a noticeable time to execute.

    For instance, one common mistake is to launch in the main thread a query to a database that takes very long to return, and launch a secondary thread to display a progress bar. Since the secondary thread has to call the main thread to display the bar, but the main thread cannot display it because it is busy doing the query, then the progress bar is frozen despite being launched in a different thread. The remedy for such a situation would be to launch the long-running query in a secondary thread.

    Thursday, March 19, 2020 4:53 PM
    Moderator