-
June 8th, 2021, 06:13 AM
#1
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!");
}
}
-
June 25th, 2021, 02:06 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|