|
jondaeh wrote: if (display.InvokeRequired) { display.Invoke(new MethodInvoker(delegate() { setDispText(); })); } else { setDispText(); }
Try using BeginInvoke and returning
|
|
|
|
|
I have already tried BeginInvoke method, that didn't make any difference. Sorry, forgot to write that. What do you mean with "returning"?
Jon.
|
|
|
|
|
Welcome to the world of the dead(lock)
The method writeDisp calls setDispText and setDispText calls writeDisp. That looks very bad.
Do not abuse Invoke and use it for setting data in the control from a different thread without other code (the set display text also discards a serial port buffer).
Natza Mitzi
|
|
|
|
|
The reason why setDisispText calls writeDispText is to make the Thread go in a loop, and set the display whenever scanThread tells it so.
The reason I made a separate method for setDispText is that I found a code example on some other forum, which used exactly a separate method for the job. I thought this made sense, when it's no-go to access variables outside a delegate directly. This would be the case if I had wrote the invoke part like this
display.Invoke(new MethodInvoker(delegate() { display.Text = line; }));
However, I must say I'm on unknown ground here.. Please correct me
I will try to figure out a way to let the invoke only make the call to setDispText..just have to do a little thinking;)
Thanks for the answer!
Jon.
|
|
|
|
|
Hi,
sorry, this is wrong in many ways.
1.
you have unintended "tail recursion" in readPic; your code basically looks like:
public void readPic() {
...
readPic();
}
which means this method will NEVER terminate, since when it reaches the end, it calls itself, which calls itself, which... etc until you get a stack overflow.
2.
you have the same problem in writeDisp(): once the thread launches the first writeDisp, it will invoke setDispText() on the GUI thread, which calls writeDisp() again, which calls setDispText() again, etc, again climbing up the stack until it is exhausted (and halting all normal GUI action while doing it).
3.
the lock does not make any sense. Locks protect against two or more threads that otherwise could access the same code and data at the same time; you having only one lock statement, there is no one to keep out. Throw it away!
4.
Why do you perform serialPort.DiscardInBuffer(); ?? Every time you read one byte (which removes it from the buffer) you clear out the buffer; doing it in a separate statement (and here even on a separate thread) causes an undefined and variable delay so you may be throwing away the very next byte that comes in. Does not make sense to me.
5.
You are using two threads (and an AutoResetEvent to prevent the second one from running ahead of the first), however IMO you don't need two threads at all.
This is what I would try as a first attempt, details may be wrong, I haven't tested it:
public void readPic() {
while(running) {
try {
int i = serialPort.ReadByte();
string line = i.ToString();
display.Invoke(new MethodInvoker(delegate() { display.Text = line; }));
} catch (TimeoutException) {
}
}
}
So the second thread is gone, the tail recursion is gone; a loop is introduced to keep reading the serial port until a bool flag is cleared to cause the thread to exit.
The one thing that is not clear to me is what you want from the time-out: all you do is try again,
so setting the timeout to infinite would do exactly the same.
Luc Pattyn [Forum Guidelines] [My Articles]
- 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi!
Followed your advice, and it worked great right away!
However, it's still some details that is not completely clear to me. I know that it doesn't matter now that it is working, but I can't stand not knowing what went wrong(psycho.. ).
1.
I know that the loop I had written would never stop. However, what do you mean with stack overflow? As I see it, it will just go around and around, with a pause on 100ms on every cycle. Or does it make a new "process" on each cycle?
3.
I tried to have that lock I had written there on both places where line was accessed. This I meant as a safety to prevent line not to be written from the scanThread and read by writeThread at the same time.
I see why these locks didn't work, but how do I get a lock to understand what object it shall lock? Would I have to write it like this: lock(line){"do something with line"} ? Or must line be accessed from inside a method to make a lock?
4.
I used DiscardBuffer() to make sure that scanThread would get hold on only the newest byte. I was afraid of the buffer to fill up with several bytes while the writeThread did it's job. But I see now how accessing serialPort from another thread will make unnecessary thread "collision". Now I just implemented DiscardBuffer() before the while loop, and it seem to work fine.
Sorry being so curious, but I think these threading things is very interesting. Plus, apparently, we won't be learning it at school
Thanks for good help and good feedback! Appreciate it!
Jon.
|
|
|
|
|
Hi Jon,
1.
jondaeh wrote: I know that the loop I had written
your code did NOT have a loop at all, it had "recursion", i.e. a method calling itself, and an infinite recursion since there was no way to get out of it (compare with the typical example of calculating x factorial with recursion). Every method call takes a number of bytes of stack space; google and read up on "stack".
3.
jondaeh wrote: lock(line){"do something with line"}
one uses a lock to protect some data from being manipulated by two or more threads at the same time, when there is a danger of an inconsistent state (half of the data being written by one, then all of the inconsistent data being read by the others). The object on which the lock applies may or may not be the data to be protected, it all depends.
jondaeh wrote: Sorry being so curious
No problem. Being eager to learn can't be bad. Buy some books and study them, that is the best way to learn languages and technologies. And don't forget to read some CodeProject articles, choose from those with a high score!
Luc Pattyn [Forum Guidelines] [My Articles]
- 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 the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
I am getting this error while sending message though VS08 C# code....
replicator error,
no route is known from this host to the destination address
What possible solution...?
|
|
|
|
|
Replace bloated goatse with a real email client
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Please........don't make me feel shameful. I use it.
जय हिंद
|
|
|
|
|
as long as you didn't pick it for your company on the basis of glossy's in PHB Times...
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
I did not pick it. That is the only mail client we are allowed to use.
जय हिंद
|
|
|
|
|
My commiserations. I used it for nearly 3 years at my first job.
|
|
|
|
|
I have a form that has many text boxes on it. When the user selects a record to work with I load all the text boxes with the approiate data. I want to be able to determine if the user changed any of the content in any of the text boxes?
I have looked at the text change event, but won't this event fire when I load the text boxes from the db?
If any body has any suggestions or sample code for doing this it would be appreciated!
Thanks!
sk
|
|
|
|
|
Two quick solutions.
0: Unsubscribe to the TextChanged events immediately prior to loading from the Database and add the subscribers again afterwards.
1. Use a boolean flag field that you set to true when loading and set to false afterwards. Check this flag in the TextChanged event handling method(s) and just return immediately if true.
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)
|
|
|
|
|
I will vote for the boolean. Here is an example:
private bool mInUpdate;
private void LoadControlWithData()
{
//I want to keep the last mode
bool lastUpdateMode = mInUpdate;
try
{
mInUpdate = true;
//Update code goes here
}
finally
{
//restore the last flag
mInUpdate = lastUpdateMode;
}
}
private void textBox1_TextChanged(....)
{
//Test the update flag
if(!mInUpdate)
{
//Not in update lets do stuff
}
}
Natza Mitzi
|
|
|
|
|
I have this scenario:
I have hash table with keys are same as private member variable names something like below:
key ---- value
--------------------
m_strID ---- "123"
m_strName ---- "xyz"
Now without writing any loop and case statement i want to assign values from hashtable to my class local variables:
I don't want to do like below as i have to hard code things:
foreach (string key in hashtable.keys)
{
switch(key)
{
case "m_strID":
(local member) m_strID = value
}
}
Even i don't want to use reflection, i guess, it has some overhead. I can't think of any other way. Any thoughts are appreciated.
Thanks.
|
|
|
|
|
I don't think that's possible without Reflection. The overhead would only really be noticed if you loop several hundred times anyway, so there really isn't any need to worry if you're just putting together a small application
|
|
|
|
|
Thanks for the reply, i just want to confirm that there is no other way without reflection and am not missing any good logic. This bit which i am talking about is used in my serialize and deserialize process. Even there are few members (about 50), its going to be executed many times (every time i send a query across wire). Even MS logic for finding the serializable members and serializing uses reflection (hope i am not wrong).
|
|
|
|
|
see system.runtime.serialization[^]
hope it helps
dhaim
ing ngarso sung tulodho, ing madyo mangun karso, tut wuri handayani. "Ki Hajar Dewantoro"
in the front line gave a lead, in the middle line build goodwill, in the behind give power support
modified on Tuesday, March 17, 2009 3:27 PM
|
|
|
|
|
Dhaim, we are implemented ISerializable in our classed and handling serialization ourself. I am looking more optimized code for serializing objects on ourown.
|
|
|
|
|
If you are looking for a faster way and less space, serialize the bytes your self using BitConverter class (static methods). The downside is that you need to hand code all of this unless you build a little SW that will do it for you.
Natza Mitzi
|
|
|
|
|
Hi!
I have build C# DLL with few function and Registered it successfully
and also consuming it through Javascript ActiveXObject.
So far All is good.
Now I want to raise one Event in C# Dll and want that event to be handled by Javascript
Is that possible to catch event in Javascript which is thrown by C# DLL, If yes can you please help me.
Thanks!
Develop2Program & Program2Develop
|
|
|
|
|
You mean Ajax?[^]
Have a look at this article[^] and how it gets the data from the server side using the Ajax.Net framework.
hmmm pie
modified on Tuesday, March 17, 2009 12:29 PM
|
|
|
|
|
Thanks for your reply, But no this is not what I mean
I want to raise an Event in C# DLL class and this should be Caught in Javascript Code
<script language="javascript">
var myObj
myObj = new ActiveXObject("myDLL.myClass");
myObj.myPropertyOne="value1";
myObj.processValue();
</script>
Now if I through Event with value 0 Then how can I get the Event in
Javascript.
Thanks!
Develop2Program & Program2Develop
|
|
|
|