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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:57:44+00:00 2026-06-03T22:57:44+00:00

I want to process the http request in thread, the code as follow where

  • 0

I want to process the http request in thread, the code as follow where the problem is that OutputStream (as noted in codes) can not be writen. When I put this process in main thread, it’s no problem. Can you give me any advice?

    public partial class MainWindow : Window
    {
       private void Window_Loaded(object sender, RoutedEventArgs e)
       {
            var server = new HttpServer();
            try
            {
                server.EndPoint = new IPEndPoint(127.0.0.0, 80); //set webServer para
                server.Start(); //start webServer
                server.RequestReceived += server_RequestReceived; //register the event
            }
            catch (Exception ex)
            {
                return;
            }
       }
       private void server_RequestReceived(object sender, HttpRequestEventArgs e)
       {
           var dataProcess = new Action<HttpRequestEventArgs>(DataProcess);
           Dispatcher.BeginInvoke(dataProcess,e); //start thread
       }
       private void DataProcess(HttpRequestEventArgs e)
       {
           StreamReader sr = new StreamReader(@"c:\test.txt");
           string text = sr.ReadToEnd();
           using (var writer = new StreamWriter(e.Response.OutputStream)) //**Cannot write stream**
           {
               writer.Write(text);
           }
           sr.Close();
       }
    }
  • 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-06-03T22:57:46+00:00Added an answer on June 3, 2026 at 10:57 pm

    I think your request is being close before the new thread runs. What you can do is to execute the whole request in the new thread. Something like:

    public partial class MainWindow : Window
    {
       private void Window_Loaded(object sender, RoutedEventArgs e)
       {
            var start = new Action<HttpRequestEventArgs>(Start);
            Dispatcher.start(start,e); //start thread
       }
    
       private void Start(HttpRequestEventArgs e)
       {
            var server = new HttpServer();
            server.EndPoint = new IPEndPoint(127.0.0.0, 80); //set webServer para
            server.Start(); //start webServe
            server.RequestReceived += server_RequestReceived; //register the event
       }
    
       private void server_RequestReceived(object sender, HttpRequestEventArgs e)
       {
           StreamReader sr = new StreamReader(@"c:\test.txt");
           string text = sr.ReadToEnd();
           using (var writer = new StreamWriter(e.Response.OutputStream)) //**Cannot write stream**
           {
               writer.Write(text);
           }
           sr.Close();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to process every element of a for-loop. Taking this code, why is
I want to process a request header using a custom rewrite map. Therefore I
I have the following XML and I want to process it so that I
I have a c++ process, I want that process should always remain on foreground,
Say I have a data file that I want to process; I want to
I have some data generated in MATLAB that I want to process using Perl.
I have a set of .csv files that I want to process. It would
I want to make 10 asynchronous http requests at once and only process the
I have one class named handler and this class process the http request comming
When a http post request from a remote client arrived, I want to pass

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.