euclid135
euclid135

Reputation: 1272

C# Form MultiThreading, Need a tricky solution

I have a problem about using multithreading in c#. I have a complex computing thing that must run on the main thread means it must run on the main window.

My program runs like this :

Problem: Since the long time complex computing is running on the main thread, the monitoring window will be hang and not responding. Is there any tricky way to solve this? Can i use different thread to run the monitoring window? the main form is okay for not responding, but not okay for monitoring window. Here i put some code that resembles my program, not my real program, but the flow is the same.

Main Form(main thread that will run the computing thing,and input parameter from user)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        compute c = new compute();
        c.computing();
    }
}

Graph Form (Monitoring Graph that will plot chart, needed to be real time)

    public graphic()
    {
        InitializeComponent();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    public void updateLabel(int count)
    {
        label1.Text = "Current i value:  " + count;
    }

Compute class (doing the computing thing)

class compute
{
    delegate void writeToForm(int i);
    graphic g = new graphic();

    public compute()
    {
        g.Show();
    }
    public void computing()
    {
       //THIS PART MUST RUN ON THE MAIN THREAD, DON'T MAKE IT ON THE OTHER THREAD
       //IT IS A MUST
        int count = 0;
        //THIS IS THE LONG TIME COMPUTING THING
        for (int i = 0; i < 100; i++)
        {
            count += i;
            updateLabel(count);
            Thread.Sleep(2000);
        }
    }

    private void updateLabel(int count)
    {
        if (g.InvokeRequired)
        {
            g.Invoke(new writeToForm(updateLabel), new object[] { count });
            return;
        }
        g.updateLabel(count);
    }
}

I can't do anything about the method must run in the main thread, i'm using library that prevent its method to be ran from other method, thanks..

Upvotes: 0

Views: 292

Answers (3)

crypted
crypted

Reputation: 10306

If it is must for you to perform computation in the main thread, modify your code as below

public void computing()
    {
       //THIS PART MUST RUN ON THE MAIN THREAD, DON'T MAKE IT ON THE OTHER THREAD
       //IT IS A MUST
        int count = 0;
        //THIS IS THE LONG TIME COMPUTING THING
        for (int i = 0; i < 100; i++)
        {
            count += i;
            updateLabel(count);
            Thread.Sleep(2000);
            Application.DoEvents(); //tell windows to process pending message.
        }
    }

Upvotes: 1

Connell
Connell

Reputation: 14411

A possible workaround would be to write your monitoring form in another program, which can be launched by your main program before the computing method is executed. Yes, your main form will still hang (but you say this isn't a problem?).

You could communicate with your main application, possibly by opening a local TCP connection (which can run on a different thread without a problem). This could send commands to the monitoring application (or any other connected TCP clients if you wish), which would receive these and use the Control.Invoke method to update the UI.

Upvotes: 0

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

You could try and call Application.DoEvents after updating the graph in the second window. This processes all window messages, which should allow your second window to be updated.

Upvotes: 0

Related Questions