467,145 Members | 1,021 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,145 developers. It's quick & easy.

WPF Threading Model

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.Di spatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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

May 25 '07 #1
  • viewed: 1995
Share:
5 Replies
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.Di spatcherPriority.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]
- mv*@spam.guard.caspershouse.com
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:8F**********************************@microsof t.com...
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.Di spatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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

May 25 '07 #2
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:8F**********************************@microsof t.com...
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.Di spatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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.

May 25 '07 #3
is that my only solution, can I solve this at the server class level?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:#f**************@TK2MSFTNGP03.phx.gbl...
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.Di spatcherPriority.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]
- mv*@spam.guard.caspershouse.com
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:8F**********************************@microsof t.com...
>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.D ispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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

May 25 '07 #4
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]
- mv*@spam.guard.caspershouse.com

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:32**********************************@microsof t.com...
is that my only solution, can I solve this at the server class level?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:#f**************@TK2MSFTNGP03.phx.gbl...
>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.D ispatcherPriority.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]
- mv*@spam.guard.caspershouse.com
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:8F**********************************@microso ft.com...
>>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(OnServerDataReceive d);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading. DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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


May 25 '07 #5
ok will do that, Thanks a lot

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:#X**************@TK2MSFTNGP04.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:32**********************************@microsof t.com...
>is that my only solution, can I solve this at the server class level?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:#f**************@TK2MSFTNGP03.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com
"Yehia A.Salam" <ye*****@hotmail.comwrote in message
news:8F**********************************@micros oft.com...
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(OnServerDataReceiv ed);
sConnection.StartServer();
}

private void OnServerDataReceived(MessageText Re) {
Dispatcher.BeginInvoke(System.Windows.Threading .DispatcherPriority.Normal,
new myserver.DataReceivedHandler(this.ServerDataReceiv ed),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


May 25 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

65 posts views Thread by Anthony_Barker | last post: by
19 posts views Thread by Jane Austine | last post: by
reply views Thread by James R. Saker Jr. | last post: by
12 posts views Thread by Gurpreet Sachdeva | last post: by
1 post views Thread by Dave | last post: by
8 posts views Thread by Mahesh Devjibhai Dhola [MVP] | last post: by
reply views Thread by =?Utf-8?B?VGlt?= | last post: by
5 posts views Thread by skunkwerk | last post: by
126 posts views Thread by Dann Corbit | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.