none
Thread : Window is Hanged !!!!

    Вопрос

  • public partial class Form1 : Form
        {

            delegate void PrintCallback();
            private Thread t = null;

            public Form1()
            {
                InitializeComponent();
                this.WindowState = FormWindowState.Maximized;
            }


            private void button1_Click(object sender, EventArgs e)
                {
                    textBox2.Text = ("Yup");
                    this.t = new Thread(Print);
                    this.t.Start();
                    t.IsBackground = true;
                    textBox2.Text = ("Done");
                }

                private void button2_Click(object sender, EventArgs e)
                {  
                    textBox2.Text = ("Button2");
                    textBox2.Refresh();
                }

                private void button3_Click(object sender, EventArgs e)
                {   
                    textBox2.Text = ("3 Button");
                    textBox2.Refresh();
                }
                private void Print()
                {
                    if (this.textBox1.InvokeRequired)
                    {
                        PrintCallback d = new PrintCallback(Print);
                        this.BeginInvoke(d, new object[] { });
                    }
                    else
                    {
                        int i;
                        for (i = 0; i < 100; i++)
                        {
                            this.textBox1.Text = ("" + i);
                            textBox1.Refresh();
                            button1.Text = ("Hello");
                            button1.Refresh();
                            Thread.Sleep(50);
                            textBox2.Text = ("In Thread");
                            textBox2.Refresh();
                            Thread.Sleep(0);
                            Thread.Sleep(50);
                        }
                    }

      }

    }

    When i press button1 the thread starts and count is executed. During the time when count is executed, i am not able to click anywhere on window, then is the thread actually running?

    If yes then why am i not able to click anywhere else. I am righting the count program in thread so that i can execute these other tasks when thread is running in parallel....

    Please can anyone help me ..

     

                                                                        
    • Перемещено CoolDadTxMVP 16 апреля 2012 г. 14:11 Winforms related (From:Visual C# General)
    16 апреля 2012 г. 6:16

Ответы

  • RamuDamu wrote:


    When i press button1 the thread starts and count is executed. During the time when count is executed, i am not able to click anywhere on window, then is the thread actually running?

    If yes then why am i not able to click anywhere else. I am righting the count program in thread so that i can execute these other tasks when thread is running in parallel....



    Please can anyone help me ..

    Your loop is executing on the main thread and this means that every other operation cannot be executed.

    The main thread is the one where the UI is running. On this thread there is the message queue running. This queue receive all the messages for every request that must be dispatched to the controls. For example keyboard, mouse and painting request are enqueued there.

    When you begininvoke your delegate, this goes to the queue too and ensure the loop is executed on the main thread.
    So your loop is stealing time to the queue that can't dispatch messages to the methods that, for example, redraw the window.

    If you need to do something at regular intervals, you should post messages in the queue at those intervals. This is done from the windows forms timer component.
    So avoid using thread.sleep and looping for a long time inside the main thread.

    BTW there is no need to call refresh on controls.


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Предложено в качестве ответа Bob Wu-MTModerator 18 апреля 2012 г. 2:38
    • Помечено в качестве ответа Bob Wu-MTModerator 30 апреля 2012 г. 3:14
    16 апреля 2012 г. 6:51
  • RamuDamu wrote:

    Hey ,

          I want to do a program in which i have to show the flow of a flowchart. It showed be paused, resumed  and stopped if needed. All this showed be done in thread as the buttons showed be active( Click able).  In showing the flow the Node(here TextBox)  background is highlighted for a specific time and then de- highlighted later. Then similar happens to a next NODE and then next.

    There are two main principles you have to respect in order not to screw up your app:
    - UI cannot be accessed from other threads
    - UI main thread must not be blocked.

    If you have fixed timings, the easiest solution is to use a Timer (pick it from the toolbox).

    You can set the interval and do whatever you want directly in the elapsed event exposed by the timer itself. This event will run in the main thread so you must update the controls and quickly exit from the method. Before exiting, you simply update the interval to the timing for the next update.
    Imagine that you have a sequence of instructions and you execute each instruction every time the timer event is called. Changing the interval simply means that your sequence can be faster or slower.


    But problem i have is when i start highlighting the nodes in thread , at same time when i try to press the button the buttons are not been clicked. After everything in thread is over then the event that i had kept in button click is executed.

    This is happening because you are looping inside the main thread that is the same that needs to update the UI.
    You should never block that thread.
     > I want a program in which i can show the flow and concurrently if needed

    pause it or stop it.

    very simple, just follow the timer suggestion above


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Предложено в качестве ответа Bob Wu-MTModerator 18 апреля 2012 г. 2:38
    • Помечено в качестве ответа Bob Wu-MTModerator 30 апреля 2012 г. 3:14
    16 апреля 2012 г. 14:02

Все ответы

  • RamuDamu wrote:


    When i press button1 the thread starts and count is executed. During the time when count is executed, i am not able to click anywhere on window, then is the thread actually running?

    If yes then why am i not able to click anywhere else. I am righting the count program in thread so that i can execute these other tasks when thread is running in parallel....



    Please can anyone help me ..

    Your loop is executing on the main thread and this means that every other operation cannot be executed.

    The main thread is the one where the UI is running. On this thread there is the message queue running. This queue receive all the messages for every request that must be dispatched to the controls. For example keyboard, mouse and painting request are enqueued there.

    When you begininvoke your delegate, this goes to the queue too and ensure the loop is executed on the main thread.
    So your loop is stealing time to the queue that can't dispatch messages to the methods that, for example, redraw the window.

    If you need to do something at regular intervals, you should post messages in the queue at those intervals. This is done from the windows forms timer component.
    So avoid using thread.sleep and looping for a long time inside the main thread.

    BTW there is no need to call refresh on controls.


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Предложено в качестве ответа Bob Wu-MTModerator 18 апреля 2012 г. 2:38
    • Помечено в качестве ответа Bob Wu-MTModerator 30 апреля 2012 г. 3:14
    16 апреля 2012 г. 6:51
  • Hey ,

          I want to do a program in which i have to show the flow of a flowchart. It showed be paused, resumed  and stopped if needed. All this showed be done in thread as the buttons showed be active( Click able).  In showing the flow the Node(here TextBox)  background is highlighted for a specific time and then de- highlighted later. Then similar happens to a next NODE and then next.

    But problem i have is when i start highlighting the nodes in thread , at same time when i try to press the button the buttons are not been clicked. After everything in thread is over then the event that i had kept in button click is executed.

    I want a program in which i can show the flow and concurrently if needed pause it or stop it.

    Please help

    16 апреля 2012 г. 12:03
  • You should set the IsBackground property to true before you start the thread.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    16 апреля 2012 г. 12:08
  • RamuDamu wrote:

    Hey ,

          I want to do a program in which i have to show the flow of a flowchart. It showed be paused, resumed  and stopped if needed. All this showed be done in thread as the buttons showed be active( Click able).  In showing the flow the Node(here TextBox)  background is highlighted for a specific time and then de- highlighted later. Then similar happens to a next NODE and then next.

    There are two main principles you have to respect in order not to screw up your app:
    - UI cannot be accessed from other threads
    - UI main thread must not be blocked.

    If you have fixed timings, the easiest solution is to use a Timer (pick it from the toolbox).

    You can set the interval and do whatever you want directly in the elapsed event exposed by the timer itself. This event will run in the main thread so you must update the controls and quickly exit from the method. Before exiting, you simply update the interval to the timing for the next update.
    Imagine that you have a sequence of instructions and you execute each instruction every time the timer event is called. Changing the interval simply means that your sequence can be faster or slower.


    But problem i have is when i start highlighting the nodes in thread , at same time when i try to press the button the buttons are not been clicked. After everything in thread is over then the event that i had kept in button click is executed.

    This is happening because you are looping inside the main thread that is the same that needs to update the UI.
    You should never block that thread.
     > I want a program in which i can show the flow and concurrently if needed

    pause it or stop it.

    very simple, just follow the timer suggestion above


    Raffaele Rialdi  http://www.iamraf.net
    Weblog: http://blogs.ugidotnet.org/raffaele
    Microsoft MVP profile https://mvp.support.microsoft.com/profile/raffaele
    UGIdotNET - http://www.ugidotnet.org/


    Raffaele Rialdi [MVP] My articles and videos: http://www.iamraf.net Italian blog: http://blogs.ugidotnet.org/raffaele
    • Предложено в качестве ответа Bob Wu-MTModerator 18 апреля 2012 г. 2:38
    • Помечено в качестве ответа Bob Wu-MTModerator 30 апреля 2012 г. 3:14
    16 апреля 2012 г. 14:02