TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Mahmoud Karrout
NA
16
1.4k
what is problem in this code for multi threads
Jun 21 2018 4:11 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Serverfrn.AQDatabaseDataSetTableAdapters;
namespace Serverfrn
{
public partial class Serverfrm : Form
{
// var
private Thread getStudents;//Thread acquiring client connections
private TcpListener listener; // listen for client connection
// private Thread[] studentThreads; //Threads for client interaction
public List<Thread> Students_threads_list; // List that holds the game players threads
internal bool disconnected = false; // true if the server closes
public Socket socket;
StudentClient[] student_client_array;
StudentClient student_client;
Thread student_thread;
int students_counter = 0 ; // a counter for the students
public MyDataTableAdapter Adapter = new MyDataTableAdapter(); // setup connection with DataBase
public Serverfrm()
{
InitializeComponent();
}
private void Serverfrm_Load(object sender, EventArgs e)
{
DisplayMessage("Welcome Admin at Demo system ");
}
// delegate that allows method DisplayMessage to be called
// in the thread that creates and maintains the GUI
private delegate void DisplayDelegate(string message);
// method DisplayMessage sets displayTextBox's Text property
// in a thread-safe manner
internal void DisplayMessage(string message)
{
// if modifying displayTextBox is not thread safe
if (displayTextBox.InvokeRequired)
{
// use inherited method Invoke to execute DisplayMessage
// via a delegate
Invoke(new DisplayDelegate(DisplayMessage),
new object[] { message });
} // end if
else // OK to modify displayTextBox in current thread
displayTextBox.Text += message;
} // end method DisplayMessage
private void SetUp(object argument)
{
TcpClient client = (TcpClient)argument;
try
{
StreamReader reader = new StreamReader(client.GetStream());
StreamWriter writer = new StreamWriter(client.GetStream());
string s = String.Empty;
while (s == null)
{
writer.Write("From client -> " + s);
writer.WriteLine("From server -> " + s);
}
reader.Close();
writer.Close();
client.Close();
DisplayMessage("Closing client connection!");
}
catch (IOException)
{
Console.WriteLine("Problem with client communication. Exiting thread.");
}
finally
{
if (client != null)
{
client.Close();
}
}
}
// notify Students to stop Running
private void Serverfrm_FormClosing(object sender, FormClosingEventArgs e)
{
disconnected = true;
System.Environment.Exit(System.Environment.ExitCode);
}
private void button2_Click(object sender, EventArgs e)
{
AddQuestion frm = new AddQuestion();
frm.ShowDialog(this);
}
private void button1_Click(object sender, EventArgs e)
{
int port = int.Parse(textBox2.Text);
IPAddress address = IPAddress.Parse(textBox1.Text);
DisplayMessage("Server sarted");
TcpListener listener = null;
try
{
listener = new TcpListener( address, port );
DisplayMessage("\r\nlistener started...");
listener.Start();
DisplayMessage("\r\nMultiThreadedEchoServer started...");
while (true)
{
DisplayMessage("/r/nWaiting for incoming client connections.../r/n");
TcpClient client = listener.AcceptTcpClient();
DisplayMessage("/r/nAccepted new client connection.../r/n");
getStudents = new Thread(SetUp);
getStudents.Start(client);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (listener != null)
{
listener.Stop();
}
}
}// end method Serverfrm_FormClosing
}
}
Reply
Answers (
1
)
Where does the C# Corner webinar recordings are available?
Which is the best Website for Laravel Certified Course?