“System.InvalidOperationException: The object is currently in use elsewhere” - how do I resolve this?

送分小仙女□ 提交于 2019-11-27 07:13:40

问题


I got this error when trying to update an image. It was a cross-thread update, but I used .Invoke(), so that shouldn't be the problem, should it.


回答1:


(Answering my own question, for others, and for future reference)

I think (not yet entirely sure) that this is because InvokeRequired will always return false if the control has not yet been loaded/shown. I have done a workaround which seems to work for the moment, which is to simple reference the handle of the associated control in its creator, like so:

var x = this.Handle; 

(See http://ikriv.com:8765/en/prog/info/dotnet/MysteriousHang.html - down? cached version)

(Related question: Boiler plate code replacement - is there anything bad about this code?)




回答2:


If the handle doesn't yet exist, you can force it by subclassing the control and calling CreateHandle; however, the bigger question is: why are you doing things with a form that hasn't been loaded? Personally I'd only start such an operation after Load.



来源:https://stackoverflow.com/questions/246058/system-invalidoperationexception-the-object-is-currently-in-use-elsewhere-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!