c# excel beforesave thread

末鹿安然 提交于 2019-12-07 03:00:30

Interacting with Excel through multiple threads is a bit tricky -- here is an MSDN entry on it. This is because you interact with Excel through COM, which uses thread apartments; .NET, however, does not. In particular, Excel uses Single-Treaded Apartment (STA), which means that all new threads that are created for interacting with Excel must be set to STA state:

thread.SetApartmentState(ApartmentState.STA);

before interacting with Excel.

This still leaves a whole list of problems that may be encountered, as per the linked MSDN article. On the whole I would not advise interacting with Excel in multiple threads unless it's really necessary. There are no problems with background workers that don't interact with Excel, but interactions with Excel application through background threads is too troublesome to be worth it.

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