473,569 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Dat eReceived += new
myserver.DataRe ceivedHandler(O nServerDataRece ived);
sConnection.Sta rtServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.Begi nInvoke(System. Windows.Threadi ng.DispatcherPr iority.Normal,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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 ServerDataRecei ved event in Window1
class like I would normally do in Windows Forms.

Thanks
Yehia A.Salam

May 25 '07 #1
5 2177
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.Dat eReceived += OnServerDataRec eived;

Also, you can declare your OnServerDataRec ieved like so:

private void OnServerDataRec eived(MessageTe xt Re)
{
myserver.DataRe ceivedHandler handler =
delegate(Messag eText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " + mt.content;
};

Dispatcher.Begi nInvoke(System. Windows.Threadi ng.DispatcherPr iority.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 ServerDataRecei ved 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.co m
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:8F******** *************** ***********@mic rosoft.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.Dat eReceived += new
myserver.DataRe ceivedHandler(O nServerDataRece ived);
sConnection.Sta rtServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.Begi nInvoke(System. Windows.Threadi ng.DispatcherPr iority.Normal,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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 ServerDataRecei ved event in
Window1 class like I would normally do in Windows Forms.

Thanks
Yehia A.Salam

May 25 '07 #2
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:8F******** *************** ***********@mic rosoft.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.Dat eReceived += new
myserver.DataRe ceivedHandler(O nServerDataRece ived);
sConnection.Sta rtServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.Begi nInvoke(System. Windows.Threadi ng.DispatcherPr iority.Normal,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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 ServerDataRecei ved event in
Window1 class like I would normally do in Windows Forms.

Thanks
Yehia A.Salam

Using an anon. delegate...

Dispatcher.Begi nInvoke(Dispatc herPriority.Nor mal,
(EventHandler)d elegate
{
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.c omwrote in
message news:#f******** ******@TK2MSFTN GP03.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.Dat eReceived += OnServerDataRec eived;

Also, you can declare your OnServerDataRec ieved like so:

private void OnServerDataRec eived(MessageTe xt Re)
{
myserver.DataRe ceivedHandler handler =
delegate(Messag eText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};
Dispatcher.Begi nInvoke(System. Windows.Threadi ng.DispatcherPr iority.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 ServerDataRecei ved 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.co m
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:8F******** *************** ***********@mic rosoft.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.Da teReceived += new
myserver.DataR eceivedHandler( OnServerDataRec eived);
sConnection.St artServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.Beg inInvoke(System .Windows.Thread ing.DispatcherP riority.Normal,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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
ServerDataRece ived 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.co m

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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:#f******** ******@TK2MSFTN GP03.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.Da teReceived += OnServerDataRec eived;

Also, you can declare your OnServerDataRec ieved like so:

private void OnServerDataRec eived(MessageTe xt Re)
{
myserver.DataRe ceivedHandler handler =
delegate(Messag eText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};
Dispatcher.Beg inInvoke(System .Windows.Thread ing.DispatcherP riority.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 ServerDataRecei ved
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.co m
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:8F******* *************** ************@mi crosoft.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.D ateReceived += new
myserver.Data ReceivedHandler (OnServerDataRe ceived);
sConnection.S tartServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.Be ginInvoke(Syste m.Windows.Threa ding.Dispatcher Priority.Normal ,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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
ServerDataRec eived 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.c omwrote in
message news:#X******** ******@TK2MSFTN GP04.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.co m

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

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote
in message news:#f******** ******@TK2MSFTN GP03.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.D ateReceived += OnServerDataRec eived;

Also, you can declare your OnServerDataRec ieved like so:

private void OnServerDataRec eived(MessageTe xt Re)
{
myserver.DataRe ceivedHandler handler =
delegate(Messag eText mt)
{
txtconsole.Text = txtconsole.Text + mt.name + ": " +
mt.content;
};
Dispatcher.Be ginInvoke(Syste m.Windows.Threa ding.Dispatcher Priority.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 ServerDataRecei ved
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.co m
"Yehia A.Salam" <ye*****@hotmai l.comwrote in message
news:8F****** *************** *************@m icrosoft.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(){ ...
sConnectio n = new myserver();
sConnection. DateReceived += new
myserver.Dat aReceivedHandle r(OnServerDataR eceived);
sConnection. StartServer();
}

private void OnServerDataRec eived(MessageTe xt Re) {
Dispatcher.B eginInvoke(Syst em.Windows.Thre ading.Dispatche rPriority.Norma l,
new myserver.DataRe ceivedHandler(t his.ServerDataR eceived),Re);

}

private void ServerDataRecei ved(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
BeginInvok e in myServer Class for example and just assign the
ServerDataRe ceived event in Window1 class like I would normally do in
Windows Forms.

Thanks
Yehia A.Salam


May 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
6684
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 language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace,...
19
6449
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. "somethread.interrupt()" will wake somethread up when it's in sleeping/waiting state. Is there any way of doing this with python's thread? I suppose thread...
0
1858
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 after digging thru Programming Python and Python Recipes sections on Threading class and running thru the examples, I'm still missing something. My...
12
1727
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 =
1
1906
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, there is a threading model section. Is there a general rule of thumb I can put in my guidelines that people can use to determine: 1. When to use...
8
1891
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 requirements of Forms). Is there a way to build Chat like application WITHOUT using FORM, so that one can use free threading model (The link...
0
1014
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 code if I create a Windows form application. Also what would be the diferrence between the threading model of Windows forms and ASP.NET. Please...
0
1198
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 Service). The one thing I am struggling with is how to adequately add multi-threading to the server and was wondering if anyone can offer help and...
5
2515
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 queries a database for various attributes. from the debug statements the item names are being retrieved correctly, but the attributes returned are...
126
6649
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: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have...
0
7698
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8122
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7970
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.