|
As per a previous thread I have an app which receives events from a COM object. Those events come in on a different thread than my app, so I'm using Control.BeginInvoke() to correct the problem.
BeginInvoke() asynchronously queues the event for handling by my app. The problem is, I want to log the actual time the event was received, not when my handler was finally called to process the queued event.
So I'm calling a delegate which takes one more argument than my handler, and I'm passing in the time, like this:
private delegate void _OnSoundVolumeChangedEvent(DateTime time, int newVolume);
private void itunes_OnSoundVolumeChangedEvent(int newVolume)
{
if (InvokeRequired)
{
BeginInvoke(new _OnSoundVolumeChangedEvent(_OnSoundVolumeChangedEventInvoked), new object[] { DateTime.Now, newVolume });
}
else
{
Debug.Assert("Whoa! Expected this event to require Invoking!")
}
}
private void _OnSoundVolumeChangedEventInvoked(Datetime time, int newVolume)
{
// Do my work
}
However, I'm now curious: if InvokeRequired was true, is it safe to call DateTime.Now? I've already established that it was not safe to pass any of my other form data, since that data belonged to the form thread, and not the calling thread. However, is there any problem calling system methods like DateTime.Now, from the calling thread?
|
|
|
|
|
The one thing you MUST avoid is sharing Controls: a thread that did not create a Control
should not touch it (except for the three or four members that are thread-safe,
including InvokeRequired itself).
In general, having multiple threads working on the same data requires caution (which
often means locks).
The framework classes mostly follow the rule "Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe."
DateTime has that same sentence in the doc; and DateTime.Now is static, so no problem here.
Luc Pattyn
|
|
|
|
|
I've also decided to pass the Control.InvokeRequired value itself to the delegate, so I can log whether I really needed to use BeginInvoke (which I'm going to use anyway). Since that is a form-level value, sort of - is it safe to pass it?
|
|
|
|
|
I already told you InvokeRequired is available to all threads.
The opposite would make it absolutely useless, it then would always be false !
Luc Pattyn
|
|
|
|
|
Hello,
I am writing a c# program that uses dataGridView to view my database tables (Access), which are selected by a comboBox.
I have a problem with the INSERT INTO statements. The first table (has the comboBox index 0) insertion is ok but the next two tables when the insertion is made gives me an error "syntax error in INSERT INTO statement", although its the exact same as the first table insert statement!
I can't figure whats wrong, if you can help me with it I'd appreciate it.
Thank you.
-- modified at 20:29 Friday 26th January, 2007
Regards,
Butterfly
|
|
|
|
|
Butterfly_h wrote: I can't figure whats wrong
Neither can we unless we see some code
Butterfly_h wrote: if you can help me with it I'd appreciate it
Just show us a snippet of what's going wrong and we'll try and help you.
I have no idea what I just said. But my intentions were sincere.
Poore Design
|
|
|
|
|
Please if you can take a look at my code.
Thanks.
Regards,
Butterfly
|
|
|
|
|
What is the text of the statements ? How are they generated ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
<br />
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)<br />
{<br />
switch (comboBox1.SelectedIndex)<br />
{<br />
case 0:<br />
{<br />
customerAdd();<br />
break;<br />
}<br />
case 1:<br />
{<br />
materialAdd();<br />
break;<br />
}<br />
case 2:<br />
{<br />
accessoryAdd();<br />
break;<br />
}<br />
}<br />
}<br />
<br />
private void accessoryAdd()<br />
{<br />
int rowCount = dataGridView1.SelectedRows.Count;<br />
int newRowPosition = dataGridView1.CurrentRow.Index;<br />
string x, y;<br />
<br />
string path1 = Application.StartupPath;<br />
path1 = string.Concat(path1, "\\Data\\");<br />
string myStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + "materialCopy.mdb";<br />
<br />
OleDbConnection myConn = new OleDbConnection(myStr);<br />
<br />
try<br />
{<br />
myConn.Open();<br />
}<br />
catch (OleDbException ex)<br />
{<br />
MessageBox.Show("Error in connection ..." + ex.Message);<br />
}<br />
OleDbCommand myCmnd = new OleDbCommand();<br />
OleDbDataAdapter myAdapter = new OleDbDataAdapter();<br />
DataSet ds = new DataSet();<br />
<br />
x = dataGridView1.Rows[newRowPosition].Cells[0].Value.ToString();<br />
y = dataGridView1.Rows[newRowPosition].Cells[1].Value.ToString();<br />
<br />
myCmnd = new OleDbCommand("INSERT INTO Accessories (accessory-type, accessory-price)" + "VALUES('" + x + "','" + y + "')", myConn);<br />
myAdapter.SelectCommand = myCmnd;<br />
ds = new DataSet();<br />
ds.Clear();<br />
myAdapter.Fill(ds, "Accessories");<br />
<br />
myCmnd = new OleDbCommand("SELECT * FROM Accessories", myConn);<br />
myAdapter = new OleDbDataAdapter(myCmnd);<br />
myAdapter.SelectCommand = myCmnd;<br />
ds = new DataSet();<br />
ds.Clear();<br />
myAdapter.Fill(ds, "Accessories");<br />
myAdapter.Update(ds, "Accessories");<br />
<br />
myConn.Close();<br />
accessoriesDbOn();<br />
}<br />
<br />
private void customerAdd()<br />
{<br />
int rowCount = dataGridView1.SelectedRows.Count;<br />
int newRowPosition = dataGridView1.CurrentRow.Index;<br />
string n, y, z;<br />
<br />
string path1 = Application.StartupPath;<br />
path1 = string.Concat(path1, "\\Data\\");<br />
string myStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path1 + "materialCopy.mdb";<br />
<br />
OleDbConnection myConn = new OleDbConnection(myStr);<br />
<br />
try<br />
{<br />
myConn.Open();<br />
}<br />
catch (OleDbException ex)<br />
{<br />
MessageBox.Show("Error in connection ..." + ex.Message);<br />
}<br />
OleDbCommand myCmnd = new OleDbCommand();<br />
OleDbDataAdapter myAdapter = new OleDbDataAdapter();<br />
DataSet ds = new DataSet();<br />
<br />
n = dataGridView1.Rows[newRowPosition].Cells[1].Value.ToString();<br />
z = dataGridView1.Rows[newRowPosition].Cells[2].Value.ToString();<br />
y = dataGridView1.Rows[newRowPosition].Cells[3].Value.ToString();<br />
<br />
myCmnd = new OleDbCommand("INSERT INTO Customer (name, mobile, phone)" + "VALUES('" + n + "','" + z + "','" + y + "')", myConn);<br />
myAdapter.SelectCommand = myCmnd;<br />
ds = new DataSet();<br />
ds.Clear();<br />
myAdapter.Fill(ds, "Customer");<br />
<br />
myCmnd = new OleDbCommand("SELECT * FROM Customer", myConn);<br />
myAdapter = new OleDbDataAdapter(myCmnd);<br />
myAdapter.SelectCommand = myCmnd;<br />
ds = new DataSet();<br />
ds.Clear();<br />
myAdapter.Fill(ds, "Customer");<br />
myAdapter.Update(ds, "Customer");<br />
<br />
myConn.Close();<br />
customerDbOn();<br />
}<br />
<br />
This is the code for the addition (insert new row into a table) that I use. The Material table shows no error but both Customer and Accessories tables give me the error I mentioned before (Syntax error in "INSERT INTO").
Regards,
Butterfly
|
|
|
|
|
You might log the offending INSERT INTO commands and see what is wrong.
Are spaces relevant ?
Luc Pattyn
|
|
|
|
|
I tried doing so but the same error occurs!
Regards,
Butterfly
|
|
|
|
|
Butterfly_h wrote: ("INSERT INTO Accessories (accessory-type, accessory-price)" + "VALUES('" + x + "','" + y + "')",
try using the escape char with your single quotes... for example "VALUES(\'" + x + "\', \'" + y + \'")
are x, y and the relevant db columns of the same data type?
|
|
|
|
|
yes they are.
If you can go through the code I have three tables each has the same insert statement but it only works with the Material table.
I'll try your suggestion and tell you what happens.
Regards,
Butterfly
|
|
|
|
|
I once more suggest you list the three actual INSERT lines (on 3 consecutive lines!),
and I am confident you will spot the problem.
Luc Pattyn
|
|
|
|
|
I tried it and it still not working!
Please if anyone figures it out to tell me.
Regards,
Butterfly
|
|
|
|
|
Hello again,
I got the solution to my error and it was in the columns names in both Material and Accessories tables.
I should have used [,] with the column names containing -.
Thanks for your responses.
Regards,
Butterfly
|
|
|
|
|
hi
i want to set password on my exe file,i know that i run this operate via Executable header but i don't found an article that use executable header in c#.
anybody can help me >?
thanks
|
|
|
|
|
Eh? Can you explain more clearly.
Do you want to ask the user for a password when they run the program?
If so then just add a password dialog before anything else in your main routine.
Or are you trying to encrypt the contents of the executable?
I have no idea what I just said. But my intentions were sincere.
Poore Design
|
|
|
|
|
hi,
please can anyone tell me the main difference b/w Read-only variable & Constant variable??
thanks,
Rahi
If you look at what you do not have in life, you don't have anything,
If you look at what you have in life, you have everything... "
|
|
|
|
|
Citing MSDN: "The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants" (readonly (C# Reference)[^])
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook www.troschuetz.de
|
|
|
|
|
readonly is evaluated at runtime, const is evaluated at compile time. So, if you use a const value from one dll in another, you need to redistribute both dlls if the value changes. If it's read only, you need only replace the dll that changes the value.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Also, you can have a const value type, but not a const reference type (except for System.String). Readonly can be either value or reference type objects.
:josh:
My WPF Blog[ ^]
We are what we repeatedly do. Excellence then, is not an act, but a habit. - Aristotle
|
|
|
|
|
|
Thanks to all who answered my question. now i came to know what is the difference b/w read-only & a constant.
Thanks,
Rahi
If you look at what you do not have in life, you don't have anything,
If you look at what you have in life, you have everything... "
|
|
|
|
|
I hope this is an easy one. It seems like it should be.
I have a pictureBox that I am using as a button. I'm swapping images in and out for up and down states. When the user clicks and holds the mouse down I want it to execute the MouseDown code multiple times until they MouseUp.
<br />
private void mirrorAdjust_MouseDown(object sender, MouseEventArgs e)<br />
{<br />
mirrorPictureBox.Image = Resources.MirrorDown;<br />
<br />
FunctionToRepeatUntilMouseUp();<br />
}<br />
<br />
private void mirrorAdjust_MouseUp(object sender, MouseEventArgs e)<br />
{<br />
mirrorPictureBox.Image = Resources.MirrorUp;<br />
}<br />
|
|
|
|