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 5 2167
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
"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.
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
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
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Anthony_Barker |
last post by:
I have been reading a book about the evolution of the Basic
programming language. The author states that Basic - particularly
Microsoft's version is full of compromises which crept in along the...
|
by: Jane Austine |
last post by:
As far as I know python's threading module models after Java's.
However, I can't find something equivalent to Java's interrupt and
isInterrupted methods, along with InterruptedException....
|
by: James R. Saker Jr. |
last post by:
I've got a: "start server thread > Queue object, start server thread <>
Queue object, start parsing client < Queue object" application that's
got me puzzled. Probably an easy threads issue, but...
|
by: Gurpreet Sachdeva |
last post by:
I have written a code to figure out the difference in excecution time
of a func before and after using threading...
#!/usr/bin/env python
import threading
import time
loops =
|
by: Dave |
last post by:
VB is relatively new to our company and I am putting together some
general guidelines for people to follow when deploying their
applications. On the General Tab of the Project Properties dialog,...
|
by: Mahesh Devjibhai Dhola [MVP] |
last post by:
We are building Chat like application using Forms and as a result our
programming is becoming complicated to display messages received on
different threads in the chat window (due to STA...
|
by: Carly |
last post by:
Hi,
I am not sure I understand what is the meaning of "Execution of
asynchronous code is not supported in Windows Forms threading model" -
since I am able to create threads and run asynchronous...
|
by: =?Utf-8?B?VGlt?= |
last post by:
I have written both a client (SoapClient) and server (SoapServer) application
using WSE 3.0 with the TCP transport. These are completely standalone
applications (the Server runs as a Windows...
|
by: skunkwerk |
last post by:
i'm getting the wrong output for the 'title' attributes for this
data. the queue holds a data structure (item name, position, and list
to store results in). each thread takes in an item name and...
|
by: Dann Corbit |
last post by:
Rather than create a new way of doing things:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html
why not just pick up ACE into the existing standard:...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |