ThreadSafe CaretIndex- WinForm












2















I have a multiline Textbox that uses threadsafe method to call it.
After every new text, the caret ( cursor ) goes to first line position and for multiline texts. I can't read the lasts lines.



I try to use:



 textBox.CaretIndex = _textBox.Text.Length;


but it is not for threadsafe.



This is my code:



void test()
{
Thread demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
demoThread.Start();
}
private void ThreadProcSafe()
{
ThreadHelperClass.SetText(this, textBox2, "text: ");
}
public static class ThreadHelperClass{
delegate void SetTextCallback(Form f, Control ctrl, string text);
/// <summary>
/// Set text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="text"></param>
public static void SetText(Form form, Control ctrl, string text){
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (ctrl.InvokeRequired){
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new object { form, ctrl, text });
} else {
ctrl.Text += text;
ctrl.Text += Environment.NewLine;
}
}
}


ThreadHelperClass.SetText(this, richTextBox1, "output>>" + e.Data);



I want to go at end of the Textbox ( for see the last lines text ) without clicking or moving the mouse. After a new text, caret go to end of the line.



i want to use Setting cursor at the end of any text of a textbox at ThreadHelperClass but it gives me error



want to use



txtBox.Focus();
txtBox.SelectionStart = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.CaretIndex = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);


this is the error that I get when i using above codes in ThreadHelperClass
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.textBox2' Download C:UsersworksourcereposDownloadDownloadForm1.cs 108 Active



and when I use it outside of this function my application get crashed for multi thread










share|improve this question

























  • What is "threadsafe method"? Do you just mean .Invoke(...)?

    – Enigmativity
    Nov 25 '18 at 21:41











  • I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

    – Ragnaros
    Nov 25 '18 at 21:50













  • when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

    – Ragnaros
    Nov 25 '18 at 22:13













  • Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

    – Enigmativity
    Nov 25 '18 at 22:17
















2















I have a multiline Textbox that uses threadsafe method to call it.
After every new text, the caret ( cursor ) goes to first line position and for multiline texts. I can't read the lasts lines.



I try to use:



 textBox.CaretIndex = _textBox.Text.Length;


but it is not for threadsafe.



This is my code:



void test()
{
Thread demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
demoThread.Start();
}
private void ThreadProcSafe()
{
ThreadHelperClass.SetText(this, textBox2, "text: ");
}
public static class ThreadHelperClass{
delegate void SetTextCallback(Form f, Control ctrl, string text);
/// <summary>
/// Set text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="text"></param>
public static void SetText(Form form, Control ctrl, string text){
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (ctrl.InvokeRequired){
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new object { form, ctrl, text });
} else {
ctrl.Text += text;
ctrl.Text += Environment.NewLine;
}
}
}


ThreadHelperClass.SetText(this, richTextBox1, "output>>" + e.Data);



I want to go at end of the Textbox ( for see the last lines text ) without clicking or moving the mouse. After a new text, caret go to end of the line.



i want to use Setting cursor at the end of any text of a textbox at ThreadHelperClass but it gives me error



want to use



txtBox.Focus();
txtBox.SelectionStart = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.CaretIndex = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);


this is the error that I get when i using above codes in ThreadHelperClass
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.textBox2' Download C:UsersworksourcereposDownloadDownloadForm1.cs 108 Active



and when I use it outside of this function my application get crashed for multi thread










share|improve this question

























  • What is "threadsafe method"? Do you just mean .Invoke(...)?

    – Enigmativity
    Nov 25 '18 at 21:41











  • I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

    – Ragnaros
    Nov 25 '18 at 21:50













  • when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

    – Ragnaros
    Nov 25 '18 at 22:13













  • Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

    – Enigmativity
    Nov 25 '18 at 22:17














2












2








2








I have a multiline Textbox that uses threadsafe method to call it.
After every new text, the caret ( cursor ) goes to first line position and for multiline texts. I can't read the lasts lines.



I try to use:



 textBox.CaretIndex = _textBox.Text.Length;


but it is not for threadsafe.



This is my code:



void test()
{
Thread demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
demoThread.Start();
}
private void ThreadProcSafe()
{
ThreadHelperClass.SetText(this, textBox2, "text: ");
}
public static class ThreadHelperClass{
delegate void SetTextCallback(Form f, Control ctrl, string text);
/// <summary>
/// Set text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="text"></param>
public static void SetText(Form form, Control ctrl, string text){
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (ctrl.InvokeRequired){
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new object { form, ctrl, text });
} else {
ctrl.Text += text;
ctrl.Text += Environment.NewLine;
}
}
}


ThreadHelperClass.SetText(this, richTextBox1, "output>>" + e.Data);



I want to go at end of the Textbox ( for see the last lines text ) without clicking or moving the mouse. After a new text, caret go to end of the line.



i want to use Setting cursor at the end of any text of a textbox at ThreadHelperClass but it gives me error



want to use



txtBox.Focus();
txtBox.SelectionStart = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.CaretIndex = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);


this is the error that I get when i using above codes in ThreadHelperClass
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.textBox2' Download C:UsersworksourcereposDownloadDownloadForm1.cs 108 Active



and when I use it outside of this function my application get crashed for multi thread










share|improve this question
















I have a multiline Textbox that uses threadsafe method to call it.
After every new text, the caret ( cursor ) goes to first line position and for multiline texts. I can't read the lasts lines.



I try to use:



 textBox.CaretIndex = _textBox.Text.Length;


but it is not for threadsafe.



This is my code:



void test()
{
Thread demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));
demoThread.Start();
}
private void ThreadProcSafe()
{
ThreadHelperClass.SetText(this, textBox2, "text: ");
}
public static class ThreadHelperClass{
delegate void SetTextCallback(Form f, Control ctrl, string text);
/// <summary>
/// Set text property of various controls
/// </summary>
/// <param name="form">The calling form</param>
/// <param name="ctrl"></param>
/// <param name="text"></param>
public static void SetText(Form form, Control ctrl, string text){
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (ctrl.InvokeRequired){
SetTextCallback d = new SetTextCallback(SetText);
form.Invoke(d, new object { form, ctrl, text });
} else {
ctrl.Text += text;
ctrl.Text += Environment.NewLine;
}
}
}


ThreadHelperClass.SetText(this, richTextBox1, "output>>" + e.Data);



I want to go at end of the Textbox ( for see the last lines text ) without clicking or moving the mouse. After a new text, caret go to end of the line.



i want to use Setting cursor at the end of any text of a textbox at ThreadHelperClass but it gives me error



want to use



txtBox.Focus();
txtBox.SelectionStart = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.CaretIndex = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);


this is the error that I get when i using above codes in ThreadHelperClass
Severity Code Description Project File Line Suppression State
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.textBox2' Download C:UsersworksourcereposDownloadDownloadForm1.cs 108 Active



and when I use it outside of this function my application get crashed for multi thread







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 22:29







Ragnaros

















asked Nov 25 '18 at 20:15









RagnarosRagnaros

186




186













  • What is "threadsafe method"? Do you just mean .Invoke(...)?

    – Enigmativity
    Nov 25 '18 at 21:41











  • I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

    – Ragnaros
    Nov 25 '18 at 21:50













  • when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

    – Ragnaros
    Nov 25 '18 at 22:13













  • Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

    – Enigmativity
    Nov 25 '18 at 22:17



















  • What is "threadsafe method"? Do you just mean .Invoke(...)?

    – Enigmativity
    Nov 25 '18 at 21:41











  • I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

    – Ragnaros
    Nov 25 '18 at 21:50













  • when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

    – Ragnaros
    Nov 25 '18 at 22:13













  • Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

    – Enigmativity
    Nov 25 '18 at 22:17

















What is "threadsafe method"? Do you just mean .Invoke(...)?

– Enigmativity
Nov 25 '18 at 21:41





What is "threadsafe method"? Do you just mean .Invoke(...)?

– Enigmativity
Nov 25 '18 at 21:41













I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

– Ragnaros
Nov 25 '18 at 21:50







I want to use stackoverflow.com/questions/20423211/… inside ThreadHelperClass

– Ragnaros
Nov 25 '18 at 21:50















when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

– Ragnaros
Nov 25 '18 at 22:13







when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on.

– Ragnaros
Nov 25 '18 at 22:13















Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

– Enigmativity
Nov 25 '18 at 22:17





Is this winforms or WPF? You need to provide us with a Minimal, Complete, and Verifiable example.

– Enigmativity
Nov 25 '18 at 22:17












2 Answers
2






active

oldest

votes


















0














Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:



Application.Current.Dispatcher.Invoke(
() =>
{
textBox.CaretIndex = _textBox.Text.Length;
});





share|improve this answer
























  • no not work, anyway thanks

    – Ragnaros
    Nov 25 '18 at 21:53











  • Sorry, good luck

    – Moffen
    Nov 25 '18 at 21:54



















0














Problem Solved By using
Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on



delegate void SetTextCallback(string text);

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object { text });
}
else
{
this.textBox1.Text = text;
}
}


in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.



txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);


but in my Question Code there was a



Control ctrl


that didn't have Select, Focus
like to know the answer for old way too



Anyway Thanks






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471502%2fthreadsafe-caretindex-winform%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:



    Application.Current.Dispatcher.Invoke(
    () =>
    {
    textBox.CaretIndex = _textBox.Text.Length;
    });





    share|improve this answer
























    • no not work, anyway thanks

      – Ragnaros
      Nov 25 '18 at 21:53











    • Sorry, good luck

      – Moffen
      Nov 25 '18 at 21:54
















    0














    Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:



    Application.Current.Dispatcher.Invoke(
    () =>
    {
    textBox.CaretIndex = _textBox.Text.Length;
    });





    share|improve this answer
























    • no not work, anyway thanks

      – Ragnaros
      Nov 25 '18 at 21:53











    • Sorry, good luck

      – Moffen
      Nov 25 '18 at 21:54














    0












    0








    0







    Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:



    Application.Current.Dispatcher.Invoke(
    () =>
    {
    textBox.CaretIndex = _textBox.Text.Length;
    });





    share|improve this answer













    Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:



    Application.Current.Dispatcher.Invoke(
    () =>
    {
    textBox.CaretIndex = _textBox.Text.Length;
    });






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 20:50









    MoffenMoffen

    789218




    789218













    • no not work, anyway thanks

      – Ragnaros
      Nov 25 '18 at 21:53











    • Sorry, good luck

      – Moffen
      Nov 25 '18 at 21:54



















    • no not work, anyway thanks

      – Ragnaros
      Nov 25 '18 at 21:53











    • Sorry, good luck

      – Moffen
      Nov 25 '18 at 21:54

















    no not work, anyway thanks

    – Ragnaros
    Nov 25 '18 at 21:53





    no not work, anyway thanks

    – Ragnaros
    Nov 25 '18 at 21:53













    Sorry, good luck

    – Moffen
    Nov 25 '18 at 21:54





    Sorry, good luck

    – Moffen
    Nov 25 '18 at 21:54













    0














    Problem Solved By using
    Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on



    delegate void SetTextCallback(string text);

    private void SetText(string text)
    {
    // InvokeRequired required compares the thread ID of the
    // calling thread to the thread ID of the creating thread.
    // If these threads are different, it returns true.
    if (this.textBox1.InvokeRequired)
    {
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object { text });
    }
    else
    {
    this.textBox1.Text = text;
    }
    }


    in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.



    txtBox.Focus();
    txtBox.Select(txtBox.Text.Length, 0);


    but in my Question Code there was a



    Control ctrl


    that didn't have Select, Focus
    like to know the answer for old way too



    Anyway Thanks






    share|improve this answer




























      0














      Problem Solved By using
      Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on



      delegate void SetTextCallback(string text);

      private void SetText(string text)
      {
      // InvokeRequired required compares the thread ID of the
      // calling thread to the thread ID of the creating thread.
      // If these threads are different, it returns true.
      if (this.textBox1.InvokeRequired)
      {
      SetTextCallback d = new SetTextCallback(SetText);
      this.Invoke(d, new object { text });
      }
      else
      {
      this.textBox1.Text = text;
      }
      }


      in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.



      txtBox.Focus();
      txtBox.Select(txtBox.Text.Length, 0);


      but in my Question Code there was a



      Control ctrl


      that didn't have Select, Focus
      like to know the answer for old way too



      Anyway Thanks






      share|improve this answer


























        0












        0








        0







        Problem Solved By using
        Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on



        delegate void SetTextCallback(string text);

        private void SetText(string text)
        {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object { text });
        }
        else
        {
        this.textBox1.Text = text;
        }
        }


        in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.



        txtBox.Focus();
        txtBox.Select(txtBox.Text.Length, 0);


        but in my Question Code there was a



        Control ctrl


        that didn't have Select, Focus
        like to know the answer for old way too



        Anyway Thanks






        share|improve this answer













        Problem Solved By using
        Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on



        delegate void SetTextCallback(string text);

        private void SetText(string text)
        {
        // InvokeRequired required compares the thread ID of the
        // calling thread to the thread ID of the creating thread.
        // If these threads are different, it returns true.
        if (this.textBox1.InvokeRequired)
        {
        SetTextCallback d = new SetTextCallback(SetText);
        this.Invoke(d, new object { text });
        }
        else
        {
        this.textBox1.Text = text;
        }
        }


        in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.



        txtBox.Focus();
        txtBox.Select(txtBox.Text.Length, 0);


        but in my Question Code there was a



        Control ctrl


        that didn't have Select, Focus
        like to know the answer for old way too



        Anyway Thanks







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 22:28









        RagnarosRagnaros

        186




        186






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471502%2fthreadsafe-caretindex-winform%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            To store a contact into the json file from server.js file using a class in NodeJS

            Marschland