Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219136
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:36:36+00:00 2026-05-17T15:36:36+00:00

I need to send a piece of code from another thread (Excel Interop) to

  • 0

I need to “send” a piece of code from another thread (Excel Interop) to the UI thread to execute. Normally you’d use Invoke on the Form, which implements the ISynchronizeInvoke interface:

public class MyForm : Form
{
    ...
    private void Button_Click(object sender, EventArgs e)
    {
        SomeExcelWorkbook.OnBeforeClose += delegate(ref bool Cancel)
        {
            this.Invoke(someCode);
        };
    }
}

Unfortunately there is a layer of abstraction between the form code and the code that defines the event handler, and I don’t have a reference to the form at that point:

public void CodeExecutedByUIThread()
{
    ISynchronizeInvoke sync;
    SomeExcelWorkbook.OnBeforeClose += delegate(ref bool Cancel)
    {
        sync.Invoke(someCode);
    };
}

When entering CodeExecutedByUIThread, we are still in the UI thread, so in theory everything needed should be there. Unfortunately the Form is the only class implementing that interface, is it?

How can I get an ISynchronizeInvoke from within the UI thread? Apparently Thread.CurrentThread doesn’t provide it…

Or is there a way to get a SynchronizationContext? How would I retrieve and use that?

Update: Oh, I see, getting the SynchronizationContext object looks as simple as SynchronizationContext.Current, which doesn’t need any reference to a form. So I’ll google little bit more about how to use that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:36:36+00:00Added an answer on May 17, 2026 at 3:36 pm

    In general, I think that neither is possible.

    This might seem like an obvious answer, but what we used in this case was to store both the SynchronizationContext and the reference to the Form (as an ISynchronizeInvoke) in a static class at application startup.

    MainForm main = new MainForm();
    EnvironmentService.UI = main;
    EnvironmentService.UIContext = SynchronizationContext.Current;
    Application.Run(main);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We need send h264 from flash to Wowza and after to vlc by RTSP
I need send certain attributes(say, human readable user name) from server to client after
I've a custom form (created with form API) that need send an uploaded file
I need to send an HTML report with images from an application. The report
I need to send some arguments from the iPhone to a php in the
I need to send emails from my web application (on account creation, password reset,
I have a piece of logic that I need to execute either once or
How to send enter from javascript to site? What I real need is to
I need to build an application which can receive data from over a network
I need to send video stream between 2 mobile devices. Bada has no support

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.