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

  • Home
  • SEARCH
  • 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 3660492
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:12:46+00:00 2026-05-19T01:12:46+00:00

I have wrote a simple client that use TcpClient in dotnet to communicate. In

  • 0

I have wrote a simple client that use TcpClient in dotnet to communicate. In order to wait for data messages from server i use a Read() thread that use blocking Read() call on socket. When i receive something i have to generate various events. These event occur in the worker thread and thus you cannot update a UI from it directly. Invoke() can be use but for end developer its difficult as my SDK would be use by users who may not use UI at all or use Presentation Framework. Presentation framework have different way of handling this. Invoke() on our test app as Microstation Addin take a lot of time at the moment. Microstation is single threaded application and call invoke on its thread is not good as it is always busy doing drawing and other stuff message take too long to process.

I want my events to generate in same thread as UI so user donot have to go through the Dispatcher or Invoke.

Now i want to know how can i be notified by socket when data arrive? Is there a build in callback for that. I like winsock style receive event without use of separate read thread. I also do not want to use window timer to for polling for data.

I found IOControlCode.AsyncIO flag in IOControl() function which help says

Enable notification for when data is
waiting to be received. This value is
equal to the Winsock 2 FIOASYNC
constant.

I could not found any example on how to use it to get notification. If i am right in MFC/Winsock we have to create a window of size(0,0) which was just used for listening for the data receive event or other socket events. But i don’t know how to do that in dotnet application.

  • 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-19T01:12:46+00:00Added an answer on May 19, 2026 at 1:12 am

    Ok I got it up and running. What I was really looking to was how to seamlessly post events to an UI thread, in which my connection is created. After going through framework code I came up with following proof of concept. SynchronizationContext can be use to bind my component to the UI thread that created it. Then I can post events to that UI thread directly, without using Invoke.

    In the following example I created a ThreadUISafeTimer which uses a seperate thread, just like my socket client that uses one for reading and raising events. In this case, context is used to post the event if not null, otherwise the event is raised using the worker thread.

    [DefaultEvent("Tick")]
    public class ThreadUISafeTimer : Component
    {
        private const int True = 1;
        private const int False = 0;
        private int enabled = False;
        private SynchronizationContext context;
    
        public event EventHandler Tick = delegate { };
    
        [DefaultValue(false)]
        public ushort Interval { get; set; }
    
        public ThreadUISafeTimer() {
            Interval = 100;
            this.Events.AddHandler("Tick", Tick);
            //If this class is created by a UI thread it will always post the Tick event to it.
            //otherwise it would be null and Tick would occur in a seperate thread.
            context = SynchronizationContext.Current;
    
        }
        protected override bool CanRaiseEvents {
            get {
                return true;
            }
        }
        [DefaultValue(false)]
        public bool Enabled {
            get {
                return enabled == True;
            }
            set {
                int newval = value ? True : False;
                if (enabled != newval) {
                    if (newval == False)
                        Thread.VolatileWrite(ref enabled, False);
                    else {
                        enabled = True;
                        ThreadPool.QueueUserWorkItem(
                            new WaitCallback(delegate(object o) {
                            try {
                                do {
                                    try {
                                        Thread.Sleep(Interval);
                                        if (Thread.VolatileRead(ref enabled) == True) {
                                            var callback = new SendOrPostCallback(delegate(object arg) {
                                                try {
                                                    Tick(this, EventArgs.Empty);
                                                }
                                                catch (Exception exp) {
                                                    Application.OnThreadException(exp);
                                                    return;
                                                }
                                            });
                                            //If context is null raise Tick event from current thread
                                            if (context == null)
                                                callback(null);
                                            else
                                                //otherwise post it to the UI thread that owns this timer.
                                                context.Post(callback, null);
                                        }
                                    }
                                    catch (ThreadInterruptedException) {
                                    }
    
                                } while (Thread.VolatileRead(ref enabled) == True);
                            }
                            catch (ThreadAbortException) {
                            }
                        }), null);
                    }
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I'm getting this exception from a simple JMS client I wrote to
I have to write a PHP script that works as a client against another
I need to consume a SOAP web service from a C# .Net 4.0 client.
I have a typical Silverlight application with a WCF service and I am using
I have recently starting porting a small app of mine to Facebook as a
I asked a question a few days ago ( Access to SQL Server 2005
I'm working on my first homework project in a web programming class, which is
I am familiar with the usage of Bonjour for advertising services on the local
Looking for some ideas/pattern to solve a design problem for a system I will
Given a public SOAP web service and no WSDL, I need to build a

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.