Liszt 23 Junior Poster

I will try to catch a few idéas of how to check changes in a large number of files.
I dont know what oppurtunities there is to do that.
I will have a folder that contains perheps thousands of files wich will be written to randomly.

How is it possible to in realtime catch wich file that was Just written to ?
And is this possible to do without putting it to a loop that will peek the processor like this example ?

for( int i = 0; i < 5; i++)
{
   i = i - 1;
   GetLastWriteTime(LoopAllFilesHere Infinitely);
}
Liszt 23 Junior Poster

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 && …
Liszt 23 Junior Poster

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);
Liszt 23 Junior Poster

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;
Liszt 23 Junior Poster

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++ ) …
Liszt 23 Junior Poster

When writing a timeformat in a textbox using the event _TextChanged and when timeformat has reached the written format "HH:MM", the ->RunWorkerAsync() command
should execute and create a file but it seems that the backgroundworker does not execute the code inside.
I wonder why this is not happening, have I missed any part of the process to make that work ?

private: System::Void maskedTextBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) 
{

if( maskedTextBox1->Text->Length == 5 )//When time has format "HH:MM"
{
	 //Put present Time to global variables
	 time_t now = time(0);
	 struct tm* tm = localtime(&now);

	 PresentHour = tm->tm_hour;
	 PresentMinute = tm->tm_min;
	 /*........................................*/
			
	 backgroundWorker1->RunWorkerAsync();//Execute _DoWork
}
		 
}



private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) 
{
	
	ofstream pf;
	pf.open("C:\\createdfile.txt");
	pf << "abcdefgh";
	pf.close();

}
Liszt 23 Junior Poster

Solution:

int Day;
int Minute;

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

Day = tm->tm_wday;
Minute = tm->tm_min;


	 Thread::Sleep(240000); //4 Minutes Sleep or other calculated interval

	if( Day == 6 && Minute >= 15 )
	{
		this->Close();
         }
Liszt 23 Junior Poster

Thanks all for the replies :) I think I find the difftime() interesting to check.
I will check this out now.
I found a SleepFunction under Thread :: Sleep();

Liszt 23 Junior Poster

With the code below I want to execute a task if the day is saturday and if the minute is >= 14.
There is 2 things I wonder here.
The code below is an infinite loop that never stops. So it checks for if this criteria is true but when the criteria is true nothing happens.

And more important. This cant be the way to do this because the processor is peeking all the time since it is a loop that is running.
How is it possible to check for this criteria without letting the processor "work"/peek ?

int Day;
int Minute;

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

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

	Day = tm->tm_wday;
	Minute = tm->tm_min;

		if( Day == 6 && Minute >= 14 )
		{
			//Execute task
		}
}

Check out this related thread

Liszt 23 Junior Poster

Now I have tried to put the formopening in another void and are calling this when opening
the form but with the same error. 'DragDrop registration did not succeed'

button2_Click(this, EventArgs::Empty);
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
	  Form2 ^form2 = gcnew Form2;
	  form2->Show();
}

Any idéas of what I should focus on to do here.
I think I am pulling my hair by now :P Thanks a Lot!

Liszt 23 Junior Poster

I am not sure if you could meen something like this if you know the path where the bitmap is... Hope it helps

System::IO::File::Delete("C:\\TempImage2.bmp");

I want to delete a bitmap. How do I do this in c++.

DestBitmap->Save("TempImage2.bmp");
.
.
.
.
.
//later something like
Delete("TempImage2.bmp");

is there a way to do this.

thanks,
nemo

Liszt 23 Junior Poster

I have found something about the problem that I receives in the above post, i think that this is the solution or a part of the solution but my problem is that i dont understand what this code is doing or where i will put all the code.
I have set AllowDrop to false.
As what is defined as function A, B and Formfunction in this example and as a sentence say
"Put all the code ( for which u r getting this exception) in one function say function A".

What code is that, I dont understand what functions "is" in this case ?

1) include the following
#include System::Net;
#include System::Threading;

2) add the [STAThread] Attribute just before ur main function and also remember to #include System::Net; in ur main program,

[STAThread]
static void Main()

3) The steps are as follows for threading,

1. Put all the code ( for which u r getting this exception) in one function say function A. In the function B in which u are calling ur code,do the following,
function B
{
Thread t = gcnew Thread(gcnew ThreadStart(A));
t.SetApartmentState(ApartmentState->STA);
t.Start();

}
function A
{
the code of Windows form..or whatever which is causing the error
}


Explanation:
It turns out that WF when executing all of its pretty pictures creates a thread to run each of those pictures with. These threads are in …

Liszt 23 Junior Poster

Perheps while you read the file you can use another stringvariable like:

string Addd1d2;

Addd1d2 = data1 + " " + data2;

example of my txt file : hi hi hi hi
data1 will be hi
data2 will be hi
data3 will be hi
data4 will be hi
the data seperated by the spaces
but i want my data1 to be "hi hi" instead of only "hi"

Liszt 23 Junior Poster

I have encountered a problem when using the backgroundworker. What I do is that I will have the oppurtunity to press the cancelbutton to stop the backgroundworker.
The code that I am using below works without any problem as expected. The thing that happens is that an error message occurs before the form2 is opened and the message is this.
-------------------------------------------
'DragDrop registration did not succeed'
System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be made.
Ensure that your Main function has STAThreadAttribute marked on it.
-------------------------------------------

I have searched all over google after this message and found that this code can be used to put something in a separate thread.

Thread::CurrentThread->SetApartmentState(ApartmentState:: STA);

The problem is that I have put this code in all Load events, drag_drop events, exactly before opening form2. Before all timeconsuming code is exectuting with no result.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
	backgroundWorker1->RunWorkerAsync();
}


private: System::Void ButtonCanc_Click(System::Object^  /*sender*/, System::EventArgs^  /*e*/) 
{
	 this->backgroundWorker1->CancelAsync();
	 cancelpress = true;		 
}

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) 
{
	//TimeConsuming Work will execute as long as cancelpress is false

	Form2 ^form2 = gcnew Form2;
	form2->Show();
}
Liszt 23 Junior Poster

How is it possbile to return the current hour, minute and DayOfWeek.
I have tried out these examples. The two first returns 0 wich is strange.
And the third does not go through the compiler. What can I be missing ?

System::DateTime moment = System::DateTime();
int hour = moment.Hour; //returns 0 ?

System::DateTime moment = System::DateTime();
int minute = moment.Minute; //returns 0 ?

String^ day =  DateTime::ToString(DateTime::DayOfWeek);
//Does not compile ?
Liszt 23 Junior Poster

A struct can look like this. You will call the struct for sorting in this case, mysort1 as a third argument in a sort, so you pass one a1 and a2 to the struct.

struct mysort1
  {
  bool operator () ( const std::string& a1, const std::string& b1 )
    {
    std::stringstream c1( a1 );
    std::stringstream c2( b1 );
    double d1, d2;
    c1 >> d1;
    c2 >> d2;
    return d1 < d2;
    }
  };
Liszt 23 Junior Poster

i found out that events could be used so SizeChanged and some mathematics did it.

Liszt 23 Junior Poster

I have a little problem to dock a grid with a panel.
What i have done is to first dock a panel in the top of the form. What i then want to do is to dock a datagrid so it Fills the whole form but not so it will go inunder the panel that is docked at the top of form(I have attached a picture how it looks like).
What i try to achieve is when the form will be maximized the datagrid will also be bigger and the columnheaders will be just under the panel as now and then also fill out in the sides and bottom of form.
If i dock the datagrid with Fill, the columnheaders will come inunder the panel wich i dont want.
Thanks in advance!

Liszt 23 Junior Poster

I think I got something out of this and trying around now with different testings.
Thank you for your effort and help!

Liszt 23 Junior Poster

thank you emotionalone, i wrote an idea on your question there. i have searched around a little where I found this link:
http://www.usenet-forums.com/php-language/15497-how-do-i-read-file-line-line-backwards.html

what I found here was a message
"why not just open the file, seek to the end, read byte by byte backwords, untill you find a \n then what you have read in is a line,"

As i have understand a \n tells that the end of line is reached and perheps here something can be done to save that information as a line in a string.

This will find the end of file to but from here i dont know how to continue to read.

sr->BaseStream->Seek(0, SeekOrigin::End);

I've worked a little with files at school and from what I've gathered, to actually go to the end of the file you need to know how far that is from the beginning of the file either before hand or calculating it. This is not a problem with bit files but when it comes to .txt's, you just don't know how many bits will one line take, it varies.

So you might want to go through the entire file and count the number of times you encounter a line feed. and then work with that information, maybe read (n-1) lines and then read the last sentence and then go back the sizeof(last read) * -1; or something like that, just a vague idea. I actually need help with txt's too. Read this …

Liszt 23 Junior Poster

Wonder if it could be possible for you to use ifstream and getline to get the lines and then use this line to put to you output file. This should read the file to the end.

ifstream PS("SOURCE.TXT");
ofstream pD;
pD.open("DEST.TXT");
string line;

while( getline(PS, line) )
{
	pD << line << '\n';
}
pD.close();
Liszt 23 Junior Poster

im quite new to code and are using c++ to read files. Through some examples
i have managed to read a file from top to bottom like this.
The problem is that i will need to read the file from bottom to top instead.
Is there a reversable method for this.
I know I could use vectors to reverse the information but the files are very large.
Any help would be appreciated.

StreamReader^ sr = gcnew StreamReader("TestFile.txt");
String^ line;

while (sr->Peek() >= 0)
{
	line = sr->ReadLine();
}