Click here to Skip to main content
15,905,682 members
Home / Discussions / C#
   

C#

 
AnswerRe: lotus notes Pin
Christian Graus17-Mar-09 11:56
protectorChristian Graus17-Mar-09 11:56 
QuestionUI hangs on Invoke(?) Pin
jondaeh17-Mar-09 9:37
jondaeh17-Mar-09 9:37 
AnswerRe: UI hangs on Invoke(?) Pin
fred_17-Mar-09 10:06
fred_17-Mar-09 10:06 
AnswerRe: UI hangs on Invoke(?) Pin
fred_17-Mar-09 10:06
fred_17-Mar-09 10:06 
GeneralRe: UI hangs on Invoke(?) Pin
jondaeh17-Mar-09 10:51
jondaeh17-Mar-09 10:51 
GeneralRe: UI hangs on Invoke(?) Pin
Natza Mitzi17-Mar-09 10:16
Natza Mitzi17-Mar-09 10:16 
QuestionRe: UI hangs on Invoke(?) Pin
jondaeh17-Mar-09 11:09
jondaeh17-Mar-09 11:09 
AnswerRe: UI hangs on Invoke(?) Pin
Luc Pattyn17-Mar-09 14:52
sitebuilderLuc Pattyn17-Mar-09 14:52 
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.

Smile | :)

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


GeneralRe: UI hangs on Invoke(?) Pin
jondaeh17-Mar-09 22:55
jondaeh17-Mar-09 22:55 
GeneralRe: UI hangs on Invoke(?) Pin
Luc Pattyn18-Mar-09 1:24
sitebuilderLuc Pattyn18-Mar-09 1:24 
QuestionLotus Notes Pin
lokesh143.surana17-Mar-09 9:02
lokesh143.surana17-Mar-09 9:02 
AnswerRe: Lotus Notes Pin
Dan Neely17-Mar-09 9:44
Dan Neely17-Mar-09 9:44 
GeneralRe: Lotus Notes Pin
dan!sh 17-Mar-09 10:36
professional dan!sh 17-Mar-09 10:36 
GeneralRe: Lotus Notes Pin
Dan Neely17-Mar-09 11:07
Dan Neely17-Mar-09 11:07 
GeneralRe: Lotus Notes Pin
dan!sh 17-Mar-09 19:45
professional dan!sh 17-Mar-09 19:45 
GeneralRe: Lotus Notes Pin
Vikram A Punathambekar17-Mar-09 20:40
Vikram A Punathambekar17-Mar-09 20:40 
QuestionNewbie Question Pin
kruegersck17-Mar-09 7:57
kruegersck17-Mar-09 7:57 
AnswerRe: Newbie Question Pin
DaveyM6917-Mar-09 8:29
professionalDaveyM6917-Mar-09 8:29 
AnswerRe: Newbie Question Pin
Natza Mitzi17-Mar-09 10:25
Natza Mitzi17-Mar-09 10:25 
Questionaccessing members without reflection. Pin
Member 232448317-Mar-09 7:50
Member 232448317-Mar-09 7:50 
AnswerRe: accessing members without reflection. Pin
0x3c017-Mar-09 8:03
0x3c017-Mar-09 8:03 
GeneralRe: accessing members without reflection. Pin
Member 232448317-Mar-09 8:19
Member 232448317-Mar-09 8:19 
AnswerRe: accessing members without reflection. [modified] Pin
Mbah Dhaim17-Mar-09 9:15
Mbah Dhaim17-Mar-09 9:15 
GeneralRe: accessing members without reflection. Pin
Member 232448317-Mar-09 9:44
Member 232448317-Mar-09 9:44 
GeneralRe: accessing members without reflection. Pin
Natza Mitzi17-Mar-09 10:31
Natza Mitzi17-Mar-09 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.