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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:54:53+00:00 2026-05-29T17:54:53+00:00

I am trying to fire a method as a result to an asynchronous read

  • 0

I am trying to fire a method as a result to an asynchronous read operation on a NetworkStream. The read operation is done via BeginRead and EndRead, resulting in the EndRead being called in the callback specified with BeginRead. All pretty basic stuff. Now, as the callback is done in a system generated Thread, the data read from the NetworkStream is no longer owned by my thread, that called the BeginRead. To overcome that, I wrote a method to further process the data read, which I try to call via my Thread’s dispatcher.

// These are created in my Thread
private delegate void ReceiverCallback(IPEndPoint sender, byte[] data);
private Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

With the callback looking like this:

private void DataReceived(IAsyncResult result)
{
    DataReceivedStruct drs = (DataReceivedStruct)result.AsyncState;
    NetworkStream used = drs.stream;
    AutoResetEvent handle = drs.waitHandle;
    used.EndRead(result);
    DispatchRaiseReceived(readBuffer);
    handle.Set();
}

The DataReceivedStruct is a simple struct holding the NetworkStream and an AutoResetEvent.
ReadBuffer is a global private byte[] of 1024 length, as BeginRead and EndRead aren’t called in the same method.

The DispatchRaiseReceive method looks like this:

private void DispatchRaiseReceived(byte[] data)
{
    dispatcher.BeginInvoke((ReceiverCallback)RaiseReceived, socket.Client.RemoteEndPoint, data);
}

Where socket is a TcpClient object.

The disptached method looks like the following bit of code. What this does is simply pass the data along via an event for further processing.

private void RaiseReceived(IPEndPoint sender, byte[] data)
{
    if(IsEnabled){
        if (Received != null)
        {
            Received(this, new TCPDataArgs(sender, data));
        }
    }
}

The actual method that the dispatcher needs to call, is never called. Now from what I’ve been able to find on the web is that it could have something to do with the dispatcher not being created on the correct Thread, and thus it is never invoking the method on the correct Thread. However, the dispatcher is created on my Thread, so that shouldn’t apply.

  • 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-29T17:54:54+00:00Added an answer on May 29, 2026 at 5:54 pm

    As an attempt to fix this, I explicitly got the Dispatcher from my UI-Thread, and passed that along to the desired place I wanted to use it. This instead of trying to get it with Dispatcher.CurrentDispatcher. And what do you know, it worked. The delegate is now being called correctly.

    Apparently Dispatcher.CurrentDispatcher didn’t get the correct Dispatcher I wanted it to use.

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

Sidebar

Related Questions

I'm trying to fire a Notification in a method called setPosition in one class,
I'm trying to fix a bug that involves UIView hitTest:withEvent: being called on my
I am trying to fire a method once a day at a given time.
I'm trying to use JQuery to have a method fire when the following code
I'm trying to implement a fire-and-forget pattern in a WCF method. I have this
I'm trying to attach a simple focus/blur event listener via the .live() jQuery method
I am trying to fire an event on the right and left arrow key
I am new to jQuery and am trying to fire a function on a
I'm trying to get an event to fire whenever a choice is made from
I'm trying to teach Komodo to fire up IDLE when I hit the right

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.