I am working with the backgroundworker and the datagridview. I have noticed a difference.
I have put a loop that puts a value to one cell in the datagrid first in a buttonevent.
After this loop is finished wich creates a timer textfile that tells how long the process did take, the code executes the backgroundworker that has a Sleep(10000) before the very same loop executes with a timer and put this to another file under C:\\

Loop under button takes: 26 seconds
Loop in backgroundworker takes: 36 seconds

Why is this difference and what can I do about it as the backgroundworker needs to do it as fast as in the buttonevent ?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
	time_t now = time(0);
	struct tm* tm = localtime(&now);
	ofstream Timer1;
	Timer1.open("C:\\Timercount1.txt");
	int Minute;
	int Second;

	Minute = tm->tm_min;
	Second = tm->tm_sec;

	[B]for( int s = 0; s < 111111111; s++ )
	{
		dataGridView1->Rows[0]->Cells[1]->Value = "Value1";
	}[/B]
	time_t now2 = time(0);
	struct tm* tm2 = localtime(&now2);
		

Timer1 << "Start " << Minute << "." << Second << " " << "End " << tm2->tm_min << "." << tm2->tm_sec;
Timer1.close();
backgroundWorker1->RunWorkerAsync();
}




private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) 
{
	time_t now = time(0);
	struct tm* tm = localtime(&now);

	ofstream Timer1;
	Timer1.open("C:\\Timercount2.txt");
	int Minute;
	int Second;
				

	for( int w = 0; w < 5; w++ )
	{
	 w = w - 1;

	 Thread::Sleep(10000); //10 seconds


	 for( int i = 0; i < 5; i++ )
	 {
		i = i - 1;

		Minute = tm->tm_min;
		Second = tm->tm_sec;

[B]	for( int s = 0; s < 111111111; s++ )
	{
		dataGridView1->Rows[0]->Cells[1]->Value = "Value1";
	}[/B]			
		time_t now2 = time(0);
		struct tm* tm2 = localtime(&now2);

Timer1 << "Start " << Minute << "." << Second << " " << "End " << tm2->tm_min << "." << tm2->tm_sec;
Timer1.close();
break;
	 }

	break;



	} //New for loop that updates/checks ONCE 10 second
}

Lemme get this straight, you're complaining something is 10 seconds slower because it sleeps for 10 seconds?

But is that what is really happening because I set the timer just before and after the processing loop not before the sleep(10000) as for these lines. So the sleep shouldn´t have any effect on the timer ?:

Minute = tm->tm_min;
Second = tm->tm_sec;

	for( int s = 0; s < 111111111; s++ )
	{
		dataGridView1->Rows[0]->Cells[1]->Value = "Value1";
	}			
time_t now2 = time(0);
struct tm* tm2 = localtime(&now2);

Timer1 << "Start " << Minute << "." << Second << " " << "End " << tm2->tm_min << "." << tm2->tm_sec;

backgroundWorker1_DoWork does
time
Sleep
Work
time

Forground does
time
Work
time

Oh, and you might try removing those pointless for loops which you break out of before they loop.

Thanks, Salem, I understand, I thought that when calling tm-> I would get the present time and not the time where this was declared. The breaks I will take away so the loops will be needed as it checks or updates every interval(10 seconds).
I might check out my real code again because something is still wrong and give another example.

time_t now = time(0);
struct tm* tm = localtime(&now);

Now I have localized the problem part, it took a few hour to find the problem, so this is a simplified example.
What happens is that I first have some code in the buttonevent handler. When pressing the button the datagrid fills up with 2000 rows and there is a code looking for what colors that will be put to Cells[2].

When this is done wich takes approx: 30 seconds, the backgroundworker is activated and after 60 seconds, Sleep(60000) the buttonevent handler is called so the same code will shoot again.

Now is the whole problem this that this time it takes approx 60 seconds for the grid to do the 2000 lines.
What is this depending on. A guess is that the backgroundworker must have something to do with this because I have tried to manually pressed the button1 2 times with another test and then it only took 30 seconds on both turns.
Also to mention is that something has to do with that I put colors to the cell because if I take that lines that do that away it will take 30 seconds.

[B]private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) [/B]
{
			 
dataGridView1->Rows->Clear();
for( int i = 0; i < 2000; i++ )
{
	 dataGridView1->Rows->Add();
	 for( int s = 0; s < 5000000; s++ )
	 {}

	dataGridView1->Rows[i]->Cells[2]->Value = Math::Round((1), 2); 

if((1) < 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = Color::AliceBlue;}
if((1) == 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = Color::Aquamarine;}
if((1) > 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = 
Color::Beige;}				
}


if( Workerflag == 0 && dataGridView1->Rows[0]->Cells[2]->Value != nullptr )
{
	backgroundWorker1->RunWorkerAsync();//Execute _DoWork
	Workerflag = 1; //Global Variable
}
}




[B]private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) [/B]
{		
	 Thread::Sleep(60000);
	 button1_Click(this, EventArgs::Empty); //This will activate button1	 
}

One more clue that I have found is that my processor is peeking up to 70-80% when the button1 activates by the backgroundworker.

First time button1 doing its code, the processor is about 50%.
Still I wonder and cant figure it out why these lines doing the process slower,
from 30 seconds to 60 seconds (double time).
The slower process is depending on just these lines, because if I take them away it is still 30 seconds as it should be.

if((1) < 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = Color::AliceBlue;}
if((1) == 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = Color::Aquamarine;}
if((1) > 0){dataGridView1->Rows[i]->Cells[2]->Style->ForeColor = 
Color::Beige;}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.