|
Well i don't mean network usage... i mean the CPU usage...
here for example one loop...
all my loops looks like that and so i don't understand, why i have such an CPU usage...
void connectionHolder()
{
while (cancel == false)
{
if (connection.GetStream().DataAvailable == true)
{
int maxBuffer = connection.ReceiveBufferSize;
Byte[] buffer = new Byte[maxBuffer];
int x = connection.GetStream().Read(buffer, 0, maxBuffer);
doAction(buffer);
}
Thread.Sleep(100);
}
}
|
|
|
|
|
Hi,
IMO you are wasting a lot of resources:
- what made you chose 100msec as your sampling period?
- why do you call connection.GetStream() every 100 msec?
- why do you call connection.ReceiveBufferSize every 100 msec?
- why do you allocate a new byte[] every 100 msec? can't you reuse it?
- why don't you use an event-driven mechanism, as provided by BeginRead (ignoring DataAvailable)?
softwarejaeger wrote: all my loops looks like that
and why do you need that many loops?
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Ok, some questions i really don't understand... :P
To you questions:
What made you choose 100msec as your sampling period?
Well a too long time, could give me a timeOut...
To question 2-4...
is there another way to do this? search for example for "TCP Client Server Tutorial c#" you'll find a lot of tutorials which works like this.
To question 5...
I didn't know that this is possible... have you an example for me? How the code should looks like?
To you question "why so many loops"
Well one for Listening for accepting connections and for each connection a Thread to get and process the incoming data...
|
|
|
|
|
Normally a server runs 1 thread to initiate client sessions, and 1 thread for each active client.
All of these threads perform blocking reads, so they are entirely event-driven, without the need of a polling loop, which is what your code is having.
FWIW: I think you're not ready to create decent server code; learn a programming language, object orientation, threading, and then create a client/server configuration.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Exactly that i told you one thread for listening connections,one for each connection for read/write/process data..
Wow..i shouldn't be able to create a client/server app?you talking to me like to a "newbie". Do we know us,that youre able to say something like that?
Sry i develop big applications with OoP! And a lot of clients, a few servers (which are running great,because they don't need to run asyc. Thats my first project which runs async and so i asked you how to solve that and you give such an answer? Well buts nothing suprising anymore, i knew a lot of developer with them you cannot talk normally, with them you're onl able to argue with...
So to something positive... Event-driven,i didnt knew that this is Possible with TCPClient. Great,the first test and it works fine. Now i can port it to my server.
Kind regards
|
|
|
|
|
So it works great now on the server
In 10 Minutes it was all developed, easier that i thought, i only had to change 5 lines or so, because i had a method in there i process my data.
|
|
|
|
|
No offense but it looks like you got that code by continuously evolving it until it worked.
The reason you are using CPU power for "nothing" is that you check DataAvailable. If you just read the bytes that you want it will wait anyway (if you use blocking reads you should set the socket.Blocking )
As it is now, it will continue to wait around 100ms (but sleep doesn't guarantee much) even if data is already available, which can't be good for the throughput.
This does mean, however, that you can't read "as much data as the other side has sent". The other side has to explicitly tell you how much it is going to send. I've never seen a stream based protocol that didn't do that though.
|
|
|
|
|
Hello Experts,
I have to set focus to particular cell when my form loaded.
Please Help!!
|
|
|
|
|
dataGridView1.CurrentCell = dataGridView1[1, 1];
Be sure that your datagridview had at least 1 column , 1 row .
I know nothing , I know nothing ...
|
|
|
|
|
It works!!
Thanx!!!
|
|
|
|
|
Hi all,
I want to write a sql generic function that will return numeric value.
I want to pass the whole sql query.
but it results error when executing
my user user defined function:
Create FUNCTION GetDoubleAmount(@strSQL varchar(500))
RETURNS decimal(18,6)
AS
BEGIN
return (select @strSQL)
END
calling with:
select dbo.GetDoubleAmount(
'SELECT SUM(CRAMT) As TotAmt From tblTLHistory'
)
results:
Msg 8115, Level 16, State 6, Line 1
Arithmetic overflow error converting varchar to data type numeric.
please help in asap
|
|
|
|
|
You need to do some more study, this is not going to work. The error in your current attempt is that you are using Select @StrSQL, this returns the string variable.
What you want to do is EXECUTE the string variable which I don't think you can do in a UDF. Besides it is bloody dangerous any time you exec a sql string (sql injection) and a UDF has no additional security.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Excute this code all at once from Query Anaylzer
DECLARE @val as decimal
SET @val = (SELECT SUM(CRAMT) As TotAmt From tblTLHistory)
select dbo.GetDoubleAmount(@val)
I know nothing , I know nothing ...
|
|
|
|
|
Hy,
I need to lunch a console application from a C# Windows Forms application. I have done something like that
Process serverProcess=new Process();
serverProcess.StartInfo.FileName = "wserver2";
serverProcess.StartInfo.UseShellExecute = true;
serverProcess.StartInfo.CreateNoWindow = false;
this.serverProcess.Start();
The code works fine, but I need a console to appear when the serverProcess is started and this process should appear in that console.
|
|
|
|
|
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "console.exe";
ps.Arguments = "";
ps.CreateNoWindow = false;
ps.UseShellExecute = false;
ps.RedirectStandardInput = true;
ps.RedirectStandardOutput = true;
ps.RedirectStandardError = true;
Process proc = new Process();
proc.StartInfo = ps;
proc.Start();
string line = proc.StandardOutput.ReadLine();
proc.Close();
I know nothing , I know nothing ...
|
|
|
|
|
nhack wrote: The code works fine
Is wserver2 a console application?
What do you mean by works fine as I assume you are asking the question because it isn't working fine!
Alan.
|
|
|
|
|
wserver2 is a console application and it works fine because i connect to it (it is a ssl server). My problem is that i want to see the output of the server in a new console window.
|
|
|
|
|
Hi,
I can't see anything wrong with the Process code. Are you missing any important command line switches? When the process is started from Windows Explorer does it display a console window?
Alan.
|
|
|
|
|
Yes, when the server is started from Explorer it displays a console window. If a lunch my Form application from the console the output is printed on the console, but what i want is to open a new console and to print the output from the server process on the new console.
|
|
|
|
|
I know that get and set is widely used in C#, e.g.
public class test
{
private int a = 2;
public int example
{
get {return a;}
set {a=value;}
}
}
above code is same as (I think)
public class test
{
public int a = 2;
}
but I believe that get/set should have more advantages, anyone can give me one hint for that?
thanks
|
|
|
|
|
Because they allow you to perform special handling when a value is set or got. They offload the validation checking away from main methods, keeping the main code 'clean'. Additionally, they let you provide calculated values, so that you don't have to call UpdateProperty() - you can just put the code in the get method. Also, you can properties to interface declarations
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
I used it in my router project ...
when ever I add router or computer I call the set method like this :
ObjectsCounter.Routers = 5;
here is my code :
class ObjectsCounter
{
private static int routers;
public static int Routers
{
set {
routers += value;
}
get {
return routers;
}
}
}
of course you can call it like that too :
ObjectsCounter.Routers += 5;
I know nothing , I know nothing ...
|
|
|
|
|
Alo,
Que esta' la differencia entre algun y alguno. O esta' la differencia???
Chow
Troy
|
|
|
|
|
Properties have a special use if your making a custom control with custom properties.
#region Color HighlightColor
private Color highlightColor = Color.Yellow;
[DefaultValue(typeof(Color), "Yellow")]
public Color HighlightColor
{
get { return highlightColor; }
set
{
if (highlightColor != value)
{
highlightColor = value;
this.Invalidate();
}
}
}
public bool ShouldSerializeHighlightColor()
{
return highlightColor != Color.Yellow;
}
public void ResetHighlightColor()
{
HighlightColor = Color.Yellow;
}
#endregion
here's one from a control i'm working on... the attribute for defaultvalue and the ShouldSerialize##"Property Name" Reset##"Property Name" functions are specially called by the visual studio ide... so when you have your control on the designer, and you play with a setting... you can do stuff like right click it and hit "Reset"... also the shouldSerialze function is checked for if the designer needs to save that setting to the .designer.cs file. This way the designer doesnt have to serialze a dozen default values... which probably improves performance or whatnot... looks nicer to when you gotta go in the designer file... i forget if the defaultvalue attribute means so when you type a value in the propertygrid, that if its the default value in that attribute, that the ide shows regular font or bold, that you changed it from default,...
So properties are nice if your making a custom control. Otherwise, properties are handy for controlling what comes in and leaves your class...
like have a getOnly type property like:
public int Something {
get { return something; }
}
or here's checking input:
public string SomeOtherThing {
get { return someOtherThing; }
set
{
if (!String.IsNullOrEmpty(value))
someOtherThing = value;
}
}
.... so i see your point... i mean in C++ the method was with functions to do the same job. So the only advantage of a property is that the IDE can draw a nice little icon when it detects one in your class... like i mean intellisense... other then that, and all programmers already expect to play with properties to set settings, that they have no purpose. Practically a bloated feature if we just look at C# as the language... but ya, their good for the vs.net designer when plopping controls on a form :P
|
|
|
|
|