I have a wondering here. This code I have below is working great except of one thing that doesn´t make sense to me.
I use a while-loop inside a for-loop as seen.
The for-loop is counting from 1-49 and for each count the while-loop will read a .txt file and put some info to a file.
Notice now in my code in the for-loop that I have put "Display this" to a label on my form.

This should be shown for the first count in the foor-loop wich is 1 ?
What happens in this code is that "Display this" will be shown First when the count is ready, wich then is count = 49.
Why is this happening. It should be shown directly and not in the end of the loop ?

HowEver if I put this message box in the same place instead of "CurrentSymbol" then it will
Display a new messagebox for each count wich meens 49 MessageBoxes

MessageBox::Show("Display this");
std::string Line;
ofstream MainFile;
MainFile.open ("Main.txt");


	for (int count= 1; count < (50); count++)
	{ 
		
		CurrentSymbol->Text = "Display this";
				
	
		ifstream File ("DGD.txt");
		while	(    getline(File, Line, '\n')  )           		
		{
		
			MainFile << "Hello" << ',' << "Hello2" << '\n';

		}


	}

Dani AI

Generated

Short diagnosis and practical fixes (why the label updates only after the loop finishes)

The visual symptom you describe is a classic UI-thread block: the form cannot repaint while the same thread is busy doing file I/O in the loop. That is why saw the label update only after the loop ends, and why a modal MessageBox appears to “fix” it — MessageBox pumps the message loop so pending repaints run. was right to call out file/stream handling as something to verify, but the immediate repaint issue is about the UI thread, not the label API.

Quick options (fast, but with tradeoffs)

  • Force an immediate redraw after changing the label with the control’s Refresh method (Refresh calls Invalidate + Update). Invalidate alone only marks the control; it won’t repaint until the message loop runs.
  • Use Application::DoEvents to let pending messages process. This works for quick tests but can introduce reentrancy bugs and is not recommended for production.
    See the docs for Control.Refresh and Application.DoEvents for details.

Best practice (recommended)
Do the file reading/processing off the UI thread and report progress back to the UI. A BackgroundWorker (or Task) keeps the UI responsive and lets you update the label safely via progress callbacks or Invoke/BeginInvoke. Example (C++/CLI sketch) that reports the current filename back to the UI:

BackgroundWorker^ bg = gcnew BackgroundWorker();
bg->WorkerReportsProgress = true;
bg->DoWork += gcnew DoWorkEventHandler(this, &Form1::bg_DoWork);
bg->ProgressChanged += gcnew ProgressChangedEventHandler(this, &Form1::bg_ProgressChanged);
bg->RunWorkerAsync();

void bg_DoWork(Object^ sender, DoWorkEventArgs^)
{
    for (int i = 1; i <= 49; ++i)
    {
        String^ fname = String::Format("DGD_{0}.txt", i);
        // read/process file here (background thread)
        (safe_cast<BackgroundWorker^>(sender))->ReportProgress(0, fname);
    }
}

void bg_ProgressChanged(Object^ sender, ProgressChangedEventArgs^ e)
{
    labelStatus->Text = safe_cast<String^>(e->UserState);
}

Additional notes
If you ever open an ifstream once and reuse it, ensure you clear error bits and seek back to the start or reopen it each iteration. For a responsive UI and robust behavior, move long-running work off the UI thread and marshal only small UI updates back. See BackgroundWorker for the pattern.

Recommended Answers

All 3 Replies

Here's what I see should be occurring:
The outer loop runs 49 iterations. In each it:
puts the text "Display this" to the text of CurrentSymbol (which I don't know what happens there)
the file DGD.txt will be opened, the while loop then reads it line by line to the end. As each line is read, "Hello,Hello2\n" is written to the output file.

But wait, there's less. Whe the for loop executes second time, the input file hasn't been closed or cleared, so nothing gets read in. Since there's nothing to read, nothing more goes to the output file.
This whole loop can spin so fast, you may not see the "Display This" being updated 48 times.

What is it you're really trying to do?

This is just an simple example. The thing is that later the foor-loop will read 49 different files, with the different names. So I will catch up each of these name in the labelBox.

This meens that I visually can se on the form wich File that is readed for the specific moment. This is why I am doing this.

The strange thing is that if you exchange "CurrentSymbol" to the MessageBox then it will work, The messagebox will execute before the while-loop takes in for the first time but strange that CurrenSymbol dont behave in the same way. I dont know if this has anything with that I close the file because this happen before I actually beginning with the while-loop ?

CurrentSymbol->Text = "Display this";
			
MessageBox::Show("Display this");

Here's what I see should be occurring:
The outer loop runs 49 iterations. In each it:
puts the text "Display this" to the text of CurrentSymbol (which I don't know what happens there)
the file DGD.txt will be opened, the while loop then reads it line by line to the end. As each line is read, "Hello,Hello2\n" is written to the output file.

But wait, there's less. Whe the for loop executes second time, the input file hasn't been closed or cleared, so nothing gets read in. Since there's nothing to read, nothing more goes to the output file.
This whole loop can spin so fast, you may not see the "Display This" being updated 48 times.

What is it you're really trying to do?

I have trried to Invalidate() the CurrentSymbol wich is my label right after the "Display this" was put to the label but this didn´t work.
However I think I have to use that in a way togehter with somthing like SendMessage(WM_PAINT) but I am not sure if this is the way or how to do this though ?

std::string Line;
	ofstream MainFile;
	MainFile.open ("Main.txt");


	for (int out = 1; out < (10); out++)
	{ 
		
	CurrentSymbol->Text = "Display this";
	CurrentSymbol->Invalidate();		
	

	ifstream File ("DGD.txt");
		while	(    getline(File, Line, '\n')  )           		
		{
		
			MainFile << "Hello" << ',' << "Hello2" << '\n';

		}


	}
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.