|
geekfromindia wrote: What are we trying to achieve indicating a variable as volatile?? How CLR will treat it differently??
When you modify a variable there is often a version of it cached in the processor. Applying the volitile keyword indicates that this optimization should not be used and it must be updated in memory as multiple threads are accessing it. If you update the variable in the processor cache and another thread requests it from memory the the value will be wrong.
|
|
|
|
|
how does that differ from variable in single threaded environment?? is information regarding optimization is available to only a single thread?
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
<muse>
Sometimes I wonder what will happen to the next generation of software engineers .. I grew up poking memory locations to alter my Atari's OS and progressed to debugging accidental pointer overruns in C. What happens when there's no concept of the architecture on which our beautifully abstract code runs I wonder?
</muse>
I'm largely language agnostic
After a while they all bug me
|
|
|
|
|
thanks for your thought MidwestLimey...
could you please shed some light on this topic so just to help your next generation building some robust applications..
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
Sorry just logged in, but it looks as if your question was answered. I learned most of what I know by playing around and asking questions - still amazed at how little I learned at uni in hindsight
I'm largely language agnostic
After a while they all bug me
|
|
|
|
|
As long as the world turns there will always be geeks - computer/software engineering will only move forward. .
Mark
|
|
|
|
|
MarkBrock wrote: As long as the world turns there will always be geeks - computer/software engineering will only move forward. .
Right said Mark...
Colin Angus Mackay wrote: If you update the variable in the processor cache and another thread requests it from memory the the value will be wrong.
My Ques: (rephrasing it a bit) - If we say that a thread1 reads a variable from memory and that is stored in CPU cache for optimization. now thread1 accesses that variable from cache all the time...why doesn't other threads access that variable from cache instead of going to memory.
So it seems that here optimization is being done by thread1 so it cannot be accessed from thread2 which will always access variable again from memory causing all the problem.
here's an example on volatile for more details:
int i = 0;
loop(i=0 to i=100)
{
//do something
}
here if compiler optimizes the variable before loop, it will have value 0 in memory and loop will only update value in cache.
Now if any other method tried accessing this variable inbetween this loop it will get an outdated value.
Declaring variable i as volatile tells compiler to not to optimize it in cache.
Keep DotNetting!!
GeekFromIndia
|
|
|
|
|
geekfromindia wrote: now thread1 accesses that variable from cache all the time...why doesn't other threads access that variable from cache instead of going to memory.
Because the optimisation might not be a cache. It might be a processor register, which naturally is local to the thread.
Consider for example a piece of code like this:
i = i + 1;
i = i * 2;
i = i - 4;
The compiler may determine that this code can be optimised using a processor register into something like:
reg = i
reg.add(1)
reg.mul(2)
reg.sub(4)
i = reg
In the optimised version, the variable will be unchanged until all the calculations has been done, never seeing the intermediate values. By making the variable volatile, you tell the compiler that the variable has to be updated for each calculation.
Experience is the sum of all the mistakes you have done.
|
|
|
|
|
Normally, it is valid for the compiler to do an optimization like this:
while (!abort) {
}
->
bool condition = !abort;
while (condition) {
}
Now if another thread sets "abort = true;", this won't be visible in the loop because the old abort value was "cached"; so the loop will run forever. Marking abort as volatile tells the compiler it that might be changed by other threads and must not be cached.
|
|
|
|
|
hi every one.
i want to make an application in c# through which two person can chatt from 2 different pc.
i dont know how to do voice chatting..so plz help me if some one know....
thanks
|
|
|
|
|
|
|
Hello,
I'm working in MS Visual C# 2005 Express.
I recently stumbled upon a nice "vista-like" button class posted here on codeproject.
http://www.codeproject.com/KB/miscctrl/VistaButton.aspx[^]
It's worked fine for one of my projects, except that I cannot set the DialogResult on one of those custom buttons, like you can a regular button.
I'm trying to add it manually into the vistabutton User Control but I have no luck so far. This is what I have added:
private DialogResult dResult = DialogResult.OK;<br />
<br />
public DialogResult DialogResult<br />
{<br />
get { return DialogResult.OK; }<br />
set { dResult = value; }<br />
}
And i'm trying to use it like this in my designer code:
this.button8.DialogResult = System.Windows.Forms.DialogResult.OK;
I'm still a newb coder, can someone point out my errors? It compiles and runs fine.. but doesn't return the dialogresult
|
|
|
|
|
Ok, I made a few changes but it still does not seem to work.
private DialogResult dResult = DialogResult.None;<br />
<br />
public DialogResult DialogResult<br />
{<br />
get { return dResult; }<br />
set { dResult = value; }<br />
}
|
|
|
|
|
The form's DialogResult property needs to be set to the button'd DialogResult property when clicked so in the button8 Click event enter this code
this.DialogResult = button8.DialogResult;
Dave
|
|
|
|
|
Yes! That was it. Thanks!!
|
|
|
|
|
My project just stop to run my recent changes, it continues running and earlier version of my project
it means, not matter what kind of changes I do to my code, the earlier version still run.
Before this estrange behavior, every time a make a mistake a message box appear and ask about run the last running version or stop, but now the windows doesn't appear.
Regards
Rafael
Life is Great, and kids the best
|
|
|
|
|
Do a complete recompile. It sounds like you've got a bug in your code and Visual Studio is running the last known good build.
|
|
|
|
|
Hi,
I sometimes get the same behavior with VS2005 C# Express Edition, seems like a bug
in Visual; in my experience there are several ways that typically solve it:
- perform a clean build (Menu Item "Build/Rebuild Solution")
- delete the entire "bin" and "obj" directories in the project folder; first check
they contain output files only, your input files (source files, resources) should
not be there! Then rebuild.
- reboot and build again.
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips:
- before you ask a question here, search CodeProject, then Google;
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get;
- use PRE tags to preserve formatting when showing multi-line code snippets.
|
|
|
|
|
I am trying to mimic the operation of a temperature transmitter that streams data via UDP. So, currently I have a program that sends data every second. For some reason, in my receive program, I receive each packet twice. Once from the correct IP address and the wrong port, and secondly from 0.0.0.0 ip address and the right port (8000). Here is my code. Does anyone have any suggestions?
// defined as a member of the form
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void Form1_Load(object sender, System.EventArgs e)
{
EndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 8000);
s.Connect(ipep);
}
private void timer1_Tick(object sender, System.EventArgs e)
{
// ignore the string part for now
string strLine = (string)lstData[cnt++];
if (cnt == lstData.Count)
cnt = 0;
byte[] data = Encoding.ASCII.GetBytes(strLine);
s.Send(data);
}
Chase Davis
|
|
|
|
|
Chase Davis wrote: Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Chase Davis wrote: EndPoint ipep = new IPEndPoint(IPAddress.Broadcast, 8000);
s.Connect(ipep);
Chase Davis wrote: s.Send(data);
I'm no expert with UDP but I think you are supposed to use Socket.SendTo(...)
Maybe this will help[^]
led mike
|
|
|
|
|
Actually, it works either way. With the Send function, I think you have to call the connect function. With the SendTo function, you don't have to connect, but you do have to set the SocketOption to Broadcast. Anyway, I tried using the SendTo function and I still see the same results. Thanks for the help though.
Chase Davis
|
|
|
|
|
Perhaps the problem is in the Receiving code which you have not posted?
led mike
|
|
|
|
|
Well, I thought about that. However, I used a program from the customer that monitors UDP data and the same thing happens. In any case, here is the receive code:
// local member of the form
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private void btnStart_Click(object sender, System.EventArgs e)
{
// user defined class to contain socket information
StateObject state = new StateObject();
state.workSocket = s;
EndPoint ipep = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPortNum.Text, 10));
// only bind if it has not been bound yet
if (s.LocalEndPoint == null)
s.Bind(ipep);
s.BeginReceiveFrom(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, ref ipep, new AsyncCallback(OnReceive), state);
}
void OnReceive(IAsyncResult iar)
{
StateObject state = (StateObject)iar.AsyncState;
Socket client = state.workSocket;
EndPoint epRemote = new IPEndPoint(IPAddress.Any, Convert.ToInt32(txtPortNum.Text, 10));
int bytesRead = client.EndReceiveFrom(iar, ref epRemote);
string str = Encoding.ASCII.GetString(state.buffer);
// do stuff with the string
s.BeginReceiveFrom(state.buffer, 0, StateObject.BufferSize, SocketFlags.None, ref epRemote, new AsyncCallback(OnReceive), state);
}
Chase Davis
|
|
|
|
|
Chase, like I said I have had no experience with UDP so I just put together two apps, Sender and Client, they work as expected here is the code
Sender Application
public partial class Form1 : Form
{
private System.Net.Sockets.Socket _sock;
private System.Net.IPEndPoint _ipEnd;
public Form1()
{
InitializeComponent();
_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPHostEntry hostent = Dns.Resolve(Dns.GetHostName());
_ipEnd = new IPEndPoint(hostent.AddressList[0], 3030);
}
private void button1_Click(object sender, EventArgs e)
{
byte[] bytes = Encoding.ASCII.GetBytes(textBox1.Text);
Cursor oldCursor = Cursor;
Cursor = System.Windows.Forms.Cursors.WaitCursor;
_sock.SendTo(bytes, _ipEnd);
Cursor = oldCursor;
}
}
Client application
public partial class Form1 : Form
{
UdpClient _client;
IPEndPoint _ipEndPoint;
public Form1()
{
InitializeComponent();
_ipEndPoint = new IPEndPoint(IPAddress.Any, 3030);
_client = new UdpClient(_ipEndPoint);
_client.BeginReceive(Recv, new State(_client, _ipEndPoint, this));
}
public void OnRecvMsg(string msg)
{
if( textBox1.Text.Length > 0)
textBox1.Text = String.Format("{0}\r\n{1}", textBox1.Text, msg);
else
textBox1.Text = msg;
}
public static void Recv(IAsyncResult ar)
{
State state = ar.AsyncState as State;
Byte[] bytes = state.Client.EndReceive(ar, ref state.EndPoint);
state.TForm.OnRecvMsg(Encoding.ASCII.GetString(bytes));
state.Client.BeginReceive(Recv, state);
}
}
internal class State
{
public IPEndPoint EndPoint;
public UdpClient Client;
public Form1 TForm;
internal State(UdpClient client, IPEndPoint endpoint, Form1 form)
{
EndPoint = endpoint;
Client = client;
TForm = form;
}
}
led mike
|
|
|
|