C# Multithreading — Invoke without a Control

前端 未结 7 1196
闹比i
闹比i 2020-12-13 20:44

I am only somewhat familiar with multi-threading in that I\'ve read about it but have never used it in practice.

I have a project that uses a third party library tha

7条回答
  •  被撕碎了的回忆
    2020-12-13 21:21

    Use SynchronizationContext.Current, which will point to something that you can synchronize with.

    This will do the right thing™ depending on the type of application. For a WinForms application, it will run this on the main UI thread.

    Specifically, use the SynchronizationContext.Send method, like this:

    SynchronizationContext context =
        SynchronizationContext.Current ?? new SynchronizationContext();
    
    context.Send(s =>
        {
            // your code here
        }, null);
    

提交回复
热议问题