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
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];
if (dicConnection != null &&
dicConnection.Count > 0)
{
if (dicConnection.ContainsKey(ep1.Name))
{
//do some code
//when i reach this section the
// dicConnection is null and does not have
// any element and that is the problem
}
}
catch()
{}
}
void OnNewConnection(Socket soc)
{
endPoints.Add("fff"); // that calls
// endPoints_ListChanged
}
}
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.
As I already asked, please wrap your code in code tags because it is too hard to read otherwise. Use the button on the advanced editor or else type them manually.
I don't want to download a text file. I want to be able to read your code in your post. I was more than willing to help but if wrapping the code in tags is too hard then I guess you don't need my help all that much after all.
Use code tags in future, please -- and for heaven's sake, format your code nicely when you write it in the first place. If you want help from others you have to make them interested in reading what you wrote. If it's a dogs dinner, they wont bother.
I don't want to download a text file. I want to be able to read your code in your post. I was more than willing to help but if wrapping the code in tags is too hard then I guess you don't need my help all that much after all.
sadly, it's just as much of a gash mess with them on as off..
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.