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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:05:48+00:00 2026-05-28T13:05:48+00:00

I am very very new to delegates! I have an AsyncCallback delegate which runs

  • 0

I am very very new to delegates!

I have an AsyncCallback delegate which runs a method.

This method periodically writes text to the console (Console.WriteLine(“FooBar”))

This delegate is kicked off from my Main method and I need to find a way to keep this Main method open while the delegate runs. Otherwise, the program starts, kicks off the delegate and closes again so I am using Console.Readline.

Will this work? Will my program be able to both sit at Console.ReadLine while my delegate periodically writes text to the console with Console.WriteLine or am I an idiot? My code is below:

static void Main(string[] args)
{
    NetworkStream myNetworkStream;
    Socket socket;
    IPEndPoint maxPort = new IPEndPoint(IPAddress.Parse("x.x.x.x"), xxxx);

    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
    socket.Connect(maxPort);

    myNetworkStream = new NetworkStream(socket);

    byte[] buffer = new byte[1024];
    int offset = 0;
    int count = 1024;

    string loginString = "FOOBARR";
    ASCIIEncoding encoder = new ASCIIEncoding();

    myNetworkStream.BeginRead(buffer, offset, count, new AsyncCallback(OnBeginRead), myNetworkStream);
    myNetworkStream.Write(encoder.GetBytes(loginString), 0, encoder.GetByteCount(loginString));

    Console.ReadLine();
}

public static void OnBeginRead(IAsyncResult ar)
{
    NetworkStream ns = (NetworkStream)ar.AsyncState;
    int bufferSize = 2014;
    byte[] received = new byte[bufferSize];
    string result = String.Empty;

    ns.EndRead(ar);

    int read;

    while (true)
    {
        if (ns.DataAvailable)
        {
            read = ns.Read(received, 0, bufferSize);
            result += Encoding.ASCII.GetString(received);
            received = new byte[bufferSize];

            Console.WriteLine(result);
        }
        else
        {
            Thread.Sleep(1000);
        }
    }
}

Also, if I call ‘myNetworkStream.BeginRead’ multiple times with different parameters, will a different version of my ‘OnBeginRead’ method start each time on a seperate thread on the processor or will the method that was running stop and be replaced by the more recent one?

  • 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-28T13:05:49+00:00Added an answer on May 28, 2026 at 1:05 pm

    Will my program be able to both sit at Console.ReadLine while my delegate periodically writes >text to the console with Console.WriteLine?

    Short answer: Yes

    if I call ‘myNetworkStream.BeginRead’ multiple times with different
    parameters will a different version of my ‘OnBeginRead’ method start
    each time on a seperate thread on the processor or will the method
    that was running stop and be replace by the more recent one

    MSDN says this about calling BeginRead multiple times

    Read and write operations can be performed simultaneously on an
    instance of the NetworkStream class without the need for
    synchronization. As long as there is one unique thread for the write
    operations and one unique thread for the read operations, there will
    be no cross-interference between read and write threads and no
    synchronization is required.

    Which implies that you shouldn’t call BeginRead from multiple threads. This will lead to unexpected behaviour

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

Sidebar

Related Questions

I'm very new to this, so pardon my ignorance. I have the following method
Very new to python and can't understand why this isn't working. I have a
Im very new in C++ I have found this post http://msdn.microsoft.com/en-us/magazine/cc163486.aspx and trying to
I very new to Python, and fairly new to regex. (I have no Perl
Let me preface this with the disclaimer that I am very new to multithreading
I'm very much new to objective C. I have a case where I need
I am very new to C# and i have written the following class: using
This is my first time on this site and I am very new to
I am very new to xcode & Objective-C (having come from PHP) i have
I am very new and very inexperienced. I have downloaded a WPF version of

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.