|
Ok, ive considered it - now what? Did you have a question?
Edit: You asked ther same question 4 hours ago and said the solution you were given was working
|
|
|
|
|
Look u r totally in confusion the question which i posted first is different from the thing i posted now....
Sorry to say this friend
The fact is u cant understand the question...
Dont make funny on others think first u r fit to do that...
|
|
|
|
|
Bala K wrote: The fact is u cant understand the question...
Where is the question?
Bala K wrote: Assume 3 or 5 textbox which will be filled by user at runtime (v dont know how many he will fill)
and these values should assigned to radio button text which will be created dynamically.
Hmm... yes i assumed it. Goody goody?
-------------------------------------------
It's code that drives you - Shyam
|
|
|
|
|
|
Iterate through the controls, test each control is a TextBox and build a string from those that are.
Rough example:
string BuildRadioText()
{
StringBuilder textBuilder = new StringBuilder();
foreach (Control control in Controls)
{
if (control is TextBox)
if (Text.Length > 0)
{
if (textBuilder.Length > 0)
textBuilder.Append(", ");
textBuilder.Append(control.Text);
}
}
return textBuilder.ToString();
}
RadioButton dynamicRadio = new RadioButton();
dynamicRadio.Text = BuildRadioText();
Controls.Add(dynamicRadio);
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
modified on Thursday, November 6, 2008 10:37 AM
|
|
|
|
|
I'm developing an application in which multiple clients exchange data on a TCP link with a server using a custom protocol. I've chosen the asynchronous approach to implement the server.
Below I've attached an excerpt of my code, showing the points discussed in my questions. I have several doubts about correct asynchronous callback usage, and I could not find anything in the docs:
1) Suppose you have several clients connected. Do all the callbacks (OnClientConnect , OnClientRead ...) "live" in the same thread? That is, while I'm inside OnClientRead to serve one of the clients, can my code be interrupted by an OnClientRead from another client?
I performed a simple investigation, and it seems that OnClientRead for different clients are called from the same thread. So, it seems that OnClientRead execution never gets interrupted. Am I right? Is this always true? If so, I think I can safely remove all the lock statements for clientList access.
2) Is it correct to always launch a new AsyncCallback every time? e.g. new AsyncCallback(OnClientRead) inside OnClientRead . Does this waste resources? Should I keep a different AsyncCallback for each client and reuse it?
Regards,
Andrea
class ClientState
{
}
class NetworkServer
{
private List<clientstate> clientList;
private object lockObject = new object();
public void Start(int backlog)
{
clientList = new List<clientstate>();
serverSocket.Listen(backlog);
serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
private void OnClientConnect(IAsyncResult state)
{
Socket clientSocket = serverSocket.EndAccept(state);
ClientState clientState = new ClientState();
...
lock(lockObject)
{
clientList.Add(clientState);
}
clientSocket.BeginReceive(clientState.Buffer,
0,
clientState.Buffer.Length,
SocketFlags.None,
new AsyncCallback(OnClientRead),
clientState);
serverSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
private void OnClientRead(IAsyncResult state)
{
SocketError socketError;
int numBytes = clientSocket.EndReceive(state, out socketError);
if ((numBytes == 0) || (socketError != SocketError.Success))
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
lock(lockObject)
{
clientList.Remove(clientState);
}
}
else
{
...
clientSocket.BeginReceive(clientState.Buffer,
0,
clientState.Buffer.Length,
SocketFlags.None,
new AsyncCallback(OnClientRead),
clientState);
}
}
}</clientstate></clientstate>
|
|
|
|
|
Hi again Andrea. That's looking good.
Metal76 wrote: 1) Suppose you have several clients connected. Do all the callbacks (OnClientConnect, OnClientRead...) "live" in the same thread? That is, while I'm inside OnClientRead to serve one of the clients, can my code be interrupted by an OnClientRead from another client?
I performed a simple investigation, and it seems that OnClientRead for different clients are called from the same thread. So, it seems that OnClientRead execution never gets interrupted. Am I right? Is this always true? If so, I think I can safely remove all the lock statements for clientList access.
The callbacks happen on a thread pool thread. This means that each callback owns it's thread while it is executing and will not be 'interrupted'. However, the thread pool has many threads! Each callback is handled separately, so it may or may not execute on the same thread pool thread each time.
If multiple clients are active at the same time, you may have many threads from the pool, each executing OnClientConnect or OnClientRead for a particular client. This means that you do have to protect your shared state ( clientList ) by using a lock, just as you have done.
Metal76 wrote: 2) Is it correct to always launch a new AsyncCallback every time? e.g. new AsyncCallback(OnClientRead) inside OnClientRead. Does this waste resources? Should I keep a different AsyncCallback for each client and reuse it?
Yes, you have to start a new BeginXXX call each time to continue accepting clients and receiving data. The AsyncCallback is just a delegate, so doesn't waste much. You can declare an instance in your class, instantiate it in a constructor, and reuse it each time. It doesn't really matter, though.
Just as a matter of style, I would put the calls to BeginAccept and BeginReceive in a private method each, just so you don't have two copies of each call.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Hi Nick! By the way, I also considered the ReceiveAsync pattern you suggested in your last post, but as a newbie I found it more difficult to approach I'll consider it for future enhancements.
Nick Butler wrote: Just as a matter of style, I would put the calls to BeginAccept and BeginReceive in a private method each, just so you don't have two copies of each call.
Do you mean something like the following?
private void StartReceiving(ClientState clientState)
{
clientSocket.BeginReceive(clientState.Buffer,
0,
clientState.Buffer.Length,
SocketFlags.None,
new AsyncCallback(OnClientRead),
clientState);
}
A couple of additional points (just to increase the amount of beer pints I already owe you):
1) Which is the correct way to gracefully shutdown the server app? I know for sure that I have to close all the client sockets; are there any other resources to shutdown in the async pattern I'm following?
2) I noticed that the ClientState instance I create inside OnClientConnect for each of the clients seems to be internally maintained by the async pattern, without any intervention on my side.
That is: when OnClientRead fires from one of the clients, the input IAsyncResult state is the right ClientState instance associated with that client.
So, it seems to me that keeping a clientList is only useful when I want to shutdown the server (so that I can dispose of each client socket) and if I want to offer to the users of my class the possibility to perform actions on connected clients (e.g. check state of client A, forcibly shutdown connection with client B etc.).
Is this right?
Thanks in advance and best regards,
Andrea
|
|
|
|
|
Metal76 wrote: Do you mean something like the following?
Yes, that's it.
Metal76 wrote: 1) Which is the correct way to gracefully shutdown the server app? I know for sure that I have to close all the client sockets; are there any other resources to shutdown in the async pattern I'm following?
If your protocol doesn't have any command messages to send when closing, all you have to do is call Shutdown and then Dispose .
Metal76 wrote: So, it seems to me that keeping a clientList is only useful when I want to shutdown the server (so that I can dispose of each client socket) and if I want to offer to the users of my class the possibility to perform actions on connected clients (e.g. check state of client A, forcibly shutdown connection with client B etc.).
Is this right?
Yes, that's right.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks Nick! As usual, very useful info.
Bye,
Andrea
|
|
|
|
|
Help me please!
I want columns is loaded from database. It is displayed depend in database
I don't speak english good.
sdbfkdskjfdkksdbfkdsjbfdksbf
|
|
|
|
|
Hi,
So a short little search on Google provided me with a couple of hits, but this one is what you are looking for:
See here[^]
Regards
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
I assume that you can load your database data in a datagridview but not in crystal reports!!!
Ok, you will have to create a scheme dataset in design time that is the exact match to the scheme of the data you are loading from the database.
As you noticed the datagridview load everthing regardeless the data scheme! but crystal reports is diferent!!!
scheme from dataset created in design time
id(int), f_name(string), l_name(string), birth(datetime), job(string)
scheme from sql database store procedure
select id(int), f_name(string), l_name(string), birth(datetime), job(string) from ...
Got it?! it´s really simply
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
Any one has code sample about crystal report dynamic columns (C#)?
header in crystal report is loaded from database (command: select * from tbl...).
Thanks!
khuongkd@gmail.com
|
|
|
|
|
i really dont know where you are stuck, i dont know nothing about dynamic columns i just get data from sql to crystal.
Good luck
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
i countered this error when debug my program, so i switch off the detection. My program works fine. But is my program quality affected by switch it off?
thanks
|
|
|
|
|
This MDA is triggered when your main STA thread stops pumping messages. If you are not doing long work on your UI thread and this only happens when you are stepping through the debugger, then you can safely ignore it.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
When you get an error, switching of the checking for that error isn't normally the best solution. I would recommend tracking down the cause of the error and eliminating/handling it.
This MSDN page[^] may help.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Is the Function keys(F1,F2....) will work on web application(at run time)?if yes please can i know how it works?
Thanks
|
|
|
|
|
praneetham wrote: web application
Wrong place. Try ASP.NET or web forums.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
...
Blog link to be reinstated at a later date.
|
|
|
|
|
I have this code developed to run a bat file. the code is set to a buttonclick event......bat file is crating correctly ..when i double click on it it works fine..but the bat is not executing from the program..cant figure out what the reason..your help is appriciated..
string text = "compdld -d -i\"C:\\softpay\\ver22\\output\\cpac\\ads\\files\\softpayx.dld\" -w\"C:\\HSBMFC\" -momni-5150 -aHSBMFC -tTID01 -gl -fl";
File.WriteAllText(@"C:\COMPDLD\a.bat", text);
ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = @"C:\COMPDLD\a.bat";
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = psi;
p.EnableRaisingEvents = true;
p.Start();
while (!p.HasExited)
{
System.Threading.Thread.Sleep(1000);
}
if (p.ExitCode != 0)
{
}
|
|
|
|
|
Good morning.
May be you should also specify the working directory.
or try to launch cmd.exe with your batch file as argument (same command as the one created when you make a shorcut on a batch file:
C:\WINDOWS\system32\cmd.exe /k mybatch.bat.
Regards.
|
|
|
|
|
Get the standard output stream from the process
Natza Mitzi
|
|
|
|
|
You actually should do both (specify the correct working folder and using cmd.exe).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|