I am trying to separate my working code from my GUI code to a completely different class, but I want to be able to report my GUI to update and exit the file. For example, I want my GUI to tell the working class: "Read ten lines from the serial port, but tell me everything you read when you receive it." Currently, my best way to do this is to have a GUI loop ten times and in each loop send a command to the working class to read one thing and return it.
I would prefer to keep the whole cycle on the side of my working class, since it will have more information about what is available (the actual amount of data will be variable, and the working class already has access to the amount of data available, and I would prefer not to send it back into the GUI class to start the loop itself.
I looked at the backgroundworker, but it seems that only the percentage of the report made during a long operation, and nothing else, so that would not help me here. Does anyone have a good idea how can I do this?
The following is the shell of the program to try (hopefully) better illustrate what I want to do. How would you edit the code to do what I need?
The main GUI class:
class Main_Class
{
...
Worker_Class timewaster = new Worker_Class();
private void buttonDoSomething_Click(object sender, EventArgs e)
{
timewaster.a_lengthy_task();
}
}
Separate working class:
class Worker_Class
{
...
void a_lengthy task()
{
int iteration = 0;
while(iteration < 10)
{
Datetime saveNOW = Datetime.Now;
Thread.sleep(10000);
iteration++
}
}
}
source
share