none
C# program freezing up RRS feed

  • Question

  • So I think my interface freezes up  at this point. I was printing out command line to the Output and and looking at my user interface, I see it freezes up here: 

    Updating percentpass : 43
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    Updating percentpass : 45
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    add TrasmissionV
    Updating percentpass : 46

    45% was the last value 

    but I am not see anything that should cause the user interface to freeze up. There is not exception are anything.

    the code keeps running in the background, and it event pop up other forms, but the main form is freeze .

     also after I hit the break all button two times, the user interface starts working again.

    how/why?

     

    I saw a few of these:

    The thread 0x3580 has exited with code 0 (0x0).
    The thread 0x329c has exited with code 0 (0x0).
    The thread 0x3adc has exited with code 0 (0x0).
    The thread 0x2588 has exited with code 0 (0x0).
    The thread 0x2cb8 has exited with code 0 (0x0).

    IDK how to troubleshoot this..

    does anyone have any ideas?

    Wednesday, October 23, 2019 4:59 PM

All replies

  • Hi,

    Can you show us your code, please?

    Greetings,

    Chris

    Wednesday, October 23, 2019 5:08 PM
  • private void DimMeaBTN_Click(object sender, EventArgs e)
            {
                if (isrunning == true)
                    return;
    
                if (!PLC.Safetyswitch)
                {
                    MessageBox.Show("Panel is Open. TOO dangerous to run", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    return;
                }
    
                MessageBox.Show("Preparing to Take Performance Measurement ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                title = "Performance Measurement";
    
                TakeMeasurement();
    
            }
    
            void TakeMeasurement()
            {
                isrunning = true;
                PerformanceM.Clear();
                removedoubleclick();
    
                camProcess = new Thread(multiRunning);
                camProcess.IsBackground = true;
                camProcess.Start();
    
    
    
            }
    
    
            void multiRunning()
            {
               
                StepInfo step = get_StepInfo();
                runningTemp(step);
                PerformanceM.Clear();
                BaseLine_Bright.Clear();
            }
    
            void runningTemp(StepInfo step)
            {
                string CP = "OS";
                float TS;
                float TP;
                float CR;
    
                bool pass = running(0, step, 0);
                isrunning = false;
                if (pass == true) {
    
                    for (int i = 0; i < stIRList.Count; i++)
                    {
    
                        TS = (float)get_TS(Transmission[i].ToArray());
                        TP = (float)get_TP(Transmission[i].ToArray());
                        if (TS == 0)
                            TS = 1F;
                        CR = TP / TS;
    
                        if (takeMeaFlag == true)
                        {
    
                            if (PixeLink_RB.Checked == true)
                                add_To_table(CP, Xpostxt.Text, Ypostxt.Text, TS, TP, CR, 0F, 0F, 0F, XumList[i], YumList[i], 1);
                            else
                                add_To_table(CP, Xpostxt.Text, Ypostxt.Text, 0F, 0F, 0F, TS, TP, CR, XumList[i], YumList[i], 1);
    
                            takeMeaFlag = false;
                            UpdateGrid(1);
    
    
                        }
                        else
                        {
                            if (PixeLink_RB.Checked == true)
                                add_To_table(CP, Xpostxt.Text, Ypostxt.Text, TS, TP, CR, 0F, 0F, 0F, XumList[i], YumList[i], 2);
                            else
                                add_To_table(CP, Xpostxt.Text, Ypostxt.Text, 0F, 0F, 0F, TS, TP, CR, XumList[i], YumList[i], 2);
    
                            UpdateGrid(2);
                        }
    
                        try
                        {
                            Console.WriteLine("Printing out grapth" + i);
                            callingHistogram_Form(Transmission[i].ToArray());
                        }
                        catch (Exception err)
                        {
                            MessageBox.Show(err.Message);
                            Console.WriteLine(err.Message);
                        }
    
                    }
    
                }
                isrunning = false;
            }
    
    
       bool callingHistogram_Form(double[] luminanceValues)
            {
                bool temp = true;
                Console.WriteLine("callingHistogram Start");
                Histogram_Form f = new Histogram_Form();
                f.luminance_values = luminanceValues;
               
                f.step_Pass = stepPass;
                f.start_Step = startStep;
                f.stop_Step = stopStep;
                f.ShowDialog(); // == DialogResult.OK 
                Console.WriteLine("callingHistogram Done");
                return temp;
            }
    
       public void UpdateprocessorBar()
            {
                try
                {
                    if (InvokeRequired)
                    {
    
    
                        BeginInvoke(new Action(() =>
                        {
                            if (callpopflag == true)
                            {
                                callpopflag = false;
                                callPopUp();
                            }
                            popup.Percents = percentpass;
                            popup.Title = title;
                            popup.hidepage = hidepage;
                            popup.Refresh();
                        }));
    
                        return;
                    }
                    if (callpopflag == true)
                    {
    
                        callpopflag = false;
                        callPopUp();
                    }
                    popup.Percents = percentpass;
                    popup.Title = title;
                    popup.hidepage = hidepage;
                    popup.Refresh();
                }
                catch (Exception y)
                {
                    Console.WriteLine("Error: " + y.Message);
                }
            }

    Wednesday, October 23, 2019 5:16 PM
  • You should use async to not freezing GUI thread.

    private async void DimMeaBTN_Click(object sender, EventArgs e)
    {
    ...
    	await TakeMeasurement();
    }
    
    async void TakeMeasurement()
    {
    ...
    	await Task.Run(() => multiRunning());
    }

    • Edited by DerChris88 Wednesday, October 23, 2019 5:49 PM
    Wednesday, October 23, 2019 5:45 PM
  • DerChris88..

    I am using a thread.

    did you see this part :

     camProcess = new Thread(multiRunning);
                camProcess.IsBackground = true;
                camProcess.Start();

    ?

    The GUI only freeze up at the very end of the run, about 20 mins later, before that it is not.

    Also sometimes it take 2 run before it freeze up so 40 min later 

    Wednesday, October 23, 2019 5:54 PM
  • Can you pause debugging if it freeze and tell me where it freeze exactly?

    Maybe you need to use Dispatcher

    • Edited by DerChris88 Wednesday, October 23, 2019 5:57 PM
    Wednesday, October 23, 2019 5:56 PM
  • the problem is when I hit break all it always stops here :

     static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
              // AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
                Application.Run(new Form1());
    
                
            }

    at the application.Run(New Form1());

    Wednesday, October 23, 2019 6:01 PM
  • is there anyway to troubleshoot why a program just freezes up ?

    because I am having a hard time find what in the code is making it fail.

    it is not giving me an error message, it is just freezing up 


     
    Wednesday, October 23, 2019 7:37 PM
  • the line where it breaks in your debugger is strange.

    but anyway: you seem to create a form in your thread (callingHistogram_Form), then show it modal.
    I am not sure this can even work.
    I recommend doing all UI stuff on the UI thread. 
    And you should dispose a form after use.
    Showing a messagebox from a background thread (your exception handler) is also not a good idea.
    Wednesday, October 23, 2019 8:20 PM
  • Eckis, it does works !! I can print out my graphics .

    but sometimes, it  does freeze  up sometimes.

    how do you dispose a form ?

    Wednesday, October 23, 2019 8:43 PM
  • Eckis,

    also I  commented out that line as one point just to see if it would fix the problem.

    so I was not displaying the graph.  It did not help


    Wednesday, October 23, 2019 8:47 PM
  • ok I changed the code to this :

      async void TakeMeasurement()
            {
                isrunning = true;
                PerformanceM.Clear();
                removedoubleclick();
    
                await Task.Run(() => multiRunning());
             
                /* camProcess = new Thread(multiRunning);
                 camProcess.IsBackground = true;
                 camProcess.Start(); */
    
                this.Refresh();
    
            }
    

    it still froze up during the run. but once it got to this.Refresh(), it unfroze the GUI which is good.

    but my question is why?

    Also a run can be only 5 min or it can be 45 mins depending on on how many points the user picks. it works for when you do like 1- 3 points at a time, it when you do more 4-14 point it start freezing up . and it take longer.  the  Process Memory can go up to a little over 10 GB.

    but I am thinking it is a memory or resources problem. The computer only has 20.0 GB of RAM.

    but that should be more that enough right ?



    • Edited by Btb4198 Wednesday, October 23, 2019 9:28 PM
    Wednesday, October 23, 2019 9:27 PM
  • Have you looked at the Windows Event Viewer under applications to see if anything is listed there for your application?

    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, October 23, 2019 10:25 PM
    Moderator
  • Hi Btb4198, 

    Thank you for posting here.

    For your question, you find your C# program freezing.

    I hope the following references can help you.

    1. Prevent C# App from freezing
    2. c# run process without freezing my App's GUI
    3. Form (GUI) hangs when starting threads

    Best Regards,

    Xingyu Zhao


    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.

    Thursday, October 24, 2019 8:50 AM
    Moderator
  • Hi, So I am looking at it now and I see this:

    Application: Tool.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException
       at PixeLINK.Api.GetFeature(Int32, PixeLINK.Feature, PixeLINK.FeatureFlags ByRef, Int32 ByRef, Single[])
       at Pixelation_Tool.PixeLinkDataSource.GetNumPixels()
       at Pixelation_Tool.PixeLinkDataSource.getBufferSize()
       at Pixelation_Tool.PixeLinkDataSource.Framerate2()
       at Pixelation_Tool.PixeLinkDataSource.WorkerThread()
       at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
       at System.Threading.ThreadHelper.ThreadStart()

    that is the only one dealing with my application  I saw

    Thursday, October 24, 2019 1:24 PM
  • when the AccessViolation is at the time of the freeze: the problem is in a 3rd party assembly: "Pixelation_Tool"
    (although then the process should crash, not freeze)

    I wonder why your process needs 10Gb ram.
    Looking at your screenshot: seems like ever 20 seconds the garbage collector has to clean up 5GB,
    which will probably result in short freezes.
    Thursday, October 24, 2019 2:24 PM
  • Eckis

    what do you mean ?


    Monday, October 28, 2019 7:02 PM
  • so why ever 20 seconds the garbage collector has to clear up 5GB.

    I think it is because of this:

    double MeasNoAvgwrapper() { double temp =0D; double[] tempList = new double [MeasNoAvg]; for(int t =0; t < MeasNoAvg; t++) { tempList[t] =AveragePixeLink2(); } temp = tempList.Average(); return temp; }

    double AveragePixeLink2()
            {
                double answer = 0D;
                double l = 0D;
                double tempD = 0D;
                try
                {


                    origPosxStart = (int)((0 - videoSourcePlayer.imagex) / videoSourcePlayer.zoomFactor);
                    origPosYStart = (int)((0 - videoSourcePlayer.imagey) / videoSourcePlayer.zoomFactor);


                    origPosxStop = (int)((videoSourcePlayer.Size.Width - videoSourcePlayer.imagex) / videoSourcePlayer.zoomFactor);
                    origPosYStop = (int)((videoSourcePlayer.Size.Height - videoSourcePlayer.imagey) / videoSourcePlayer.zoomFactor);


                    origPosxStart = (int)((3208D / videoSourcePlayer.Size.Width) * origPosxStart);
                    origPosYStart = (int)((2200D / videoSourcePlayer.Size.Height) * origPosYStart);


                    origPosxStop = (int)((3208D / videoSourcePlayer.Size.Width) * origPosxStop);
                    origPosYStop = (int)((2200D / videoSourcePlayer.Size.Height) * origPosYStop);




                    int[,] data = GetSnapshot();//GetSnapshot();


                    for (int y = origPosYStart; y < origPosYStop; y++)
                    {
                        for (int x = origPosxStart; x < origPosxStop; x++)
                        {
                            tempD = data[y, x];
                            answer = answer + tempD;
                            l++;
                        }
                    }
                    answer = answer / l;
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
                return answer;






            }


    I am taking a picture from a camera  5 times and adding them together and diver by 5.

    And I am doing this from 0 - 350 steps. I have a motor that is mover from 0 - 360 by 1 count steps 

    the picture size is data = {int[2200, 3208]}

    Monday, October 28, 2019 7:13 PM
  • when the size of the array is always the same, you should probably allocate it only once and reuse it.
    But measure with the profiler.

    as for the access violation: is this logged every time your program "freezes"?


    Monday, October 28, 2019 7:50 PM
  • No It does not!
    Monday, October 28, 2019 8:39 PM
  • It has "freezes" up like 3 times today and there was no new error message in the log file 

    I am not sure what is is freezing up like this 

    Monday, October 28, 2019 10:23 PM
  • do you think I should use a list and not an array ? 
    Monday, October 28, 2019 10:27 PM
  • Well, using a List is preferable to an array anyway, in my opinion. For one thing, it doesn't allocate all your space "up front" when it is first created (which, I believe arrays do).

    Whether a List would help or not, I don't know.  Try it and see what happens! =0)


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, November 5, 2019 1:46 AM
    Moderator
  • Just want to remind you that when you debug multithread application, sometimes you need to select your thread from the "ThreadId" dropdown in your debug toolbar. (You'll see something like "IDE cannot find relevent code" if you select other threads, but don't worry, just keep picking the next one until it lands at your background thread.)
    Tuesday, November 5, 2019 6:26 AM
    Answerer