CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2018
    Posts
    12

    Problem with autosave function?

    I get the following error with this code below this text.
    Message=Cross-thread operation not valid: Control 'richTextBox1' accessed from a thread other than the thread it was created on.
    Can someone help me?


    HTML Code:
    private void OnTimeEvent(object sender, System.Timers.ElapsedEventArgs e) 
                 {
                     if (saved == true)
                     {
                         saved = false;
                         richTextBoxInfo.Invoke((MethodInvoker)(() => richTextBoxInfo.Text = richTextBoxInfo.Text));
                         File.WriteAllText(@"wininfo.txt", richTextBoxInfo.Text);
                         timerSave.Stop();
                         MessageBox.Show("Saved!");
                     }
                 }

  2. #2
    Join Date
    Jun 2021
    Posts
    10

    Re: Problem with autosave function?

    Hello, you should try this code to autosave
    HTML Code:
    < div >   
        @Html.HiddenFor(model = > model.MyNoteViewModels.ICTMyNoteId, new {@id = "ID"})  
        @Html.TextAreaFor(model = > model.NoteViewModels.NoteItemText, new {@id = "Text", @onkeyup = "autosave()"})   
    < /div>   
       
    <script>   
        var autosave_timer = null;  
        function save() {  
            $.ajax({  
                type: "POST",  
                url: '@Url.Action("SaveNotes", "Note")',  
                data: { nID: $('#ID').val() || 0, nText: $('#Text').val() },  
                success: function (data) {  
                    console.log("Notes saved");  
                }  
            })  
        }  
        function autosave() {  
            if (autosave_timer)  
            clearTimeout(autosave_timer);  
            autosave_timer = setTimeout(save, 5000);  
        }  
       
    </script >
    HTML Code:
    public ActionResult SaveNotes(int nID, string nText)  
    {  
       MyNoteViewModel model = new MyNoteViewModel();  
       model.Id = nID;  
       model.Text = nText;  
       bool results = false;  
       results = _myNMgr.SaveNoteText(model);  
       return View(model);  
    }
    If you have any query you can check these resources
    c-sharpcorner

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured