AutoComplete Texbox error - write to protected memory

前端 未结 2 1769
情书的邮戳
情书的邮戳 2021-01-22 07:00

I have an autocompleate textbox that looks into a data base. Some times while I\'m typing I received the following error.

Attempted to read or write protected memory

相关标签:
2条回答
  • 2021-01-22 07:19

    You can use lock:

    private void tBSearchName_TextChanged(object sender, EventArgs e)
    {
        lock(this) { /* do magic */
    }
    

    Do note that it's bad practice to perform long tasks in the event handlers. If the search takes more then 30ms, better use a worker thread.

    0 讨论(0)
  • 2021-01-22 07:39

    It is a bug in Windows Forms's wrapper of autocomplete APIs. Windows Forms does not protect the AutoCompleteCustomSource object from being replaced while it is being enumerated by a background thread created by autocomplete.

    Instead of replacing the data store, you can try replace the autocomplete object or use the IAutoCompleteDropDown interface to reset the enumerator.

    0 讨论(0)
提交回复
热议问题