WPF Threading Model

Y

Yehia A.Salam

Hello,

I am building a network application that make use of .Net Sockets, I created
a class that works like a server and fires an event when anything arrives at
the server, however I ran into some problems because of the WPF threading
model, my code is like:

//*******************************************
public Window1(){ ...
sConnection = new myserver();
sConnection.DateReceived += new
myserver.DataReceivedHandler(OnServerDataReceived);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceived),Re);

}

private void ServerDataReceived(MessageText Re) {
txtconsole.Text = txtconsole.Text + Re.name + ": " + Re.content;
}
//**********************************************

the code runs correctly but I am not satisfied with the structure, I don't
like that I must have at least two methods for modifying anything in the UI,
is there a better structure to do this, like putting BeginInvoke in myServer
Class for example and just assign the ServerDataReceived event in Window1
class like I would normally do in Windows Forms.

Thanks
Yehia A.Salam
 
N

Nicholas Paldino [.NET/C# MVP]

Yehia,

Well, personally, you are taking on a good deal of typing overhead which
I don't feel you have to do. First, you don't have to declare the delegate
constructor when you assign event handlers, like so:

sConnection.DateReceived += OnServerDataReceived;

Also, you can declare your OnServerDataRecieved like so:

private void OnServerDataReceived(MessageText Re)
{
myserver.DataReceivedHandler handler =
delegate(MessageText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " + mt.content;
};

Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
handler, Re);
}

This way, you don't need to have another handler. Granted, the same
amount of jumps are made (the anonymous delegate creates a method on your
class underneath the covers which is really assigned to the delegate), but
you have consolidated the code somewhat.

However, if you need to reuse that code (the ServerDataReceived method)
and using an anonymous delegate only leads to code duplication, then what
you have is the best way to handle it (and code it, with the exception of
declaring the delegate types).
 
W

Willy Denoyette [MVP]

Yehia A.Salam said:
Hello,

I am building a network application that make use of .Net Sockets, I
created a class that works like a server and fires an event when anything
arrives at the server, however I ran into some problems because of the WPF
threading model, my code is like:

//*******************************************
public Window1(){ ...
sConnection = new myserver();
sConnection.DateReceived += new
myserver.DataReceivedHandler(OnServerDataReceived);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceived),Re);

}

private void ServerDataReceived(MessageText Re) {
txtconsole.Text = txtconsole.Text + Re.name + ": " + Re.content;
}
//**********************************************

the code runs correctly but I am not satisfied with the structure, I don't
like that I must have at least two methods for modifying anything in the
UI, is there a better structure to do this, like putting BeginInvoke in
myServer Class for example and just assign the ServerDataReceived event in
Window1 class like I would normally do in Windows Forms.

Thanks
Yehia A.Salam


Using an anon. delegate...

Dispatcher.BeginInvoke(DispatcherPriority.Normal,
(EventHandler)delegate
{
txtconsole.Text = txtconsole.Text + Re.name + ":
" + Re.content;

}, Re);

Willy.
 
Y

Yehia A.Salam

is that my only solution, can I solve this at the server class level?

Nicholas Paldino said:
Yehia,

Well, personally, you are taking on a good deal of typing overhead
which I don't feel you have to do. First, you don't have to declare the
delegate constructor when you assign event handlers, like so:

sConnection.DateReceived += OnServerDataReceived;

Also, you can declare your OnServerDataRecieved like so:

private void OnServerDataReceived(MessageText Re)
{
myserver.DataReceivedHandler handler =
delegate(MessageText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};


Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
handler, Re);
}

This way, you don't need to have another handler. Granted, the same
amount of jumps are made (the anonymous delegate creates a method on your
class underneath the covers which is really assigned to the delegate), but
you have consolidated the code somewhat.

However, if you need to reuse that code (the ServerDataReceived method)
and using an anonymous delegate only leads to code duplication, then what
you have is the best way to handle it (and code it, with the exception of
declaring the delegate types).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Yehia A.Salam said:
Hello,

I am building a network application that make use of .Net Sockets, I
created a class that works like a server and fires an event when anything
arrives at the server, however I ran into some problems because of the
WPF threading model, my code is like:

//*******************************************
public Window1(){ ...
sConnection = new myserver();
sConnection.DateReceived += new
myserver.DataReceivedHandler(OnServerDataReceived);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceived),Re);

}

private void ServerDataReceived(MessageText Re) {
txtconsole.Text = txtconsole.Text + Re.name + ": " + Re.content;
}
//**********************************************

the code runs correctly but I am not satisfied with the structure, I
don't like that I must have at least two methods for modifying anything
in the UI, is there a better structure to do this, like putting
BeginInvoke in myServer Class for example and just assign the
ServerDataReceived event in Window1 class like I would normally do in
Windows Forms.

Thanks
Yehia A.Salam
 
N

Nicholas Paldino [.NET/C# MVP]

Yehia,

There is nothing you can do at the server level. As far as the server
is concerned, it gets a request and responds to it, it cares nothing (nor
should it, or could it) care about what thread the client called on in it's
app domain.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yehia A.Salam said:
is that my only solution, can I solve this at the server class level?

Nicholas Paldino said:
Yehia,

Well, personally, you are taking on a good deal of typing overhead
which I don't feel you have to do. First, you don't have to declare the
delegate constructor when you assign event handlers, like so:

sConnection.DateReceived += OnServerDataReceived;

Also, you can declare your OnServerDataRecieved like so:

private void OnServerDataReceived(MessageText Re)
{
myserver.DataReceivedHandler handler =
delegate(MessageText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};


Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
handler, Re);
}

This way, you don't need to have another handler. Granted, the same
amount of jumps are made (the anonymous delegate creates a method on your
class underneath the covers which is really assigned to the delegate),
but you have consolidated the code somewhat.

However, if you need to reuse that code (the ServerDataReceived
method) and using an anonymous delegate only leads to code duplication,
then what you have is the best way to handle it (and code it, with the
exception of declaring the delegate types).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Yehia A.Salam said:
Hello,

I am building a network application that make use of .Net Sockets, I
created a class that works like a server and fires an event when
anything arrives at the server, however I ran into some problems because
of the WPF threading model, my code is like:

//*******************************************
public Window1(){ ...
sConnection = new myserver();
sConnection.DateReceived += new
myserver.DataReceivedHandler(OnServerDataReceived);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceived),Re);

}

private void ServerDataReceived(MessageText Re) {
txtconsole.Text = txtconsole.Text + Re.name + ": " + Re.content;
}
//**********************************************

the code runs correctly but I am not satisfied with the structure, I
don't like that I must have at least two methods for modifying anything
in the UI, is there a better structure to do this, like putting
BeginInvoke in myServer Class for example and just assign the
ServerDataReceived event in Window1 class like I would normally do in
Windows Forms.

Thanks
Yehia A.Salam
 
Y

Yehia A.Salam

ok will do that, Thanks a lot

Nicholas Paldino said:
Yehia,

There is nothing you can do at the server level. As far as the server
is concerned, it gets a request and responds to it, it cares nothing (nor
should it, or could it) care about what thread the client called on in
it's app domain.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Yehia A.Salam said:
is that my only solution, can I solve this at the server class level?

Nicholas Paldino said:
Yehia,

Well, personally, you are taking on a good deal of typing overhead
which I don't feel you have to do. First, you don't have to declare the
delegate constructor when you assign event handlers, like so:

sConnection.DateReceived += OnServerDataReceived;

Also, you can declare your OnServerDataRecieved like so:

private void OnServerDataReceived(MessageText Re)
{
myserver.DataReceivedHandler handler =
delegate(MessageText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};


Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
handler, Re);
}

This way, you don't need to have another handler. Granted, the same
amount of jumps are made (the anonymous delegate creates a method on
your class underneath the covers which is really assigned to the
delegate), but you have consolidated the code somewhat.

However, if you need to reuse that code (the ServerDataReceived
method) and using an anonymous delegate only leads to code duplication,
then what you have is the best way to handle it (and code it, with the
exception of declaring the delegate types).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hello,

I am building a network application that make use of .Net Sockets, I
created a class that works like a server and fires an event when
anything arrives at the server, however I ran into some problems
because of the WPF threading model, my code is like:

//*******************************************
public Window1(){ ...
sConnection = new myserver();
sConnection.DateReceived += new
myserver.DataReceivedHandler(OnServerDataReceived);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceived),Re);

}

private void ServerDataReceived(MessageText Re) {
txtconsole.Text = txtconsole.Text + Re.name + ": " + Re.content;
}
//**********************************************

the code runs correctly but I am not satisfied with the structure, I
don't like that I must have at least two methods for modifying anything
in the UI, is there a better structure to do this, like putting
BeginInvoke in myServer Class for example and just assign the
ServerDataReceived event in Window1 class like I would normally do in
Windows Forms.

Thanks
Yehia A.Salam
 
Top