samedi 18 avril 2015

Deadlock when using Control.Invoke()

I have to do as an assignment to school a client-server application using .NET Remoting. The client has a class that contains some forms, and it is also an observer to an object from the server. When the server notifies it, it tries to update a Property of a form and a deadlock always occurs. I tried using Control.Invoke and Control.BeginInvoke with no luck.Here is some snippet of code


LibraryController(Client)



public class LibraryController :MarshalByRefObject,IObserver{

private SubscriberGUI subscriberGUI;(this is a form with a datagridview and some buttons)


public void notifyObserver()
{
//this is part of IObserver interface and it is called by the server
refreshAllTables();
}

private void refreshAllTables()
{
refreshSubscriberGUIAvailableBooksTable();
...
}


private void refreshSubscriberGUIAvailableBooksTable()
{
if (subscriberGUI != null)
{

DataTable table = initialiseBooksDataTable();
foreach (Book bk in bookService.getAvailableBooks())
{
table.Rows.Add(bk.getId(), bk.getAuthor(), bk.getTitle(), bk.getSubscriberId());
}
if (subscriberGUI.AvailableBooksDataGridView.InvokeRequired)
{
SetDataSourceToAvailableBooksDataGridView(table);
}
else
{
subscriberGUI.AvailableBooksDataGridView.DataSource = table;
}


}

}


delegate void SetDataSourceCallback( DataTable table);

private void SetDataSourceToAvailableBooksDataGridView(DataTable table)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.


if (subscriberGUI.AvailableBooksDataGridView.InvokeRequired)
{

SetDataSourceCallback d = new SetDataSourceCallback(SetDataSourceToAvailableBooksDataGridView);

this.subscriberGUI.BeginInvoke(d, new object[] {table });


}
else
{
subscriberGUI.AvailableBooksDataGridView.DataSource = table;
}
}


}


Could anyone shed a light on the problem? I would appreciate your help!!


Aucun commentaire:

Enregistrer un commentaire