|
-
September 9th, 2007, 04:21 AM
#1
Problem with multithread
Hello,
I have function in the main window (main thread) which create a Listener class.
in a function in the Listener class i call a function in the main thread.
this secind function checks a Dictionary which is declared in the main window. the problem is that when the Listener calls the function in the main thread there is an exception that idicates that the Dictionary is null.
when i debug and i stop on the row which cuases the problem the Dictionary is not null.
can some one know were is the problem?
here is the code:
the main class:
Code:
public partial class mainForm : Form
{
public BindingList<EndPoint> endPoints = new BindingList<EndPoint>();
Listener m_listener = null;
Dictionary<string, string> dicConnection = new Dictionary<string, string>();
public mainForm()
{
InitializeComponent();
bindingSource1.DataSource = endPoints;
m_listener = new Listener();
m_listener.NewConnectionEvent += new Listener.NewConnectionDelegate(OnNewConnection);
endPoints.ListChanged += new ListChangedEventHandler(endPoints_ListChanged);
}
void endPoints_ListChanged(object sender, ListChangedEventArgs e)
{
EndPoint ep1 = null;
EndPoint ep2 = null;
string value = string.Empty;
if (e.ListChangedType == ListChangedType.ItemAdded)
{
try
{
ep1 = endPoints[endPoints.Count - 1];
//MessageBox.Show(Thread.CurrentThread.Name.ToString());
if (dicConnection != null && dicConnection.Count > 0)
{
if (dicConnection.ContainsKey(ep1.Name))
}
catch()
{}
}
void OnNewConnection(Socket soc)
{
endPoints.Add("fff");
}
}
the Listener class:
class Listener
{
public delegate void NewConnectionDelegate();
public event NewConnectionDelegate NewConnectionEvent;
public Listener(int port)
{
thread = new Thread(threadProc);
thread.Start();
}
void threadProc()
{
if (NewConnectionEvent != null)
{
NewConnectionEvent();
}
}
}
Last edited by cjard; September 10th, 2007 at 12:48 PM.
Reason: learn how to use CODE tags
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|