Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:02:50+00:00 2026-06-04T16:02:50+00:00

I am trying to separate my worker code from my GUI code into an

  • 0

I am trying to separate my worker code from my GUI code into an entirely different class, but I want to be able to report back to my GUI for progress update and file output. For example, I want to have my GUI say to the worker class, “Read ten lines from the serial port, but report to me each thing you read as you receive it”. Currently my best way to do it is to have the GUI loop ten times and in each loop send a command to the worker class to read one thing and return it.

I would really prefer to keep all of the looping on the side of my worker class, since it will have more information of what is available (the actual amount of data will be variable, and the worker class already has access to the amount of data available, and I would prefer not to send this back to the GUI class to run the loop itself.

I have looked into backgroundworker but that seems to only report percentage done during a lengthy operation, and nothing else, so that would not help me much here. Does anybody have a good idea how I can accomplish this?

Below is a shell of a program to try to (hopefully) better illustrate what I want to do. How would you edit the code to do what I require?

The GUI’s main 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();
    }
}

The separate worker 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++
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:02:51+00:00Added an answer on June 4, 2026 at 4:02 pm

    What you need is events. If you can’t work around what’s built into the BackgroundWorker class to get it to work for you, you can at least model your solution after what it does.

    The worker class should have a public event IterationComplete or whatever you want to call it. You can give it an EventArgs object (or an object that extends EventArgs) that contains the information relevant to that iteration that the UI will need. The event can be fired after each iteration is complete (or whenever the UI modifications should take place).

    The UI can then subscribe to the event for all UI tasks related to that iteration.

    Code sample:

    Main class:

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

    Worker class:

    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++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to basically trying to separate a specific amount of text from the
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I'm trying to write a library to separate all the disk activity out into
I'm trying to combine two separate javascript functions' outputs but each function is on
I am trying to populate a ListView in a separate class with data taken
I'm trying to convert some PHP code from Mysqli to PDO, and I need
we're trying to sort out some conventions for handling dependencies with code checked into
I'm trying to optimize my code to be called from both an UI-less commandline
I have been trying to separate a pList that I have already alphabetized into
I am trying to separate my logic for better readability and re-useability. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.