Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I Have 2 Form In My Project
In Form 1 Run This Code
C#
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    while (true)
    {
        i++;
        textbox1.text = i.toString();
    }
}

In Form 2 Have One Animated Gif By PictureBox
But My Question When Not Responding My Application For Example In While(true)
The second form does not display anything but there in the form2 of a tool(pictureBox) and Show One Gif

What I have tried:

while(true) is sample code For Not Responding
I Want Show True PictureBox When Not Responding
Posted
Updated 2-Oct-16 22:15pm

This is a bad way to do things, but if you must try using Application.DoEvents() :
C#
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    while (true)
    {
        i++;
        textbox1.text = i.ToString();
        Application.DoEvents();
    }
}

Alternatively use Task :
C#
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
    Form2 F = new Form2();
    F.Show();
    Task.Factory.StartNew( () =>
    {
        i++;
        textbox1.text = i.ToString();
    });
}
 
Share this answer
 
No, it won't.
In fact what that code does is lock up the UI thread completely, because it can't do anything to look at the event that are occurring because it is completely busy inside your while loop - and since that never ends, no more events are ever actioned.
Windows is a message based operating system - and everything you (or the user) does is translated into a message which is passed around the system and processed by a "hidden" part of your code called a "Message Loop". If you do no exist from an Event Handler then control never gets back to the message loop and it never gets to look at further messages - so your UI freezes up and never recovers.
What you are doing inside that loop wouldn't display anything anyway, even without the additional Form for exactly the same reasons.

Probably, you need to look at starting a second thread to do something (if that code isn't a placeholder for your real actions) - but if you do that, you need to be pretty careful as you can't access controls unless it's on the main thread. It gets complicated, and I'm not going to try and explain it to you for that code!

Basically, what you are trying to do there won't work: and we can;t tell you what to actually do unless we have a much, much better idea what you are trying to do in reality.
 
Share this answer
 
if you do huge work in one thread and also want to display others thing it will not possible until and unless your code execution finished

for doing this you can use the below idea.
You can Use background worker for doing your not responding work
while your application thread will display the gif Picture box
that's it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900