|
If you're "looking" for faster code - you won't find it. It's something that needs to be written.
You plot the graph yourself, and update the graph with new info instead of redrawing it completely.
Human eyes aren't realtime either; you'd need 24 frames/second to get it to look convincingly realtime. Although Windows might delay your app (one a single-core machine, a virusscanner and some automatic updates would do the trick), it should be doable to update a picture in a reasonable time.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
there is one another question: why can Matlab depict these charts so faster than c# ?
|
|
|
|
|
I have used this tutorial to make a chat:
http://www.youtube.com/watch?v=BDVfpPq3weo[^]
It works on the same computer but not between two.
The problem seem so be that I need more than one thread.
I get Cross-thread operation not valid.
|
|
|
|
|
No, the problem is the reverse of that: "Cross-thread operation not valid" means that you are executing code on one thread that can only be executed on a different thread: normally, this occurs when you try to update a control from a different thread to that from which it was created (which must be the UI thread), either in a BackgoundWorker, a Thread instance or in the handler of a communications class that uses threading to handle it's events (the SerialPort does this for example).
Check your code: you may just have to start invoking the control instead of accessing it directly. For example:
private void AddNewTab(string tabName)
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(delegate { AddNewTab(tabName); }));
}
else
{
TabPage tp = new TabPage(tabName);
myTabControl.TabPages.Add(tp);
}
}
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Not sure I understand...Could you show how to change my code?
This is my code:
private void Form1_Load(object sender, EventArgs e)
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
txtLocalIP.Text = GetLocalIP();
txtRemoteIP.Text = GetLocalIP();
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
return ip.ToString();
}
return "127.0.0.1";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData);
listMessage.Items.Add("Friend: " + receivedMessage);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
epLocal = new IPEndPoint(IPAddress.Parse(txtLocalIP.Text), Convert.ToInt32(txtLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(txtRemoteIP.Text), Convert.ToInt32(txtRemotePort.Text));
sck.Connect(epRemote);
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
private void btnSend_Click(object sender, EventArgs e)
{
ASCIIEncoding aEncoding = new ASCIIEncoding();
byte[] sendingMessage = new byte[1500];
sendingMessage = aEncoding.GetBytes(txtMessage.Text);
sck.Send(sendingMessage);
listMessage.Items.Add("Me: " + txtMessage.Text);
txtMessage.Text = "";
}
|
|
|
|
|
Where do you get the error? Which line?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Ok... The other guy whose thread I can´t se now wrote:
I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use
InvokeRequired
and
Invoke
to get back to the UI thread when you want to access the form's controls.
Invoke((Action<object>)listMessage.Items.Add, "Friend: " + receivedMessage);
I Think he was right about the line. Tried to do change the line accordingly but got an error:
int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type
|
|
|
|
|
larsp777 wrote: int System.Windows.Forms.ListBox.ObjectCollection.Add(object)' has the wrong return type
Whoops - didn't notice that the method returned an int . The delegate type will need to be a Func<object, int> instead of an Action<object> .
Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Thanks, Richard, it worked!
Well, thank you both for helping me out!
|
|
|
|
|
Do you know if there is a simple way for a third person can listen in what the others are Writing. (only has to listen to the sender)
I am making an application where you should be able to simulate someone eavesdropping the conversation.
|
|
|
|
|
If all the computers are on the same LAN, you could use the broadcast address so that any computer can pick up the messages:
Broadcasting Using Socket-Oriented Approach[^]
If they're on different networks, or you don't want the overhead associated with broadcasting, then you'll need to use multicasting:
IP Multicasting in C#[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks! It will probebly be on the same LAN.
|
|
|
|
|
The error occurs when I try to send a message. It occurs on both computers.
When I send from one computer the error shows on the other.
|
|
|
|
|
larsp777 wrote: listMessage.Items.Add("Friend: " + receivedMessage);
I'm guessing that's the line that's causing the problem. The callback method will be called on a background thread; you will need to use InvokeRequired and Invoke to get back to the UI thread when you want to access the form's controls.
Invoke((Func<object, int>)listMessage.Items.Add, "Friend: " + receivedMessage);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you both!
I think that is the problem. Will check it out and get back.
|
|
|
|
|
hey OG, please enlighten me more on this.
|
|
|
|
|
Sorry Rahul, but it's a huge subject - I couldn't begin to do it justice in a small text box!
There are some good tutorials out there which explain threading and the UI pretty well:
http://stuff.seans.com/2009/05/21/net-basics-do-work-in-background-thread-to-keep-gui-responsive/[^] (Backgound and why to thread)
http://www.dreamincode.net/forums/topic/246911-c%23-multi-threading-in-a-gui-environment/[^] (Fairly advanced)
But basically when you start to use multiple threads you can't touch any controls, except from the thread that created them - which is called the UI thread (for User Interface) and is the original thread the form started on. If you try, you will get a "cross-threading" error telling you not to do that. The only way to get round it is to Invoke the control - which basically requests the UI thread to do the work for you.
Have a look at the BackgroundWorker thread - it provides a way to update the display without invoking via the ProgressChanged event, which is executed on the original thread.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Can you mark a question as solved.
You really solved it Richard but Griffin also helped.
I am working on a game for XNA and need both computers to have access to the same "board".
Do you know anything about that?
|
|
|
|
|
I am reading file and after validation i am saving data in properties
Now i was hoping to use properties for validation and then save if validation succeed for e.g.
const string AN2Mandatory = @"[a-zA-Z@$^�-9\s\S]{2}";
const string AN70Optional = @"[a-zA-Z@$^�-9\s\S]{70}"; these are for validations
and these are properties
public string VersionNumber { get; set; }
public string ClientName { get; set; }
this is sample logic
Match m = Regex.Match(Line, AN70Optional);
VersionNumber = m.Value;
can i apply attributes or something so that while puting value in property my validation is evaluated first and then fill property. instead of validating separately and then storing it
|
|
|
|
|
I can offer you two ways...
1 - Put the validation into the property setter (simple).
2 - Write (or find) an attribute that gets the regex as parameter and validates the property decorated with (much powerful).
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
i am also trying the 2 approach and for that i have to write a custom attribute.
|
|
|
|
|
Great - when you ready made it into an article to share with all!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
This is a win form application
i have created a class
public class ValidateProperty : Attribute
{
string _pattern;
bool _mandatory;
public ValidateProperty(string Pattern, bool Mandatory = false)
{
_pattern = Pattern;
_mandatory = Mandatory;
}
public override bool Match(object obj)
{
Match m = Regex.Match((string)obj, _pattern);
if (_mandatory == true)
{
if (string.IsNullOrWhiteSpace(m.Value)) return false;
else
{
return true;
}
}
else
return true;
}
}
and then applied on property
[ValidateProperty(@"[a-zA-Z@$^-9\s\S]{70}",true)]
public string ClientName { get; set; }
but it is not validating when i put a string with less than 70 char. even in debug mode code is not going to match function
|
|
|
|
|
{70} doesn't mean exactly 70, but up to 70!!!
The correct format of {} is {m,n} where m is from and n is to...
When you use only one number it interpreted as {0,n}.
For exactly 70 characters try {70,70}!!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|