473,662 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Form freezing form

Why in this code the form *does not refresh* when it gets the focus/after some time?

#using <mscorlib.dll >
#using <system.windows .forms.dll>
#using <system.dll>
#using <system.drawing .dll>
#include <windows.h>
#include <cstdlib>
// ==> Just a Form with two labels, one progress bar, opacity 90%.
// ==> label1, and progressBar are public and accessed through main().
// ==> Placed some refreshes but the problem persists.
__gc class Form1: public System::Windows ::Forms::Form
{
private:
System::Windows ::Forms::Label * label2;

public:
System::Windows ::Forms::Progre ssBar * progressBar1;
System::Windows ::Forms::Label * label1;

Form1()
{
this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );

this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );
this->SuspendLayout( );

this->label2->Font = new System::Drawing ::Font(S"Micros oft Sans Serif", 9.75F,
System::Drawing ::FontStyle::Re gular, System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 61);
this->label2->Location = System::Drawing ::Point(14, 16);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing ::Size(264, 40);
this->label2->TabIndex = 1;
this->label2->Text = S"Message 1...";
this->label2->TextAlign = System::Drawing ::ContentAlignm ent::MiddleCent er;

this->progressBar1->Location = System::Drawing ::Point(10, 176);
this->progressBar1->Name = S"progressBar1" ;
this->progressBar1->Size = System::Drawing ::Size(272, 23);
this->progressBar1->TabIndex = 2;

this->label1->Font = new System::Drawing ::Font(S"Micros oft Sans Serif", 8.25F,
System::Drawing ::FontStyle::Re gular, System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 61);
this->label1->Location = System::Drawing ::Point(14, 80);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing ::Size(264, 64);
this->label1->TabIndex = 3;

this->AutoScaleBaseS ize = System::Drawing ::Size(5, 13);
this->ClientSize = System::Drawing ::Size(292, 271);
this->Controls->Add(this->label1);
this->Controls->Add(this->progressBar1 );
this->Controls->Add(this->label2);
this->FormBorderStyl e = System::Windows ::Forms::FormBo rderStyle::Fixe d3D;

this->Name = S"Form1";
this->Opacity = 0.9;
this->ShowInTaskba r = false;
this->Text = S"Some Application";
this->TopMost = true;
this->ResumeLayout(f alse);
}
};
int main()try
{
Form1 *pForm1= __gc new Form1;

pForm1->Show();
pForm1->Refresh();

pForm1->label1->Text= "Diagnostic code";
pForm1->progressBar1->Value= 0;
pForm1->progressBar1->Maximum= 100;

while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1) ;
pForm1->Refresh();
}

pForm1->progressBar1->Value= 0;
}

}

catch(System::E xception *pe)
{
using namespace System;

Console::WriteL ine("Error: {0}", pe->Message);

return EXIT_FAILURE;
}
Nov 17 '05 #1
13 2463
Ioannis Vranos wrote:

How about calling Application::Do Events() instead of pForm1->Refresh()?
DoEvents processes all messages in the message queue and returns. It's
not nearly as good as multithreading, but it's very simple and prevents
your application from freezing while you are processing something that
lasts more than a 100 milliseconds. Call DoEvents periodically, but not
too often, because it slows down your application.

Why in this code the form *does not refresh* when it gets the
focus/after some time?
while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1) ;
pForm1->Refresh();
// <<<<<<<<<<<<<<< <<<<<<
Application::Do Events();
// <<<<<<<<<<<<<<< <<<<<<
}

pForm1->progressBar1->Value= 0;
}

}


Tom
Nov 17 '05 #2
Tamas Demjen wrote:
How about calling Application::Do Events() instead of pForm1->Refresh()?
DoEvents processes all messages in the message queue and returns. It's
not nearly as good as multithreading, but it's very simple and prevents
your application from freezing while you are processing something that
lasts more than a 100 milliseconds. Call DoEvents periodically, but not
too often, because it slows down your application.


Thanks for the tip Tamas. It appears that it solves the problem, but another one exists.
When I close the Form by pressing the X button, the application process does not
terminate. Any ideas?
Here is the corrected code:
#using <mscorlib.dll >
#using <system.windows .forms.dll>
#using <system.dll>
#using <system.drawing .dll>
#include <windows.h>
#include <cstdlib>
// ==> Just a Form with two labels, one progress bar, opacity 90%.
// ==> label1, and progressBar are public and accessed through main().
// ==> Placed some refreshes but the problem persists.
__gc class Form1: public System::Windows ::Forms::Form
{
private:
System::Windows ::Forms::Label * label2;

public:
System::Windows ::Forms::Progre ssBar * progressBar1;
System::Windows ::Forms::Label * label1;

Form1()
{
this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );

this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );
this->SuspendLayout( );

this->label2->Font = new System::Drawing ::Font(S"Micros oft Sans Serif",
9.75F, System::Drawing ::FontStyle::Re gular, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 61);
this->label2->Location = System::Drawing ::Point(14, 16);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing ::Size(264, 40);
this->label2->TabIndex = 1;
this->label2->Text = S"Message 1...";
this->label2->TextAlign = System::Drawing ::ContentAlignm ent::MiddleCent er;

this->progressBar1->Location = System::Drawing ::Point(10, 176);
this->progressBar1->Name = S"progressBar1" ;
this->progressBar1->Size = System::Drawing ::Size(272, 23);
this->progressBar1->TabIndex = 2;

this->label1->Font = new System::Drawing ::Font(S"Micros oft Sans Serif",
8.25F, System::Drawing ::FontStyle::Re gular, System::Drawing ::GraphicsUnit: :Point,
(System::Byte)1 61);
this->label1->Location = System::Drawing ::Point(14, 80);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing ::Size(264, 64);
this->label1->TabIndex = 3;

this->AutoScaleBaseS ize = System::Drawing ::Size(5, 13);
this->ClientSize = System::Drawing ::Size(292, 271);
this->Controls->Add(this->label1);
this->Controls->Add(this->progressBar1 );
this->Controls->Add(this->label2);
this->FormBorderStyl e = System::Windows ::Forms::FormBo rderStyle::Fixe d3D;

this->Name = S"Form1";
this->Opacity = 0.9;
this->ShowInTaskba r = false;
this->Text = S"Some Application";
this->TopMost = true;
this->ResumeLayout(f alse);
}
};
int main()try
{
using namespace System;
using namespace System::Windows ::Forms;
using namespace System::Threadi ng;

Form1 *pForm1= __gc new Form1;

pForm1->Show();

pForm1->label1->Text= "Diagnostic code";
pForm1->progressBar1->Value= 0;
pForm1->progressBar1->Maximum= 100;

while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1) ;

if(i%10== 0)
Application::Do Events();

Thread::Sleep(1 00);
}

pForm1->progressBar1->Value= 0;
}

}

catch(System::E xception *pe)
{
using namespace System;

Console::WriteL ine("Error: {0}", pe->Message);

return EXIT_FAILURE;
}
Nov 17 '05 #3
Ioannis Vranos wrote:
Thanks for the tip Tamas. It appears that it solves the problem, but
another one exists. When I close the Form by pressing the X button, the
application process does not terminate. Any ideas?


If I'm not mistaken, your main function if in a forever loop. This is
not typical in real-world applications. Usually your processing finishes
sooner or later. If you want to be able to cancel a long lasting
operation, you could have a Cancel button, which would set an
IsCancelled flag on click. Your main loop could then check against this
flag. This is how typical applications work.

I almost always perfer using threads, as opposed to DoEvents, because
threads have automatic load balancing, and you don't have to figure out
how often DoEvents should be called. It's also dangerous to call
DoEvents, because it may call other functions you are not prepared for
(it can have serious side effects, unwanted recursion, etc.).

When using DoEvents, your application won't be as smooth and responsive
as using threads. You either call DoEvents too often, which slows down
your processing, or you call it too rarely, which makes the GUI sluggish
during processing. There is no such problem with threads, as the system
scheduler is very smart, and you can even assign priorities to threads.
Assigning low priority you can instruct the system to "do it if you have
time, otherwise don't".

Tom
Nov 17 '05 #4
You never give a chance to your application to handle the paint message
posted to the application queue, your infinite loop takes all processing
time.
One bad advise would be to call DoEvents, but he! we are slowly moving to 64
bit OS'ses and this is a something that's been used on 16 bit non-preemptive
windows.
You should run your task (not really a realistic one) on another thread and
update the UI from there using Control::Invoke or BeginInvoke.

Willy.
Willy.

"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
Why in this code the form *does not refresh* when it gets the focus/after
some time?

#using <mscorlib.dll >
#using <system.windows .forms.dll>
#using <system.dll>
#using <system.drawing .dll>
#include <windows.h>
#include <cstdlib>
// ==> Just a Form with two labels, one progress bar, opacity 90%.
// ==> label1, and progressBar are public and accessed through main().
// ==> Placed some refreshes but the problem persists.
__gc class Form1: public System::Windows ::Forms::Form
{
private:
System::Windows ::Forms::Label * label2;

public:
System::Windows ::Forms::Progre ssBar * progressBar1;
System::Windows ::Forms::Label * label1;

Form1()
{
this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );

this->label2 = new System::Windows ::Forms::Label( );
this->progressBar1 = new System::Windows ::Forms::Progre ssBar();
this->label1 = new System::Windows ::Forms::Label( );
this->SuspendLayout( );

this->label2->Font = new System::Drawing ::Font(S"Micros oft Sans Serif",
9.75F, System::Drawing ::FontStyle::Re gular,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 61);
this->label2->Location = System::Drawing ::Point(14, 16);
this->label2->Name = S"label2";
this->label2->Size = System::Drawing ::Size(264, 40);
this->label2->TabIndex = 1;
this->label2->Text = S"Message 1...";
this->label2->TextAlign = System::Drawing ::ContentAlignm ent::MiddleCent er;

this->progressBar1->Location = System::Drawing ::Point(10, 176);
this->progressBar1->Name = S"progressBar1" ;
this->progressBar1->Size = System::Drawing ::Size(272, 23);
this->progressBar1->TabIndex = 2;

this->label1->Font = new System::Drawing ::Font(S"Micros oft Sans Serif",
8.25F, System::Drawing ::FontStyle::Re gular,
System::Drawing ::GraphicsUnit: :Point, (System::Byte)1 61);
this->label1->Location = System::Drawing ::Point(14, 80);
this->label1->Name = S"label1";
this->label1->Size = System::Drawing ::Size(264, 64);
this->label1->TabIndex = 3;

this->AutoScaleBaseS ize = System::Drawing ::Size(5, 13);
this->ClientSize = System::Drawing ::Size(292, 271);
this->Controls->Add(this->label1);
this->Controls->Add(this->progressBar1 );
this->Controls->Add(this->label2);
this->FormBorderStyl e = System::Windows ::Forms::FormBo rderStyle::Fixe d3D;

this->Name = S"Form1";
this->Opacity = 0.9;
this->ShowInTaskba r = false;
this->Text = S"Some Application";
this->TopMost = true;
this->ResumeLayout(f alse);
}
};
int main()try
{
Form1 *pForm1= __gc new Form1;

pForm1->Show();
pForm1->Refresh();

pForm1->label1->Text= "Diagnostic code";
pForm1->progressBar1->Value= 0;
pForm1->progressBar1->Maximum= 100;

while(true)
{
for(long i= 0; i< 100; ++i)
{
pForm1->progressBar1->Increment(1) ;
pForm1->Refresh();
}

pForm1->progressBar1->Value= 0;
}

}

catch(System::E xception *pe)
{
using namespace System;

Console::WriteL ine("Error: {0}", pe->Message);

return EXIT_FAILURE;
}

Nov 17 '05 #5
Willy Denoyette [MVP] wrote:
You never give a chance to your application to handle the paint message
posted to the application queue, your infinite loop takes all processing
time.
One bad advise would be to call DoEvents, but he! we are slowly moving to 64
bit OS'ses and this is a something that's been used on 16 bit non-preemptive
windows.
You should run your task (not really a realistic one) on another thread and
update the UI from there using Control::Invoke or BeginInvoke.

This is about a window displaying progress of file operations. I assume a form is the only
thing that can be used as a "window". I want to display each file operation. Is your
suggestion that I can't do much else than skipping to display all file operations, but
instead display only from here and there?

The behaviour I am experiencing is exactly the same with this sample application, the form
freezes, but the program itself (a method of the form calling a couple of other methods of
the form) continues normally.
Nov 17 '05 #6
Ioannis Vranos wrote:
This is about a window displaying progress of file operations. I assume
a form is the only thing that can be used as a "window". I want to
display each file operation. Is your suggestion that I can't do much
else than skipping to display all file operations, but instead display
only from here and there?

The behaviour I am experiencing is exactly the same with this sample
application, the form freezes, but the program itself (a method of the
form calling a couple of other methods of the form) continues normally.

It seems that the problem got fixed after placing an Application::Do Events() call at each
iteration.

However if you have some more elegant idea, I would be glad to hear it (executing the file
processing method in a separate thread and letting it modify the Form from there does not
work, the program freezes).
Nov 17 '05 #7
Ioannis Vranos wrote:
It seems that the problem got fixed after placing an
Application::Do Events() call at each iteration.

However if you have some more elegant idea, I would be glad to hear it
(executing the file processing method in a separate thread and letting
it modify the Form from there does not work, the program freezes).

you= you both, or someone else.
Nov 17 '05 #8
Ioannis Vranos wrote:
However if you have some more elegant idea, I would be glad to hear it
(executing the file processing method in a separate thread and letting
it modify the Form from there does not work, the program freezes).


Using a separate threads is the only reasonable and elegant solution.
It's not terribly hard to do, but you really have to be careful. Writing
thread safe code is hard, and if you miss something, it will freeze or
crash.

I haven't tried threads in .NET/WinForms, but use them all the time in
unmanaged applications, when a progress dialog is needed for a
long-lasting operation.

When you update the form from the thread, are you taking care of the
synchronization ? Only the main thread is allowed to modify the GUI, you
can't do it directly from the thread, you have to do the update via
Control::Invoke .

There are tutorials out there, although few of them are C++, you can
still get the point from them:
http://samples.gotdotnet.com/quickst...olinvoker.aspx
http://samples.gotdotnet.com/quickst...rshalling.aspx
http://www.dotnetspider.com/technology/kbpages/903.aspx

Tom
Nov 17 '05 #9
Tamas Demjen wrote:
When you update the form from the thread, are you taking care of the
synchronization ? Only the main thread is allowed to modify the GUI, you
can't do it directly from the thread, you have to do the update via
Control::Invoke .

Do you mean I should create a delegate for this? However my question is this, as far as I
understand, before the Application::Do Events() use, the form got "frozen" because of the
frequency of the updates. Why this will not happen when Invoke() is used?
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

57
3591
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
0
1217
by: Rob | last post by:
Problem: Main form and progress bar do not update as data manipulation progresses I have a progress bar located in the status bar and some text boxes on the main form that show the progression of records being read and saved. Me.Repaint is in code to update the form and progress bar. Sometimes the form and progress bar work, but most of the time they freeze and do not show the end-user what's going on. If I <crtl><break> to...
1
1801
by: Kevin McCarthy | last post by:
I have a form I am opening in Dialog mode, but noticed if I click on the title bar, the form and entire application freezes, with ctl-alt-del the only way out. I've tried to debug by putting stops on all events for the form, but nothing appears to fire. Task Manager dialog says that it cannot close the application because it "is waiting for a response" from me. I have no idea why this is happening - can anyone help? Using Windows 2k...
1
2656
by: Jim Heavey | last post by:
Hello, there is something that I am forgetting, I just don't remember what is. I create a form which will server as a dialog box. I set the "CancelButton", "AcceptButton" and the "FormBorderStyle = "FixedDialog" for the properties of the form. When the user presses the "Accept" button, I do some editing so see if the data is acceptable. When the data is NOT acceptable, I think I am suppose to set some property on the form? which will...
6
1945
by: Steven K | last post by:
Hello, I am having a problem where my computer is freezing when I run a ASP.net project. It freezes in the debugger, or if I try to run it as localhost. I cannot even access the windows Taskbar, or anything. I have to power off my computer. It is a brand new machine, XP Pro, VS.NET 2003. Other Notes:
1
1347
by: boB | last post by:
When I try to run a program - any program - my environment all of a sudden starts freezing up. Even an empty solution. It will build, but never run the built program and the GUI won't respond and needs to be killed with Task Manager. It was running fine until I experimented with VBPowerPack copying it into a current folder instead of using Copy Local. Now, even after deleting it completely, I'm frozen no matter what. To make matters worse,...
2
1634
by: Job Lot | last post by:
Is there any way of freezing columns in Windows Forms Data Grid control? Thanks
7
2036
by: bearophileHUGS | last post by:
Most of my ideas seem usless or stupid, but I think expressing them here doesn't harm much. This is an idea for Py 3.0, because it's not backward compatible. Dicts and sets require immutable keys, like tuples or frozensets, but to me they look like a duplication. So the idea is to remove tuples and frozensets (and replace the few other uses of tuples with lists, like the % interpolation), and to add a freeze operation, to freeze lists,...
1
1607
by: JasonT | last post by:
Hi. I have created a dll in VC++ 08 that exports a CLR windows form class. I call into this dll from an external (closed source, third-party) application to instantiate the form and embed it into the external application window using SetParent(). The problem is, the form runs in the external application window, but the external application no longer updates (renders, responds to close button click, etc.). Now I know the reason is that in...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.