Click here to Skip to main content
15,889,403 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to get painted image from picturebox Pin
Vishwanath Patil12-Nov-09 18:04
Vishwanath Patil12-Nov-09 18:04 
GeneralRe: How to get painted image from picturebox Pin
Dave Kreskowiak13-Nov-09 15:15
mveDave Kreskowiak13-Nov-09 15:15 
QuestionThreaded Proxy? Pin
joana.simoes12-Nov-09 0:40
joana.simoes12-Nov-09 0:40 
AnswerRe: Threaded Proxy? Pin
Covean12-Nov-09 2:08
Covean12-Nov-09 2:08 
GeneralRe: Threaded Proxy? Pin
Luc Pattyn12-Nov-09 2:23
sitebuilderLuc Pattyn12-Nov-09 2:23 
GeneralRe: Threaded Proxy? Pin
Covean12-Nov-09 2:47
Covean12-Nov-09 2:47 
AnswerRe: Threaded Proxy? Pin
Luc Pattyn12-Nov-09 2:22
sitebuilderLuc Pattyn12-Nov-09 2:22 
GeneralRe: Threaded Proxy? Pin
joana.simoes13-Nov-09 5:22
joana.simoes13-Nov-09 5:22 
Ok, thanks a lot all for your answers.
So I think the summary of the situation concerning multithreading in C# is (correct me if I'm wrong):
- use asynchronous requests and let the system handle the multithreading; (1)
- use threadPool and let the system handle the multitreading (distribute tasks among worker threads); (2)
- implement "real" multi threading, handling synchronization, locks, etc; (3)

I wanted to keep it as simple as possible, without having to write a lot of "thread" of code (unless absolutely necessary), so I tried to stick to option (2), which as Luc Pattyn mentioned, has some limitations but I think I can "live" with them (that is, the app fails sometimes, I can catch the errors and carry on without major implications for the user).

One thing that I amended in my code, was removing the "restart" of the listener on the event of an exception; there is no need to stop and start the listener on error, and in fact, when I had a lot of threads running, the "restart" of the listener itself was a cause of problems...

The I/O operation has been aborted because of either a thread exit or an application request

So, after refactoring my "ProcessRequest" callback, it now looks like this: no restart of the listener and separate handling of different types of exceptions; one important thing is to close the Context.Response after an error, so that it sends a response to the client;

private void ProcessRequest(object listenerContext)
 {
     try
     {
         byte[] buffer;
         var context = (HttpListenerContext)listenerContext;

         try
         {
             //Make necessary replacements in the host name
             Uri aUri = context.Request.Url;
             string url = aUri.AbsolutePath + aUri.Query;

             System.Uri uri = m_service.GetUri();
             string address = uri.Scheme + "://" + uri.Host + ":" + uri.Port + url;

             // Build Request
             HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

             //DO STUFF

             // Close streams
             instream.Close();
             outstream.Close();
             context.Response.OutputStream.Close();
         }

         catch (System.ArgumentException ex)
         {
             context.Response.StatusCode = int.Parse(ex.Message);
             context.Response.StatusDescription = GetStatusDescription(context.Response.StatusCode);

             byte[] buff = System.Text.Encoding.UTF8.GetBytes("<HTML><BODY><b>Proxy Failure:</b> (" +
                 context.Response.StatusCode + ") " + context.Response.StatusDescription + "</HTML></BODY>");

             context.Response.ContentLength64 = buff.Length;
             context.Response.OutputStream.Write(buff, 0, buff.Length);
         }
         catch (System.Net.HttpListenerException ex) { }
         catch (System.Net.WebException ex)
         {
             int status = (int)((HttpWebResponse)ex.Response).StatusCode;

             context.Response.StatusCode = status;
             context.Response.StatusDescription = GetStatusDescription(status);

             context.Response.Close();
         }
         catch (Exception ex) { }//Default
     }
     catch (Exception ex)
     {
         System.Diagnostics.Debug.WriteLine(ex.Message);
         // It should not come here! Are we having troubles closing the stream?
     }
 }


It's not the best solution, has it fails to deliver some jobs (not properly handling the multithreading), but its kind of working and is simple.
cheers,
Jo
QuestionPDF conversion Pin
Santhosh Sebastian Mattathil11-Nov-09 23:34
Santhosh Sebastian Mattathil11-Nov-09 23:34 
QuestionShowing non 96dpi jpegs in PictureBox Pin
Chesnokov Yuriy11-Nov-09 23:00
professionalChesnokov Yuriy11-Nov-09 23:00 
AnswerRe: Showing non 96dpi jpegs in PictureBox Pin
Eduard Keilholz11-Nov-09 23:03
Eduard Keilholz11-Nov-09 23:03 
QuestionLoading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 22:57
Larantz11-Nov-09 22:57 
AnswerRe: Loading app.config from a remote location fails to load section handler. Pin
Shameel11-Nov-09 23:38
professionalShameel11-Nov-09 23:38 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 23:40
Larantz11-Nov-09 23:40 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 23:50
Larantz11-Nov-09 23:50 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Shameel12-Nov-09 0:00
professionalShameel12-Nov-09 0:00 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz12-Nov-09 0:11
Larantz12-Nov-09 0:11 
Questionadd row to data grid view problem Pin
Rinad11-Nov-09 22:51
Rinad11-Nov-09 22:51 
AnswerMessage Closed Pin
11-Nov-09 22:55
stancrm11-Nov-09 22:55 
GeneralRe: add row to data grid view problem Pin
Rinad11-Nov-09 23:31
Rinad11-Nov-09 23:31 
GeneralRe: add row to data grid view problem Pin
Gerry Schmitz12-Nov-09 1:39
mveGerry Schmitz12-Nov-09 1:39 
GeneralRe: add row to data grid view problem Pin
Luc Pattyn12-Nov-09 2:34
sitebuilderLuc Pattyn12-Nov-09 2:34 
QuestionInheritence Pin
sris 42611-Nov-09 22:27
sris 42611-Nov-09 22:27 
AnswerRe: Inheritence Pin
The Man from U.N.C.L.E.11-Nov-09 22:38
The Man from U.N.C.L.E.11-Nov-09 22:38 
GeneralRe: Inheritence Pin
sris 42611-Nov-09 22:59
sris 42611-Nov-09 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.