Click here to Skip to main content
15,902,198 members
Home / Discussions / C#
   

C#

 
GeneralMFC 2 C# Pin
alex.barylski11-Feb-02 5:57
alex.barylski11-Feb-02 5:57 
GeneralRe: MFC 2 C# Pin
Mazdak11-Feb-02 6:13
Mazdak11-Feb-02 6:13 
GeneralSpawning an application from C# Pin
aluedke9-Feb-02 13:58
aluedke9-Feb-02 13:58 
GeneralRe: Spawning an application from C# Pin
James T. Johnson9-Feb-02 18:55
James T. Johnson9-Feb-02 18:55 
QuestionHosting ASP.NET outside IIS? Pin
roglan9-Feb-02 11:54
roglan9-Feb-02 11:54 
GeneralOpenThemeData Pin
kasturirawat8-Feb-02 13:18
kasturirawat8-Feb-02 13:18 
GeneralANN: Magic Version 1.1 released Pin
Phil Wright8-Feb-02 11:50
Phil Wright8-Feb-02 11:50 
GeneralTcpClient throws exception when on worker thread Pin
JohnRLewis8-Feb-02 7:01
JohnRLewis8-Feb-02 7:01 
// // What am I doing wrong here?
//
using System;
using System.Net.Sockets;
using System.Threading;


class Class1
{

[STAThread]
static void Main(string[] args)
{
WorkerClass w = new WorkerClass();

// If I call the Start method on the
// main thread, the code works properly
// w.Start();

// But I want to run the code on a own worker thread.
// However, the code will throw an exception.

// Unhandled Exception: System.IO.IOException:
// Unable to read data from the transport connection.
// ---> System.Net.Sockets.SocketException:
// The I/O operation has been aborted because of either a thread exit or an application request

Thread workerThread = new Thread(new ThreadStart(w.Start));
workerThread.Start();

Console.ReadLine();
}
}

class WorkerClass
{
private TcpClient tcp;
private AsyncCallback readCallback;
private byte[] buffer;

public void Start()
{
buffer = new byte[1024];

readCallback = new AsyncCallback(ReadComplete);

tcp = new TcpClient();
tcp.Connect("www.microsoft.com", 80);

byte[] request = System.Text.ASCIIEncoding.ASCII.GetBytes("GET /\n\n");
tcp.GetStream().Write(request, 0, request.Length);

ReadStart();
}

private void ReadStart()
{
tcp.GetStream().BeginRead(buffer, 0, buffer.Length, readCallback, null);
}

private void ReadComplete(IAsyncResult ar)
{
int bytesRead = tcp.GetStream().EndRead(ar);
Console.WriteLine("{0} bytes read.", bytesRead);
if (tcp.GetStream().DataAvailable)
ReadStart();
}
}




---
John R. Lewis
john@aspZone.com
GeneralNaming Convention Pin
paulb7-Feb-02 14:05
paulb7-Feb-02 14:05 
GeneralRe: Naming Convention Pin
Stan Shannon7-Feb-02 15:31
Stan Shannon7-Feb-02 15:31 
GeneralRe: Naming Convention Pin
7-Feb-02 15:33
suss7-Feb-02 15:33 
GeneralRe: Naming Convention Pin
James T. Johnson7-Feb-02 17:38
James T. Johnson7-Feb-02 17:38 
GeneralRe: Naming Convention Pin
paulb7-Feb-02 17:54
paulb7-Feb-02 17:54 
GeneralRe: Naming Convention Pin
James T. Johnson7-Feb-02 18:45
James T. Johnson7-Feb-02 18:45 
GeneralRe: Naming Convention Pin
Senkwe Chanda11-Feb-02 2:16
Senkwe Chanda11-Feb-02 2:16 
GeneralRe: Naming Convention Pin
James T. Johnson11-Feb-02 16:39
James T. Johnson11-Feb-02 16:39 
GeneralRe: Naming Convention Pin
Peter Stephens8-Feb-02 7:44
Peter Stephens8-Feb-02 7:44 
Generaltrying to prevent user from exiting the application sometimes Pin
7-Feb-02 6:59
suss7-Feb-02 6:59 
GeneralRe: trying to prevent user from exiting the application sometimes Pin
7-Feb-02 13:01
suss7-Feb-02 13:01 
GeneralWindows Form Help Application Pin
kyledunn7-Feb-02 1:39
kyledunn7-Feb-02 1:39 
GeneralVisio with c# Pin
6-Feb-02 23:04
suss6-Feb-02 23:04 
GeneralShow ContextMenu in Node Pin
thongkk6-Feb-02 14:51
thongkk6-Feb-02 14:51 
GeneralRe: Show ContextMenu in Node Pin
Schnemar7-Mar-02 4:38
Schnemar7-Mar-02 4:38 
GeneralRe: Show ContextMenu in Node Pin
thongkk7-Mar-02 14:26
thongkk7-Mar-02 14:26 
GeneralWin9x Pin
woodcarver6-Feb-02 11:41
woodcarver6-Feb-02 11:41 

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.