c# - How to fix auto-suggest? - Stack Overflow

I made a program in c# where I type a ID and it gets those ID in my mongoDB database, the problem is wh

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

相关推荐

  • c# - How to fix auto-suggest? - Stack Overflow

    I made a program in c# where I type a ID and it gets those ID in my mongoDB database, the problem is wh

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信