hi,
i put together this code to read a text file line by line, split the resulting string into the 4 values and send these values to four text-boxes. during each cycle of that while loop it's suppose to initialize the backgroundworker_dowork, which will read the values and use them(and start a long loop and stuff).

The first and obvious problem i have is that when i run this i get an error saying that this backgroundworker is already busy and can't handle another task.

The part that's supposed to read text works as far as i can tell from using breakpoints while debugging but i'm not sure if it behaves like i think it does. (only the last set of values appears on the form, i'm not sure if the rest ever get painted to the form, i know however that they are read from using a breakpoint where this happens)

- I would like some advice on how to get this thing to work. Every set of values should be displayed, the backgroundworker initated, when it finishes it's job the next values read, displayed and so on.

This is the first app in which i use the bgworker, so i dunno much about it yet. I think it has some way of reporting that it has finished it's work. Should i use that as a condition to move on with the while loop?

sorry if what i wrote didn't make sense in some places, pls lemme know so i can try to explain better. i'm using VS 2010 express, c++, not total beginner but not too much experience:P
thanks for any help you can give with this:)

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
	String^ path = "<path>\\My_file.txt";
	StreamReader^ sr = gcnew StreamReader( path );
	array<String ^> ^ values = gcnew array<String ^>(4);
	int i2=0;
	System:String^ Input= gcnew System::String(" ");// input;
	try
	{
	    while ((Input = sr->ReadLine()) != ".")//read until it reaches the . on the final row
	        {
			input_box->Text = Input; //the string from the file is displayed as is
			i2=i2+1; //this will count the rows that are read
			counter_box->Text = i2.ToString(); //
			//------------------------------------------
			int j2=0;
			int l2=0;
			int a2=Input->Length;
                        values[0] = ""; //these will hold the separated numbers
			values[1] = "";
			values[2] = "";
			values[3] = "";
        //this next loop will split the string into the 4 values

			for (l2 = 0; l2 < a2;  l2++)
			{
				if (Input[l2] == '\040') //check if a space is found
				{
				        j2++; // move on to the next word
					l2++; //skip over blank space
				}
				values[j2] += Input[l2]; //increment the current value with the next digit
			}

			text_1->Text = values[0]; //send the 4 values to the form
			text_2->Text = values[1];
			text_3->Text = values[2];
			text_4->Text = values[3];
			backgroundWorker2->RunWorkerAsync();//call the DoWork event
		}	
	}
	finally
	{
		delete sr;
	}
}
};

Umm I do not see any code for your worker.. it doesn't seem to have any work to do :S

Your program itself seems to be doing the work and then you call backgroundworker() which isn't shown here..
Backgroundworker is a thread.. where you give it work to do while the program does something else.. then the program gathers the work while the thread sleeps..
When the thread is done working, there is a command to tell the program it is finished and then it *joins* and sleeps.

Calling RunWorkerAsync when the work is already being done will result in *InvalidOperationException*

This Code would definitely get around that problem.. But it only allows you to run whatever is in the backgroundWorker_DoWork Function..

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
    backgroundWorker1->DoWork();
    //Program does other stuff here while backgrounder worker does other stuff.

}

private: System::Void backgroundWorker1_DoWork(System::Object^  sender, System::ComponentModel::DoWorkEventArgs^  e) {
                               //Does calculations and all the work here.                                 
                                 Thread::Sleep(1000);  //Thread must sleep or else u may find a huge increase in CPU usage.
			 }

For Running Worker Asynchronously, you definitely have to do something like this:
http://msdn.microsoft.com/en-us/library/waw3xexc.aspx

What happens is that they call runworkerasync() and it does the work specified.. then it reports when its completed or if its cancelled and then the thread sleeps or is given more work to do. RunWorkerAsync can be given more work where as DoWork() just does work that is already pre-defined.. thats the only differences.

Oh yeah and you might want to check out OnRunWorkerCompleted event.. that will get it to do other stuff when the work is finally completed.

Examples of differences:
RunWorkerAsync() = like a telephone because both users talk at the same time without interupting eachother.. and they both hear eachother at the same time if they talk at the same time.

DoWork() = Train that never stops.. just continues until it reaches its stop, you get off.. and it leaves and starts over if requested.

Uhh Iunno what I was thinking above.. the Code is right.. but I meant to say that there are no differences as Async only raises the DoWork Event.. I was thinking async versus sync..

Hate the stupid 30 min edit timing..

line 7, forgot a :

thanks for the replies.
yup i didn't post the dowork part because i already got it working in a previous version of the code with no reading from file stuff, just calling runworkerasync.
i'll try OnRunWorkerCompleted, that might make a difference, thanks for mentioning it.
But i still dunno why it doesn't seem to run the _DoWork even when it reads the first line of text, i checked if it ever gets to "runworkerasync" and it does, just seems to pass right over it, never enters backgroundworker_dowork at all.
corrected the missing :, thanks:P
i'm working on it now but any new hints could help:)

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.