How does a C # GUI class get periodic updates from a working class?

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
{
    ...
    /*  Assume in the area we have instantiated these items and placed them on the form:
    *   Button  DoSomething:  A button to do something
    *   TextBox ShowInfo:  A text box to report something from the worker 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
{
    ...//Various Setup stuff up here

    void a_lengthy task()
    {
        int iteration = 0;

        while(iteration < 10)
        {
            Datetime saveNOW = Datetime.Now;        //lets say I report this back to the the GUI to write in that ShowInfo box
            Thread.sleep(10000);                    //To waste time and make this lengthy

            //Your code here to facilitate sending saveNOW back to the the Main_Class and display it on the ShowInfo textbox.

            iteration++
        }
    }
}
+3
source share
4

. , BackgroundWorker, , , .

IterationComplete , . EventArgs ( , EventArgs), , , . ( , ).

, .

:

:

public class MainClass
{
    Worker_Class timewaster = new Worker_Class();


    private void buttonDoSomething_Click(object sender, EventArgs e)
    {
        timewaster.IterationComplete += new EventHandler<Worker_Class.IterationEventArgs>(timewaster_IterationComplete);
        timewaster.LengthyTask();
    }

    void timewaster_IterationComplete(object sender, Worker_Class.IterationEventArgs e)
    {
        string infoFromWorker = e.iterationNumber;
    }
}

:

public class Worker_Class
{
    //Various Setup stuff up here

    public class IterationEventArgs : EventArgs
    {
        public string iterationNumber { get; set; }
    }

    public event EventHandler<IterationEventArgs> IterationComplete;

    public void LengthyTask()
    {
        int iteration = 0;

        while (iteration < 10)
        {
            DateTime saveNOW = DateTime.Now;        //lets say I report this back to the the GUI to write in that ShowInfo box
            Thread.Sleep(10000);                    //To waste time and make this lengthy

            //Your code here to facilitate sending saveNOW back to the the Main_Class and display it on the ShowInfo textbox.

            if (IterationComplete != null)
            {
                IterationEventArgs args = new IterationEventArgs();
                args.iterationNumber = iteration.ToString();
                IterationComplete(this, args);
            }

            iteration++;
        }
    }
}
+3

BackgroundWorker. , , .

BackgroundWorker onCompleted onProgressUpdated. , winform/wpf. , ReportProgress() DoWork() BackgroundWorker

. , , . BackgroundWorker .

MSDN - http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx

+1

, Worker_Class . Main_Class . . Control.Invoke , . . System.Windows.Forms.Timer DispatcherTimer. .

class YourForm : Form
{
  private volatile MyEventArgs progress = null;

  private void buttonDoSomething_Click(object sender, EventArgs args)
  {
    var timewaster = new Worker_Class();
    timewaster.ProgressChanged += (sender, args) { progress = args; };
    Task.Factory.StartNew(
      () =>
      {
        timewaster.a_lengthy_task();
      }
    UpdateTimer.Enabled = true;
  }

  private void UpdateTimer_Tick(object sender, EventArgs args)
  {
    if (progress != null)
    {
      if (progress.IsFinished)
      {
        ShowInfo.Text = "Finished";
        UpdateTimer.Enabled = false;
      }
      else
      {
        ShowInfo.Text = progress.SaveNow.Value.ToString();
      }
    }
    else
    {
      ShowInfo.Text = "No progress yet";
    }
  }
}

class Worker_Class
{
  public event EventHandler<MyEventArgs> ProgressChanged;

  public Worker_Class()
  {
    ProgressChanged += (sender, args) => { };
  }

  public void a_lengthy task()
  {
    int iteration = 0;
    while(iteration < 10)
    {
        Datetime saveNOW = Datetime.Now;
        Thread.sleep(10000);
        ProgressChanged(this, new MyEventArgs { SaveNow = saveNOW, IsFinished = false });
        iteration++
    }
    ProgressChanged(this, new MyEventArgs { SaveNow = null, IsFinished = true });
  }
}

class MyEventArgs : EventArgs
{
  public DateTime? SaveNow { get; set; }
  public bool IsFinished { get; set; }
}
+1

:

this.Invoke((MethodInvoker)delegate
{
    //do something in GUI
});

, ,

label.Text = "whatever";

.

0
source

All Articles