Click here to Skip to main content
15,892,746 members
Home / Discussions / C#
   

C#

 
Questioncalculate elapsed time for timecard Pin
onetreeup17-Mar-09 13:31
onetreeup17-Mar-09 13:31 
AnswerRe: calculate elapsed time for timecard Pin
Christian Graus17-Mar-09 13:49
protectorChristian Graus17-Mar-09 13:49 
AnswerRe: calculate elapsed time for timecard Pin
Luc Pattyn17-Mar-09 14:27
sitebuilderLuc Pattyn17-Mar-09 14:27 
GeneralRe: calculate elapsed time for timecard Pin
Christian Graus17-Mar-09 14:42
protectorChristian Graus17-Mar-09 14:42 
Questionlotus notes Pin
tejtiks17-Mar-09 10:56
tejtiks17-Mar-09 10:56 
AnswerRe: lotus notes Pin
Pete O'Hanlon17-Mar-09 11:36
mvePete O'Hanlon17-Mar-09 11:36 
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 
Hi!

I am (still) working on the project I asked for something in this message:

http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2956783

I am trying to follow Luc Pattyn's advice on make some synchronous threading, one to scan the serialport in a loop(scanThread), and one to write what the first thread "found" to the textbox(writeThread). The threadcalls in them self seem to function(on debugging), but when the writeThread tries to invoke the textbox something seems to go wrong, and the UI hangs. I have googled the question, and found that it's a frequent problem, but everyone answers the the .Invoke() or .BeginInvoke() is the answer. However, here it isn't.

The lock I have implemented in the attached code was just because I tried to synchronize the threads, it doesn't work any better without(or with Laugh | :laugh: ). But I would like a little opinion if I have written it correct, seems strange to me that the "this" will refer to "line".

However, it's the UI "hanging" which is my main problem right now. I really can't see what's wrong..

Thanks!

Jon
Norway

The Code:
public partial class Picaxecomm : Form
    {

        private Thread scanThread;
        private Thread writeThread;
        private String line;
        private SerialPort serialPort;
        private Boolean start;
        private AutoResetEvent autoEvent;

        public Picaxecomm()
        {
            InitializeComponent();
            serialPort = new SerialPort();
            initializePort();
            autoEvent = new AutoResetEvent(false);
            scanThread = new Thread(new ThreadStart(readPic));
            writeThread = new Thread(new ThreadStart(writeDisp));
        }
         /**
         * Initializes the port with the desired parameters for the communication with the PICAXE chip.
         */
        public void initializePort()
        {
            serialPort.PortName = "COM5";
            serialPort.BaudRate = 4800;
            serialPort.DataBits = 8;
            serialPort.ReadTimeout = 100;
            serialPort.Open();
            serialPort.DtrEnable = true;
        }

        /*
         * Just a button, then I know that the program at least starts succesfully.
         */
        private void knapp_Click(object sender, EventArgs e)
        {
            if (start == false)
            {
                writeThread.Start();
                scanThread.Start();
                start = true;
            }
        }
        /**
         * The scanThrad goes in a loop when scanning the port.   
         * The autoevent signals the writeThread that the scanThread actually has got something and written it to
         * the line string. 
         */
        public void readPic()
        {
            
            try
            {
                int i = serialPort.ReadByte();
                lock (this) //I have tried to remove this, but doesn't make any difference. 
                {
                    line = Convert.ToString(i);
                }
                autoEvent.Set();
            }

            catch (TimeoutException)
            {
                
            }
            Thread.Sleep(50);
            readPic();
            
            
        }

        /**
         * This is where the writeThread works. Made an if/else thing around the invoke, just to be sure,
         * as it seems there is something with another thread than the the one that made the display writng it(killer
         * sentence;))
         */
        public void writeDisp()
        {
            autoEvent.WaitOne(); //Got the signal from the scanThread. Let's write the display.
            if (display.InvokeRequired)
           {
               display.Invoke(new MethodInvoker(delegate() { setDispText(); }));
           }
           else
           {
               setDispText();
           }
        }

        /**
         * Looks like the program stops somewhere in this method
         */
        public void setDispText()
        {
            display.Text = line;
            serialPort.DiscardInBuffer();
            writeDisp();
        }

        private void Picaxecomm_Load(object sender, EventArgs e){} //Didn't compile without. Whatever.
        }
}

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 
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 

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.