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 2109
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: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
| |