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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:34:05+00:00 2026-05-30T13:34:05+00:00

Not quite sure how to do this or if it even is a proper

  • 0

Not quite sure how to do this or if it even is a proper way to program c#. Maybe I need to rethink what I am trying to do, but I need some help.

I have a wpf application that starts/stops a service and also uses a named pipe to open a port and communicate with the service. So when the service needs to it can send a message to the app. I started a new thread to invoke a Wait() method that sits at namedPipeServerStream.WaitForConnection(). This works fine, but when the service is stopped I send a message to the app so it breaks the WaitForConnection, however I dont want to kill this thread, I want to reinvoke the same method and wait in a loop until the service starts again. Not sure of the best way to do this.

The code I have so far is below.

void Wait()
{
    while (!ServiceControl.ServiceRunning)
    {
        Thread.Sleep(250);
        if (exitProgram == true)
        {
            break;
        }
    }

    while (ServiceControl.ServiceRunning)
    {
        try
        {
            NamedPipeServerStream namedPipeServerStream = new NamedPipeServerStream("pipeName");
            namedPipeServerStream.WaitForConnection();
            byte[] buffer = new byte[255];
            namedPipeServerStream.Read(buffer, 0, 255);
            string request = ASCIIEncoding.ASCII.GetString(buffer);

            if (request != null)
            {
                if (request == "pipeExit") 
                {
                    Wait(); //Reinvoke Wait if svc calls to exit
                } 

                else
                {
                    //Do work on message
                }
            }
            namedPipeServerStream.Close();
        }
        catch (Exception){}
    }

    if (_opener.exitProgram == true)
    {
        Application.Current.Dispatcher.InvokeShutdown();
    }
  • 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-30T13:34:06+00:00Added an answer on May 30, 2026 at 1:34 pm

    Why don’t you put a while(!opener.exitProgram) around your entire Wait implementation? That way you’ll get back to the start once the pipe is closed.

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

Sidebar

Related Questions

I'm not quite sure how this happened, but somehow a completely empty hierarchy of
I'm not quite sure how to go about framing this question, but I am
I'm not quite sure how to start explaining this, but I'll try. I started
This may sound stupid, but I'm not quite sure how to get the W
Not quite sure if I should even be doing this. What I am wanting
I'm not quite sure what's happening, but all signs point me in this direction..
Not quite sure how to explain this but here goes. I have a function
I'm not sure if this is even possible or not, and I can't quite
I'm not quite sure even after reading the documentation how to do this with
I'm not quite sure if this is possible, or falls into the category 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.