I have a problem that has been driving me nuts for days.. I’ve tried so many tutorials and code snippets from this and many other websites. I am building a P2P application and i have problems accessing the main thread.
Here is the simple flow of my application:
1. frmMain is shown – user clicks on login button
2. frmlogin is shown – user enters his name
3. after “logging in” – two threads are created (threadTCPlistener and threadUDPlistener)
4. frmDataGrid is shown
Server listen = new Server();
Thread listeningUDPThread = new Thread(new ThreadStart(listen.startUDPServer));
listeningUDPThread.IsBackground = true;
listeningUDPThread.Start();
Thread listeningTCPThread = new Thread(new ThreadStart(listen.startTCPServer));
listeningTCPThread.IsBackground = true;
listeningTCPThread.Start();
frmDataGrid dg = new frmDataGrid();
dg.Show();
5.Threads work in one separate class called “Server”. In there they wait for incoming connections, and when TCP thread accepts a connection it starts receinving a file. Upon receiving the file, I would like to change the GUI in the frmDataGrid to add a new row to grid view. I’ve done something like this:
public void downloadFile()
{
//--receiving of the file--
frmDataGrid fdg = new frmDataGrid();
//filename is the name of received file, and 100's are just for testing (for now).
fdg.verifyUIRequest(fileName, 100, 100);
}
I am calling a method from frmDataGrid VerifyUIRequest that looks like this:
public void verifyUIRequest(string filename, int done, int percent)
{
if (dgvDown.InvokeRequired)
{
dgvDown.Invoke((MethodInvoker)delegate { updateDownDgv(filename, done, percent); });
}
else
{
updateDownDgv(filename, done, percent);
}
After this, the main thread should call the “updateDownDgv” method but the problem is that nothing is happening with my data grid. Here is the code for updating:
public void updateDownDgv(string filename, int done, int percent)
{
foreach (DataGridViewRow r in dgvDown.Rows)
{
if ((string)r.Cells[0].Value == filename)
{
r.Cells[1].Value = done;
r.Cells[2].Value = percent;
}
dgvDown.Invalidate();
return;
}
DataTable tab = (DataTable)dgvDown.DataSource;
DataRow row = tab.NewRow();
row[0] = filename;
row[1] = percent;
row[2] = done;
//MessageBox.Show(done.ToString());
tab.Rows.Add(row);
dgvDown.DataSource = null;
dgvDown.DataSource = tab;
}
I have tried doing this withh begin invoke, with some lambda expressions but nothing succeded. Can anyone please point me to an error or help in some other way? I would really appreciate it.
PS This is my first post, so if it is poorly formatted, i apologize in advance. 🙂
EDIT:
So the problem is obviously with instances, so I’ve done something like this:
from Server class where I create an instance of my frmDataGrid class, i now call it’s constructor that takes 3 arguments.
frmDataGrid fdg = new frmDataGrid(fileName, 100, 100);
in that constructor, in frmDataGrid, I call verifyUIRequest. But then another error occurs, and I can’t seem to figure it out. It stops at
if (dgvDown.InvokeRequired)
{...
error is as folows:
“Object reference not set to an instance of an object”, i.e. NullReferenceException. What could be the error?
You are creating a brand new data grid in your
downloadFilemethod. You should be updating the main grid and calling methods on that from your thread methods, not creating a new one that you drop on the floor when thedownloadFilemethod exits.