0

I work on an application in c# and I have a problem with my thread and my UI...

I want to add +1 on my label when the thread is running. The problem is I don't know how I can fix that... I have read a lot of "how to" but the solutions donc work with my app..

My thread class:

 class clsWorker
    {
        //Thread myThread = new Thread(new ThreadStart(ThreadLoop));

        public SerialPort port;
        public String url;
        Thread t;
        clsSMS clsobjSMS = new clsSMS();
        SMSapplication clsobjAPP = new SMSapplication();

        public clsWorker(SerialPort serialPort, String urlChamp)
        {
            this.port = serialPort;
            this.url = urlChamp;
        }


        public void StartThread()
        {   
            t = new Thread(new ThreadStart(ThreadLoop));
            t.Start();
        }


        public void ThreadLoop()
        {
             // How I can add +1 on the countSMSok label ??
             clsobjAPP.updateCountSMS("countSMSok");

        }
    }

My application class :

public partial class SMSapplication : Form
    {
public void updateCountSMS(String label)
    {
        int num;

            this.countSMSnok = new System.Windows.Forms.Label();
            this.countSMSok = new System.Windows.Forms.Label();

            this.Controls.Add(this.countSMSnok );
            this.Controls.Add(this.countSMSok );


         if (label == this.countSMSok.Name.ToString())
        {
           if (int.TryParse(this.countSMSok.Text.ToString(), out num))
                this.countSMSok.Invoke((MethodInvoker)(() => this.countSMSok.Text = num++.ToString()));

        }
        else if (label == this.countSMSnok.Name.ToString())
        {
            if (int.TryParse(this.countSMSnok.Text.ToString(), out num))
                this.countSMSnok.Invoke((MethodInvoker)(() => this.countSMSnok.Text = num++.ToString()));
        }  
    }

       private void btnRequestStart_Click(object sender, EventArgs e)
    {
        this.btnRequestStart.Enabled = false;
        this.btnRequestStop.Enabled = true;
        objclsWorker = new clsWorker(this.port, this.urlChecker.Text);
        objclsWorker.StartThread();
    }

}

Thanks you very very much for your helping !

Cœur
  • 37,241
  • 25
  • 195
  • 267
Geoffrey
  • 5
  • 5

2 Answers2

0

Make sure you create an instance of the label and add the label to the Controls

    this.countSMSnok = new System.Windows.Forms.Label();
    this.countSMSok = new System.Windows.Forms.Label();

    this.Controls.Add(this.countSMSnok );
    this.Controls.Add(this.countSMSok );
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
  • Nice jordanhill123 ! Now the application don't bug, but my label are not actualized... Si the edit please :) – Geoffrey Mar 16 '13 at 11:28
0

You can not initialize new object of SMSapplication class, update it and expect it to update your first form. Those are different 'things'. Also You should use SynchronizationContext instead of invoke.

Here's working code:

Form:

public partial class SMSapplication : Form
{
    private SynchronizationContext context = null;
    private SerialPort port;

    public SMSapplication()
    {
        InitializeComponent();
        this.countSMSok.Text = "0";
        this.context = WindowsFormsSynchronizationContext.Current;
    }

    public void updateCountSMS(String label)
    {
        this.context.Post(new SendOrPostCallback(updateCountSMSSync), label);
    }

    private void updateCountSMSSync(object o)
    {
        string label = o as string;
        int num;

        if (label == this.countSMSok.Name.ToString())
        {
            if (int.TryParse(this.countSMSok.Text.ToString(), out num))
            {
                this.countSMSok.Text = (++num).ToString();
            }
        }
    }

    private void btnRequestStart_Click(object sender, EventArgs e)
    {
        clsWorker objclsWorker = new clsWorker(this, this.port, this.urlChecker.Text);
        objclsWorker.StartThread();
    }
}

And worker:

class clsWorker
{
    public SerialPort port;
    public String url;
    SMSapplication clsobjAPP = null;

    public clsWorker(SMSapplication app, SerialPort serialPort, String urlChamp)
    {
        this.clsobjAPP = app;
        this.port = serialPort;
        this.url = urlChamp;
    }

    public void StartThread()
    {
        new Thread(new ThreadStart(ThreadLoop)).Start();
    }


    public void ThreadLoop()
    {
        clsobjAPP.updateCountSMS("countSMSok");
    }
}
Grzegorz W
  • 3,487
  • 1
  • 21
  • 21
  • Hooooo my good, you are a good ! That's work perfectly! I have understand my stupidity. I created an new instanciation... Thanks to you, I will sleep less stupid :) !! Thanks you +++ – Geoffrey Mar 16 '13 at 12:11