# [SOLVED] Progressbar Example Application Not Responding If Moved Or Loses Focus



## writhziden (Jul 23, 2012)

I wrote a simple program to test out the progress bar functionality. I'm having an issue, though. If I run the .exe (included in my attachment), the application gives the (Not Responding) message and the progress bar disappears. This only happens if I try to click on the window to move it, or if I switch to another Window and it has to redraw. 

One method around this is to use the Hide() and Show() commands each time it refreshes, but that causes unwanted side effects like making it difficult to switch between windows or having the progressbar application flash on the screen in a strobe-like fashion. Is there something I am doing wrong?

Included is the release version of the example app and the source code in c++ for further help. 

Please let me know if you know what I am doing wrong.


Here is the code being used:

Form1.h

```
#pragma once

namespace progressBar {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	public: System::Windows::Forms::ProgressBar^  progressBar1;
	protected: 
	public: System::Windows::Forms::Label^  label1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// progressBar1
			// 
			this->progressBar1->Location = System::Drawing::Point(41, 119);
			this->progressBar1->Name = L"progressBar1";
			this->progressBar1->Size = System::Drawing::Size(199, 23);
			this->progressBar1->TabIndex = 0;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->ForeColor = System::Drawing::Color::Red;
			this->label1->Location = System::Drawing::Point(64, 91);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(153, 20);
			this->label1->TabIndex = 1;
			this->label1->Text = L"Progress Bar Test";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->progressBar1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	};
}
```

progressBar.cpp

```
// progressBar.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include <sstream>
#include <string>

using namespace progressBar;
using namespace std;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Form1^ myProgress = gcnew Form1();
	myProgress->Show();
	myProgress->progressBar1->Maximum = 10000000;
	for(int i = 0; i < 10000000; i++){
		myProgress->label1->ForeColor = System::Drawing::Color::Black;
		String^ str;
		stringstream sstemp;
		sstemp << "Running "<< i + 1 << " of 10000000";
		string stringstore;
		stringstore = sstemp.str();
		str = gcnew String(stringstore.c_str());
		if(i%10000 == 0){
			myProgress->label1->Text = str;
			myProgress->progressBar1->Value = i;
			myProgress->Refresh();
		}
	}
	return 0;
}
```


----------



## AceInfinity (Jan 21, 2012)

*Re: Progressbar Example Application Not Responding If Moved Or Loses Focus*

.NET C++ , I understand what's going on here, and it's not that you're doing anything wrong. The issue is that label updates that you're doing on the UI Thread get interrupted by anything in the UI thread's queue which would cause it to have to redraw or re-validate itself. It's like having a stack of plates piled up in both of your hands, and having somebody toss a tennis ball at you. Do you drop the plates to catch the tennis ball, or let the tennis ball hit you and keep the plates in your hands? It's really an overload in the UI queue which causes the UI and program to go unresponsive.

This is a threading issue, not even a progressbar issue, although it's related to both the label and the progressbar being updated simultaneously nonstop.

What you could do, and what you should be doing if you're going to update a control that fast and repetitively, is to dynamically create it and assign it to the form container. Updates can then happen on the newly created thread instead of affecting any of the UI processing.

Thus I should see this in there:

```
using namespace System::Threading;
```
I could give you many examples, but I don't really like C++ with .NET as much as standard C++ libraries. I'd be able to provide you with a VB.NET or C# example though. Basically the same thing anyways... With the exception of syntax.

:thumb:


----------



## writhziden (Jul 23, 2012)

*Re: Progressbar Example Application Not Responding If Moved Or Loses Focus*

Thanks. I actually figured this out on my own and reached the same conclusion. Much appreciated that you took the time to write it out for me, though. 

The BSOD Apps are now improved with this addition!


----------



## AceInfinity (Jan 21, 2012)

You should've asked lol. This is my area of expertise.


----------



## writhziden (Jul 23, 2012)

I had been wanting to add the progress bar to my apps for a couple months, but when I first looked into it, it seemed I would have to redesign them completely. I gave up on it for a bit to focus on other aspects of the apps, but I forgot about adding the progress bar stuff. A few days ago, Stephen (Tekno Venus) was checking out your apps area (AutoBlue BSOD Tool Development), and I watched the video of what you had done. Looked like what I had in mind originally, so I delved deeper.

I gave it some more thought, and it hit me a couple days ago that I could just add one form and use it as an object within the class that I wanted the progress bar for. Don't know why it did not occur to me sooner. Anyway, you were the reminder and then inspiration behind the revamped progress bar output, so I wanted to give you some credit. 

Your original work would have been a great starting point for the apps that I redesigned with Richard's help. May I ask why you did not pursue the blue screen apps further?


----------



## AceInfinity (Jan 21, 2012)

writhziden said:


> I had been wanting to add the progress bar to my apps for a couple months, but when I first looked into it, it seemed I would have to redesign them completely. I gave up on it for a bit to focus on other aspects of the apps, but I forgot about adding the progress bar stuff. A few days ago, Stephen (Tekno Venus) was checking out your apps area (AutoBlue BSOD Tool Development), and I watched the video of what you had done. Looked like what I had in mind originally, so I delved deeper.
> 
> I gave it some more thought, and it hit me a couple days ago that I could just add one form and use it as an object within the class that I wanted the progress bar for. Don't know why it did not occur to me sooner. Anyway, you were the reminder and then inspiration behind the revamped progress bar output, so I wanted to give you some credit.
> 
> Your original work would have been a great starting point for the apps that I redesigned with Richard's help. May I ask why you did not pursue the blue screen apps further?


That's a good question... And to be honest with you, I would ask myself the same thing. I think it came down to just not having enough time at that point in my life, dealing with other priorities and such. So I stopped development there, and never got back to it, because when I did have spare time available again, I had newer and bigger projects in mind.

Maybe i'll come out with something even better soon, we'll see... I should, it was a good project for me, and looking back at that, I could do better. It was more of an experiment at the time though, and features were being added on the go. Now that I have a bigger picture of what I would like to have for that kind of app. I think a revised version would almost make AutoBlue be considered 'obsolete'. I actually didn't efficiently utilize multi-threading in that app either to be honest. It's horrible lol :rofl:


----------

