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 1044375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:49:07+00:00 2026-05-16T15:49:07+00:00

I have a MainWindow (form1) and a class called Module In Module there is

  • 0

I have a MainWindow (form1) and a class called Module

In Module there is a method to create a new Backgroundworker and change a label in MainWindow. I have tried creating a public or internal method in the MainWindow code and calling it in the Module class but it doesn’t seem to work.

Can anyone help me figure this out, it’s just something which is stopping me from continuing development.

Sorry if I didn’t make things clear, if you need something cleared up let me know.

Module.cs

public class Module
{
    protected System.Diagnostics.PerformanceCounter cpuCounter;
    BackgroundWorker cpuUThread;
    private delegate void UIDelegate();
    MainWindow mn;

    public void runCPUUsage()
    {

        cpuUThread = new BackgroundWorker();
        cpuUThread.DoWork += new DoWorkEventHandler(cpuUThread_DoWork);
        cpuUThread.WorkerSupportsCancellation = true;
        cpuUThread.RunWorkerAsync();
        mn = new MainWindow();
    }

    void cpuUThread_DoWork(object sender, DoWorkEventArgs e)
    {
        cpuCounter = new System.Diagnostics.PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

        try
        {
            mn.changeCPUULabel(getCurrentCpuUsage().ToString());
        }
        catch (Exception ex)
        {

        }
    }

    public double getCurrentCpuUsage()
    {
        return Math.Round(cpuCounter.NextValue(), 0);
    }

    public void disposeCpuUsage()
    {
        cpuUThread.CancelAsync();
        cpuUThread.Dispose();
    }
}

MainWindow – Contains a label (labelCPUU)

internal void changeCPUULabel(string val)
    {
        Dispatcher.Invoke(new UIDelegate(delegate
            {
                this.labelCPUU.Content = val;
            }));
    }
public double getCurrentCpuUsage()
    {
       return mod.getCurrentCpuUsage();
    }
void activateCPUU(){ mod.runCPUUsage(); }
  • 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-05-16T15:49:08+00:00Added an answer on May 16, 2026 at 3:49 pm

    It’s a good idea not to couple the form to tightly with the module. One way to do that is to define an interface that the form implements, and have the module accept such an interface as a parameter. I’ll exemplify with some code. Let’s start with the interface:

    public interface IDataReceiver
    {
        void SetData(string data);
    }
    

    In my example I use a string for the data, but that can be whatever type you need to use. Then we implement this interface in the form:

    public partial class Form1 : Form, IDataReceiver
    {
    
    
        private void Button_Click(object sender, EventArgs e)
        {
            // create a module intstance, passing this form as parameter
            var module = new SomeModule(this);
            module.DoSomeWork();
        }
    
        public void SetData(string data)
        {
            // use the data that is received
            txtSomeTextBox.Text = data;
        }
    
        // the rest of the form code left out to keep it short
    }
    

    Finally the module with the BackgroundWorker:

    public class SomeModule
    {
        private BackgroundWorker _worker = new BackgroundWorker();
        private IDataReceiver _receiver;
        public SomeModule(IDataReceiver receiver)
        {
            _worker.DoWork += new DoWorkEventHandler(Worker_DoWork);
            _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
            _worker.ProgressChanged += new ProgressChangedEventHandler(Worker_ProgressChanged);
            _worker.WorkerReportsProgress = true;
            _receiver = receiver;
        }
    
        void Worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            _receiver.SetData(e.UserState.ToString());
        }
    
        public void DoSomeWork()
        {
            // start the worker
            _worker.RunWorkerAsync();
        }
    
        private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // call method to pass data to receiver
            _receiver.SetData(e.Result.ToString());
        }
    
        private void Worker_DoWork(object sender, DoWorkEventArgs e)
        {
            // do some work here
            // assign the resulting data to e.Result
            for (int i = 0; i < 10; i++)
            {
                _worker.ReportProgress(0, "some data " + i);
                Thread.Sleep(250);
            }
            e.Result = "Finished";
        }
    }
    

    This way the module is in no way depending on what your form class looks like (it is even unaware that it is talking to a form). In my sample I call _receiver.SetData from the RunWorkerCompleted event handler, but it could just as well be done from a ReportProgress event handler.

    Also note how the form is the “driving force” here. The module does not create the form, or take any initiatives of any kind. It is simply used by the form.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following dependency property in my MainWindow class (inherits from WPF's Window)
I have the following code: <Window x:Class=kkk.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350 Width=525> <Window.Resources> <Style
i have a window which is like this <Window x:Class=pharmacy_Concept.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=350
Suppose I have this class: class MyClass(object): def uiFunc(self, MainWindow): self.attr1 = foo self.attr2
I have piece of Code in Form that works: public class Form1 : System.Windows.Forms.Form
I have a class class Window: public QMainWindow { // ... private: Ui::MainWindow ui;
I have a class with INotifyPropertyChanged interface. There is a property with the name
I have a window that contains a label (player1). I also have a class
I have MainWindow with a button, under the button click event I want MainWindow
I have a MainWindow application I'm working on to learn C++ and Qt (C++

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.