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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:01:25+00:00 2026-05-15T00:01:25+00:00

[VS 2010 Beta with .Net Framework 3.5] I’ve written a C# component to asynchronously

  • 0

[VS 2010 Beta with .Net Framework 3.5]

I’ve written a C# component to asynchronously monitor a socket and raise events when data is received. I set the VB form to show message boxes when the event is raised. What I’ve noticed is that when the component raises the event synchronously, the message box blocks the component code and locks the form until the user closes the message. When it’s raised asynchronously, it neither blocks the code, nor locks the form.

What I want is a way to raise an event in such a way that it does not block the code, but is called on the same thread as the form (so that it locks the form until the user selects an option.)

Can you help me out?
Thanks.

[Component]

using System;
using System.Threading;
using System.ComponentModel;

namespace mySpace
{
    public delegate void SyncEventHandler(object sender, SyncEventArgs e);
    public delegate void AsyncEventHandler(object sender, AsyncEventArgs e);

    public class myClass
    {
        readonly object syncEventLock = new object();
        readonly object asyncEventLock = new object();

        SyncEventHandler syncEvent;
        AsyncEventHandler asyncEvent;

        private delegate void WorkerDelegate(string strParam, int intParam);

        public void DoWork(string strParam, int intParam)
        {
            OnSyncEvent(new SyncEventArgs());
            AsyncOperation asyncOp = AsyncOperationManager.CreateOperation(null);
            WorkerDelegate delWorker = new WorkerDelegate(ClientWorker);
            IAsyncResult result = delWorker.BeginInvoke(strParam, intParam, null, null);
        }

        private void ClientWorker(string strParam, int intParam)
        {
            Thread.Sleep(2000);
            OnAsyncEvent(new AsyncEventArgs());
            OnAsyncEvent(new AsyncEventArgs());
        }

        public event SyncEventHandler SyncEvent
        {
            add { lock (syncEventLock) syncEvent += value; }
            remove { lock (syncEventLock) syncEvent -= value; }
        }
        public event AsyncEventHandler AsyncEvent
        {
            add { lock (asyncEventLock) asyncEvent += value; }
            remove { lock (asyncEventLock) asyncEvent -= value; }
        }

        protected void OnSyncEvent(SyncEventArgs e)
        {
            SyncEventHandler handler;
            lock (syncEventLock) handler = syncEvent;
            if (handler != null) handler(this, e, null, null); // Blocks and locks
            //if (handler != null) handler.BeginInvoke(this, e, null, null); // Neither blocks nor locks
        }
        protected void OnAsyncEvent(AsyncEventArgs e)
        {
            AsyncEventHandler handler;
            lock (asyncEventLock) handler = asyncEvent;
            //if (handler != null) handler(this, e, null, null); // Blocks and locks
            if (handler != null) handler.BeginInvoke(this, e, null, null); // Neither blocks nor locks
        }
    }
}

[Form]

Imports mySpace

Public Class Form1

    Public WithEvents component As New mySpace.myClass()

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        component.DoWork("String", 1)
    End Sub

    Private Sub component_SyncEvent(ByVal sender As Object, ByVal e As pbxapi.SyncEventArgs) Handles component.SyncEvent
        MessageBox.Show("Synchronous event", "Raised:", MessageBoxButtons.OK)
    End Sub

    Private Sub component_AsyncEvent(ByVal sender As Object, ByVal e As pbxapi.AsyncEventArgs) Handles component.AsyncEvent
        MessageBox.Show("Asynchronous event", "Raised:", MessageBoxButtons.OK)
    End Sub
End Class
  • 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-15T00:01:26+00:00Added an answer on May 15, 2026 at 12:01 am

    You need to call the form‘s BeginInvoke method (only), which will run a delegate on the form’s UI thread (thus blocking the form), without blocking the calling thread to wait for the call to finish.

    If you don’t have a reference to the form instance, you can save SynchronizationContext.Current from the UI thread, then call Post on the SynchronizationContext instance, which will be equivalent.

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

Sidebar

Ask A Question

Stats

  • Questions 403k
  • Answers 403k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's pretty simple. You can summarise a simple community project's… May 15, 2026 at 5:15 am
  • Editorial Team
    Editorial Team added an answer If all your rows are the same height, then you… May 15, 2026 at 5:15 am
  • Editorial Team
    Editorial Team added an answer $_SERVER['HTTP_REFERER'] is the most reliable information you'll get about where… May 15, 2026 at 5:15 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.