none
multithreading a nested recursion method and change label.content without crashing RRS feed

  • Question

  • i cant get this to work right. i have tried to use timer, threads and threadpool to make this work but it just wont. what i am trying is to loop it with a half second pause but i got 2 issues the first one is that i cant get it multithead so it will run on another thread i dont want to use timers because i want it to run on a completely other thread and i cant get to change my int to string and when i do everytime i try to label.content = intconvertedtostring; it crashed every single time.

    heres some code:

    void devOperations()
                {
                    
                    DevOperations.Content = cIntToString(5);
                    Thread.Sleep(500);
                    //iOperations++;

                }

    Monday, October 9, 2017 6:47 PM

Answers

  • Hello Fonky44,

    >>when i do everytime i try to label.content = intconvertedtostring; it crashed every single time.

    For UI applications, there is only one thread is allowed to access UI control members. when you attempt to access control from different thread than UI will cause cross-thread exception. If you are using windows form. you should take solution of using "Invoke or BegainInvoke" method.

    public partial class Form5 : Form { public Form5() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(method); t.Start(); } private void method() { int i = 0; while (true) { label1.BeginInvoke(new MethodInvoker(delegate { label1.Text = i.ToString(); i++; })); Thread.Sleep(1000); } }

    }

    Running result

     

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Fonky44 Tuesday, October 10, 2017 2:24 PM
    Tuesday, October 10, 2017 8:30 AM
    Moderator
  • Is DevOperations a WPF control?

    If so, then the WPF equivalent of BeginInvoke is Dispatcher.BeginInvoke. Otherwise, its pretty similar to neil hu's example:

    Dispatcher.BeginInvoke((Action)(() => {
                DevOperations.Content = "Some text";
    }));

    • Marked as answer by Fonky44 Tuesday, October 10, 2017 2:24 PM
    Tuesday, October 10, 2017 1:48 PM

All replies

  • Hello Fonky44,

    >>when i do everytime i try to label.content = intconvertedtostring; it crashed every single time.

    For UI applications, there is only one thread is allowed to access UI control members. when you attempt to access control from different thread than UI will cause cross-thread exception. If you are using windows form. you should take solution of using "Invoke or BegainInvoke" method.

    public partial class Form5 : Form { public Form5() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(method); t.Start(); } private void method() { int i = 0; while (true) { label1.BeginInvoke(new MethodInvoker(delegate { label1.Text = i.ToString(); i++; })); Thread.Sleep(1000); } }

    }

    Running result

     

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Fonky44 Tuesday, October 10, 2017 2:24 PM
    Tuesday, October 10, 2017 8:30 AM
    Moderator
  • I can't call BeginInvoke from my method
    Tuesday, October 10, 2017 1:40 PM
  • Is DevOperations a WPF control?

    If so, then the WPF equivalent of BeginInvoke is Dispatcher.BeginInvoke. Otherwise, its pretty similar to neil hu's example:

    Dispatcher.BeginInvoke((Action)(() => {
                DevOperations.Content = "Some text";
    }));

    • Marked as answer by Fonky44 Tuesday, October 10, 2017 2:24 PM
    Tuesday, October 10, 2017 1:48 PM
  • Yes it is a WPF control and this worked for me thank you very much

    Tuesday, October 10, 2017 2:24 PM