none
program not responding RRS feed

  • Question

  • what does it mean when  you are running your program and sometime it say program not responding and stops working?

    like it does that when I am just playing it

    and sometime when I bult the program outside of microsoft visual studio.

    I am using C# 

    Wednesday, April 21, 2010 1:59 AM

Answers

  • Ok, lets say you've got a TreeView control that has quite a few nodes in it (maybe a file-structure that contains several dozen folders and dozens of files in each folder) and you want to convert this treeview into text. So you create a textbox and a button that allows you to take all the nodes from the treeview and export them in a reader-friendly format into the text box.

    So we'll create a recursive function that will read through each node in the tree and output it to the textbox. Here's some example code:

    // Some global variables to track which node level we're on
    // and how much to indent each line of text
    int levelCounter = 0;
    string levelString = string.Empty;


    private void btnGenerateTextFromTreeView(Object sender, EventArgs e) {
    // We dont want the textbox to draw as we go so to save graphics time we tell
    // the textbox NOT to update its graphics until we're done our calculations
    textBox.SuspendLayout();

    // Clear the textbox for input
    textBox.Clear();


    // This does the actual work of converting each level of nodes into text
    foreach(TreeNode tn in treeView.Nodes) {
    this.Update(); // This line stops your application from appearing as "Not Responding"
    PrintRecursive(tn);
    }

    // this tells the graphics processor it's now ok to draw this object
    textbox.ResumeLayout(false);
    }


    private void PrintRecursive(TreeNode tn) {
    // indent 10 spaces for each new level
    levelString = new String(' ', levelCounter * 10)

    // Write text from this node to textbox
    textBox.AppendText(levelString + tn.Text + Environment.NewLine);

    // Update what level we're on and run this function again
    // Once we're done, decrement the level counter and continue
    foreach(TreeNode aNode in tn.Nodes) {
    levelCounter++;
    PrintRecursive(aNode);
    levelCounter--;
    }
    }

    Assuming there are a lot of nodes to iterate through, all the processor time given to your application will be put into performing the code behind (converting the treenodes into text and appending them to the textbox). This leaves NO processor time for the GUI. By adding the line this.Update(); we force our application to give processor time to the GUI which will cause it to remain validated and not appear as "Not Responding".

    Keep in mind though, this will ONLY update the graphics for your application so it doesn't go white and appear as "Not Responsive". If you're planning on doing lots of processing you may want to consider starting another thread to do the work.

    • Marked as answer by Riten900 Thursday, April 22, 2010 2:26 PM
    Wednesday, April 21, 2010 5:10 PM

All replies

  • also alot of times it will start working fine but then freeze and then just to the end if the program..

     

    I am trying to make a simulation of an iso model  and some thing it work fine but other times it freezes and just  jump to the end...

    this happens both when I run it in mvs and when I buit it outside and try to run it

     

    why?

    Wednesday, April 21, 2010 2:25 AM
  • yeah well it means that you have an error somewhere in you code, usually this kind of behavior happens with infinite loops.

    what I suggest you to do is to run you code with the debugger (especially where you have loops) and see where it freezes.

    Wednesday, April 21, 2010 2:52 AM
  • ok I did but it does not say anything beside:

    Warning 1 Invalid search path 'C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib' specified in 'LIB environment variable' -- 'The system cannot find the path specified. ' project434
    Warning 2 Invalid search path 'C:\Program Files\Microsoft Visual Studio\VC98\lib' specified in 'LIB environment variable' -- 'The system cannot find the path specified. ' project434

    and it froze for like a sec and when all white but it came back

    Wednesday, April 21, 2010 3:02 AM
  • These are not coming from the debugger but from the compiler (the warnings).

    Your problem is clearly behavioral and not grammatical.

    What I suggest you to do is to put a break point where you have loops in you program, and to move step by step (using F10) to see whether you have an infinite loop.

    The "Not responding" message on the window comes if I am not mistaken when your thread is too busy to handle the GUI.

    It can be a

    while(true) {}

    for example.

    But if you use synchronous functions (usually comming from external library) the thread wait for the function to return something.

    Do you use any messaging between apps? do you call external functions from and external dll?

    What kind of computations are you doing?

    Wednesday, April 21, 2010 3:33 AM
  • I don't have any loops

     

    no while loops,  nor for loops, nor  do while loops.

     

    I do use recursion however but that's it...

    Wednesday, April 21, 2010 12:54 PM
  • if you use recursion, are you sure you do not reach a stack overflow?
    Wednesday, April 21, 2010 2:46 PM
  • no,  this is how it works...

     

    in my function I have an if statement that check a random number and if that number is less than the the set number, I call the function over with again, and make a new random number...

     

     The program does work, but some of the time it does "Not responding" and when it  start up again the simulation is done.  the  whole thing is that it needs to show the process everytime.

    Wednesday, April 21, 2010 3:01 PM
  • If the issue is that the application is reported as "Not Responding" but if you give it enough time it will always complete, the issue is that the GUI stops updating. Sometimes long code behind processes cause your GUI not to be updated, in which case calling the Update() method for your main form from time to time during your code behind process will allow the application to paint correctly and stop itself from being reported as "Not Responding"
    Wednesday, April 21, 2010 3:20 PM
  • cool

    um... could you give me an exmple ?

    Wednesday, April 21, 2010 3:52 PM
  • Ok, lets say you've got a TreeView control that has quite a few nodes in it (maybe a file-structure that contains several dozen folders and dozens of files in each folder) and you want to convert this treeview into text. So you create a textbox and a button that allows you to take all the nodes from the treeview and export them in a reader-friendly format into the text box.

    So we'll create a recursive function that will read through each node in the tree and output it to the textbox. Here's some example code:

    // Some global variables to track which node level we're on
    // and how much to indent each line of text
    int levelCounter = 0;
    string levelString = string.Empty;


    private void btnGenerateTextFromTreeView(Object sender, EventArgs e) {
    // We dont want the textbox to draw as we go so to save graphics time we tell
    // the textbox NOT to update its graphics until we're done our calculations
    textBox.SuspendLayout();

    // Clear the textbox for input
    textBox.Clear();


    // This does the actual work of converting each level of nodes into text
    foreach(TreeNode tn in treeView.Nodes) {
    this.Update(); // This line stops your application from appearing as "Not Responding"
    PrintRecursive(tn);
    }

    // this tells the graphics processor it's now ok to draw this object
    textbox.ResumeLayout(false);
    }


    private void PrintRecursive(TreeNode tn) {
    // indent 10 spaces for each new level
    levelString = new String(' ', levelCounter * 10)

    // Write text from this node to textbox
    textBox.AppendText(levelString + tn.Text + Environment.NewLine);

    // Update what level we're on and run this function again
    // Once we're done, decrement the level counter and continue
    foreach(TreeNode aNode in tn.Nodes) {
    levelCounter++;
    PrintRecursive(aNode);
    levelCounter--;
    }
    }

    Assuming there are a lot of nodes to iterate through, all the processor time given to your application will be put into performing the code behind (converting the treenodes into text and appending them to the textbox). This leaves NO processor time for the GUI. By adding the line this.Update(); we force our application to give processor time to the GUI which will cause it to remain validated and not appear as "Not Responding".

    Keep in mind though, this will ONLY update the graphics for your application so it doesn't go white and appear as "Not Responsive". If you're planning on doing lots of processing you may want to consider starting another thread to do the work.

    • Marked as answer by Riten900 Thursday, April 22, 2010 2:26 PM
    Wednesday, April 21, 2010 5:10 PM
  • Also, if you're looking for a random number in a set, instead of doing a recursive function, why don't you just try this?

    Random random = new Random()
    int result, lowBound = 100;
    result = random.Next(lowBound, int.MaxValue);

    Wednesday, April 21, 2010 5:23 PM
  • cool thanks man It is working now

    Wednesday, April 21, 2010 11:55 PM
  • Hi Riten,

    please mark the post(s) that helped you solve your problem as answers for the benefit of other community members and to properly close this thread.

    thanks

    Thursday, April 22, 2010 12:17 AM