Hi All,
I am developing a multithreaded application in c# on vs 2005 (compact
framework 2.0). I want to have a socket server in a background thread
and want to continue with the windows form code in the main thread.
In my main windows form (called 'default'), I have instantiated a class
called 'Interop'. The 'interop' class has a function called
'instantiateSoc ketServer' that instantiates the SocketServer class. I
call the 'instantiateSoc ketServer' function on a background thread in
the 'default' form.
------------code in default form------------------------
default()
{
interop = new Interop();
Thread backgroundThrea d = new Thread(interop. initiateSocketS erver);
backgroundThrea d.IsBackground = true;
backgroundThrea d.Start();
}
------------code in default form------------------------
------------code in Interop class------------------------
public void initiateSocketS erver()
{
SocketServer socket = new SocketServer();
}
------------code in Interop class------------------------
------------code in SocketServer class------------------------
public SocketServer()
{
StartListening( )
private StartListening( )
{
PHostEntry ipHostInfo = Dns.Resolve(Dns .GetHostName()) ;
IPAddress ipAddress = ipHostInfo.Addr essList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAd dress, 11000);
Socket listener = new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
listener.Bind(l ocalEndPoint);
listener.Listen (1000);
Socket handler = listener.Accept ();
}
}
------------code in SocketServer class------------------------
When I run my application, the StartListening( ) function gets executed
at the line:
Thread backgroundThrea d = new Thread(interop. initiateSocketS erver);
When it comes to backgroundThrea d.Start();, it tries to execute the
StartListening( ) function again and I get a SocketException that says,
"only one usage of each socket address is normally permitted".
Can someone help?
Thanks,
Krupa
PS: Also, is there a way where I can pass an object instead of a method
to the backgroundThrea d? 1 1342
Thanks Vadym Stetsyak! I realised that asynchronous socket
communication was more suitable for my application. That way I don't
have to bother creating any background threads.
It's wrking now!
Thanks,
Krupa
Vadym Stetsyak wrote: Hello, Krupa!
K> ------------code in default form------------------------ K> default() K> { K> interop = new Interop(); K> Thread backgroundThrea d = new Thread(interop. initiateSocketS erver); K> backgroundThrea d.IsBackground = true; K> backgroundThrea d.Start(); K> } K> ------------code in default form------------------------
K> ------------code in Interop class------------------------ K> public void initiateSocketS erver() K> { K> SocketServer socket = new SocketServer(); K> } K> ------------code in Interop class------------------------
K> ------------code in SocketServer class------------------------ K> public SocketServer() K> { K> StartListening( )
Is it mistake here?
K> private StartListening( ) K> { K> PHostEntry ipHostInfo = Dns.Resolve(Dns .GetHostName()) ; K> IPAddress ipAddress = ipHostInfo.Addr essList[0]; K> IPEndPoint localEndPoint = new IPEndPoint(ipAd dress, 11000);
K> Socket listener = new Socket(AddressF amily.InterNetw ork, K> SocketType.Stre am, ProtocolType.Tc p);
K> listener.Bind(l ocalEndPoint); K> listener.Listen (1000); K> Socket handler = listener.Accept (); K> } K> } K> ------------code in SocketServer class------------------------
Try to remove StartListening( ) call from SocketServer class and put into thread method like this public void initiateSocketS erver() { SocketServer socket = new SocketServer(); socket .StartListening (); }
StartListening has to be public...
-- Regards, Vadym Stetsyak www: http://vadmyst.blogspot.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: pradyumna |
last post by:
In Project settins - C/C++ - Code Generation,
what is the difference between the option
"Multithreaded" and "Multithreaded DLL".
I understand that on selecting multithreaded option,
single and multithreaded applications can both use that
dll, but what about multithreaded DLL option.
Thanks
|
by: daniel.bron |
last post by:
I'm maintaining a C++ application written by a developer who has now
left. The app is a multithreaded client/server app for financial data.
My compiler is MS visual C++ 6.0.
I'm a C++ neophyte. I took some C (not C++) courses in college, this is
the only production C++ code I've worked with.
I'm trying to add some logging, and having problems.
My logging scheme is to have a global ofstream, which I initialize in a
|
by: nrhayyal |
last post by:
Hi All,
thanks for reading this post.
just wanted to know about the ratio of threads and processors.
i am working on c++ on AIX5.2 platform.
my c++ program are multithreaded programs.
In a multithreaded application,should no of processors be equal to no
of threads ?
if we set the thread_scope to system( meaning 1 kernel thread to 1
|
by: groups |
last post by:
Hi all,
I've recently ported a rather large C application to run multithreaded.
A few functions have seriously deteriorated in performance, in
particular when accessing a rather large global array, that contains
information that is shared among threads.
Any idea, why the lines accessing this global array now take about 50x
longer in the multithreaded application?
|
by: |
last post by:
Is it possible to have just a multithreaded sub procedure? What I need is a
timer time_elapsed event (2 sec interval) send params to a sub that is
multithreaded. I have a COM component used to send messages,faxes, etc..
The COM com component is licensed for 6 ports. I have an app that need to
send messages/faxes very frequently (seconds) and to many, many people.
What I want to do is have a sub that has 6 threads to send thse messages...
| |
by: Jake K |
last post by:
I have a multithreaded application that I now want to convert into a Windows
Service. Does application.run work in a windows service? Are there things
to take into consideration when creating a multithreaded windows service as
opposed to a multithreaded windows forms application?
E.G.
namespace whatever
{
|
by: Andreas Schmitt |
last post by:
I am workin on a 2 part project right now. The first part is a DLL, the
second part a normal exe using that DLL.
When I use the VS2005 standard setting for compiling with the
Multithreaded-DLL runtime library (compiler option /MD )everything works
fine on my PC. But when I try to run the thing on a friends PC or my
laptop I get:
"This application has failed to start because the application
configuration is incorrect.
|
by: cj |
last post by:
I have a 2005 TCP/IP server that creates a new thread to handle each
incoming TCP/IP request. Once the request has been answered by the
thread the TCP/IP socket is disconnected and the sub/thread ends. This
program originally written in 2003 and I rewrote it in 2005 earlier this
year. I don't know how long this has been going on but we just noticed
today that the program was using over a gig of virtual memory. Real
memory was quite...
|
by: Dan Bass |
last post by:
Using:
.Net (happens to be VB but same with C#) 2.0
SQLXML4
SQL Server 2005
I've got a multithreaded .Net application that uses the SQLXmlBulkLoad call
and I'm not convinced the COM object is being released properly. The code is
also called often and I'm not sure if I should be creating the object each
time, or reusing it. I've tried reusing it (one object for all threads with
a lock or one object per thread), but because of the...
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |