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 6170893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:07:10+00:00 2026-05-23T23:07:10+00:00

In our legacy VB6 code we used a system timer to perform a callback

  • 0

In our legacy VB6 code we used a system timer to perform a callback so that state machines could be run without blocking event handlers. Some Psuedo code example

Sub Unhooked(State info)
    Select Case info
        Case 1
            NextState = somestate1
        Case 2
            NextState = somestate2
        Case 3
            NextState = somestate3
    End Select
    RunStateMachine
End Sub

Sub RunStateMachine()
    MyObject.GoDoSomethingAndCallMeBack
End Sub

Sub MyObject_EventCallback(State info)
    APITimer.SetUpCallBackTarget (Unhooked, info)
    APITimer.CallMeBackInASec
End Sub

Hopefully you get the idea where some call is made in the state machine that will result later on in the event handler being fired and it sets up a timer to do a callback so that the event handler code can complete before we move on to the next state.

I am continuing a similar vein in C# but it feels wrong as I’m sure the language would provide a cuter way of doing this. Since my C# state machine is still calling VB6 objects that raise events on the UI thread, is there a better way of ‘unhooking’ the event handler so that it is released before the state machine continues on it’s way?

I thought I could use BeginInvoke to add a message to the pump to run the state machine but on the same thread instead of a thread pool thread but my class isn’t a form or control. I think I could come up with a solution that doesn’t use timers but, well they are dead simple to use for this so any ideas would be great.

  • 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-23T23:07:11+00:00Added an answer on May 23, 2026 at 11:07 pm

    To solve your immediate problem, you could queue a Task to the UI thread context, e.g.:

    var ui = TaskScheduler.FromCurrentSynchronizationContext();
    Task.Factory.StartNew(() => Unhooked(info), CancellationToken.None,
        TaskCreationOptions.None, ui);
    

    The UI SynchronizationContext can be detected even though your object is not a form or control. For more information on SynchronizationContext, see my MSDN article.

    If you have this in your code a lot, you’ll probably want to wrap it up into a helper method:

    public void CallbackLater(Action action)
    {
      Task.Factory.StartNew(action, CancellationToken.None,
        TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());
    }
    
    void MyObject_EventCallback(State info)
    {
      CallbackLater(() => Unhooked(info));
    }
    

    In the broader scheme of things, seriously consider the Async CTP and designing a Task-Based Asynchronous Pattern API instead of having the events raised on the UI thread (i.e., the Event-Based Asynchronous Pattern).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am examining our legacy system, and there is a method that sends an
I'm changing some legacy code so that is uses smart pointers. Our projects pre-date
We have a legacy code that uses MSXML2 with vb6 that works fine. Lately
I've run into some interesting code in our legacy application running under Internet Explorer.
I’m looking at rewriting a portion of our application in C# (currently legacy VB6
We are rewriting our legacy accounting system in VB.NET and SQL Server. We brought
I'm updating some of our legacy C++ code to use the MFC feature pack
Me and several other developers are currently cleaning up our legacy code base, mostly
A very small portion of our codebase is some legacy Java code. I'm trying
I'm updating legacy code written in VB6 using Winsock controls. Essentially, I'm trying to

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.