I made a program in c# where I type a ID and it gets those ID in my mongoDB database, the problem is when I type too fast it crashes and says:
System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
I figured it may be because I'm running too much asynchronous tasks when i type too fast but I don't know how to fix it, here is my code:
private void textBox2_TextChanged(object sender, EventArgs e)
{
// Fetch and update the AutoComplete suggestions dynamically
string searchTerm = textBoxStudNum.Text.Trim();
if (!string.IsNullOrWhiteSpace(searchTerm))
{
var filter = Builders<StudentRecord>.Filter.Regex(s => s.studentNumber, new BsonRegularExpression(searchTerm, "i"));
var projection = Builders<StudentRecord>.Projection
.Include(s => s.studentNumber)
.Include(s => s.firstName)
.Include(s => s.lastName)
.Include(s => s.middleName)
.Include(s => s.college)
.Include(s => s.year);
var studentRecords = _studentRecordCollection.Find(filter).Project<StudentRecord>(projection).Limit(10).ToList();
_suggestedStudentNumbers = studentRecords.Select(s => s.studentNumber).ToList(); // Store suggestions
var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(_suggestedStudentNumbers.ToArray());
textBoxStudNum.AutoCompleteCustomSource = autoComplete;
textBoxStudNum.AutoCompleteMode = AutoCompleteMode.Suggest;
textBoxStudNum.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
else
{
_suggestedStudentNumbers.Clear(); // Clear suggestions
textBoxStudNum.AutoCompleteCustomSource = null;
}
}
I tried putting cancellation tokens to try and remove my previous tasks but the problem is the suggestions are not showing up anymore.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745309206a4621899.html
评论列表(0条)