Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. A tricky priority problem

A tricky priority problem

Scheduled Pinned Locked Moved C#
data-structureshelpquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Snowjim
    wrote on last edited by
    #1

    Hey! I am using System.IO.SerialPort in framework 2.0(Beta2) to listening on the port. I am also using DataReceived event on the serialPort object. I have set the threshold to 1, this means that the event(DataReceived) will fired as soon as 1 byte is written to the serialport. When this event is triggerd, I am using a ThreadPool to queue work to my OnRecivedData() method. In OnRecivedData() I first reads existing on the serialport, but I also writes this icoming data to file. To see if this realy got what it takes I have made som test. I am sending data as fast as posible over the nullmodem cable with the follwing port settings: Baud: 11 5200 Data: 8 Parity: None Stop; 1 Problem: I have compared the file that I send, and the file I recived(creates) and this works fine in low speeds, but whenI am pushing the system to 100% load my program by moving around a window or something, It loses data? I realy have to get a stable connection even during stress. I first thought that I could set the ThreadPool to a higher priority, but I havent found any way to do this? It could also be the evnet(DataReceived) that is never fired, can I set higher priority on this event? Best Regards SnowJim

    C S 2 Replies Last reply
    0
    • S Snowjim

      Hey! I am using System.IO.SerialPort in framework 2.0(Beta2) to listening on the port. I am also using DataReceived event on the serialPort object. I have set the threshold to 1, this means that the event(DataReceived) will fired as soon as 1 byte is written to the serialport. When this event is triggerd, I am using a ThreadPool to queue work to my OnRecivedData() method. In OnRecivedData() I first reads existing on the serialport, but I also writes this icoming data to file. To see if this realy got what it takes I have made som test. I am sending data as fast as posible over the nullmodem cable with the follwing port settings: Baud: 11 5200 Data: 8 Parity: None Stop; 1 Problem: I have compared the file that I send, and the file I recived(creates) and this works fine in low speeds, but whenI am pushing the system to 100% load my program by moving around a window or something, It loses data? I realy have to get a stable connection even during stress. I first thought that I could set the ThreadPool to a higher priority, but I havent found any way to do this? It could also be the evnet(DataReceived) that is never fired, can I set higher priority on this event? Best Regards SnowJim

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Your code for recieving data should itself be in a worker thread, so that it continues to function even when the UI thread is maxed out by you moving a window. Christian Graus - Microsoft MVP - C++

      S 1 Reply Last reply
      0
      • C Christian Graus

        Your code for recieving data should itself be in a worker thread, so that it continues to function even when the UI thread is maxed out by you moving a window. Christian Graus - Microsoft MVP - C++

        S Offline
        S Offline
        Snowjim
        wrote on last edited by
        #3

        Yes, as I said, I use ThreadPool as soon as the event is triggerd. Or do you mean that i need a separate thread for the event? I thought that when an event accure, then a new thread is automatly created to handle this event? or is it the main(GUI) thread that will be used to the event? And if so, is there any way to get an own thread to this? BestRegards SnowJim

        1 Reply Last reply
        0
        • S Snowjim

          Hey! I am using System.IO.SerialPort in framework 2.0(Beta2) to listening on the port. I am also using DataReceived event on the serialPort object. I have set the threshold to 1, this means that the event(DataReceived) will fired as soon as 1 byte is written to the serialport. When this event is triggerd, I am using a ThreadPool to queue work to my OnRecivedData() method. In OnRecivedData() I first reads existing on the serialport, but I also writes this icoming data to file. To see if this realy got what it takes I have made som test. I am sending data as fast as posible over the nullmodem cable with the follwing port settings: Baud: 11 5200 Data: 8 Parity: None Stop; 1 Problem: I have compared the file that I send, and the file I recived(creates) and this works fine in low speeds, but whenI am pushing the system to 100% load my program by moving around a window or something, It loses data? I realy have to get a stable connection even during stress. I first thought that I could set the ThreadPool to a higher priority, but I havent found any way to do this? It could also be the evnet(DataReceived) that is never fired, can I set higher priority on this event? Best Regards SnowJim

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          It really shouldn't be losing data, under any circumstances. Did you view the file after closing the application? Maybe it was just getting buffered in memory? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          S 1 Reply Last reply
          0
          • S S Senthil Kumar

            It really shouldn't be losing data, under any circumstances. Did you view the file after closing the application? Maybe it was just getting buffered in memory? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            S Offline
            S Offline
            Snowjim
            wrote on last edited by
            #5

            Yes i know that there may be a buffert. But this buffert is flushed and writen to the file. If i dont move the Window around so i get 100% CPU usage, the transfere works fine. It is not losing data in the end, its on diffrent places al the time, and its often some lines of data(maby 5-6 that disipares? But the Thread that runns the event? is this Thread, the same as the main thread(the thread that draws the GUI)? Best Regards SnowJim

            S 1 Reply Last reply
            0
            • S Snowjim

              Yes i know that there may be a buffert. But this buffert is flushed and writen to the file. If i dont move the Window around so i get 100% CPU usage, the transfere works fine. It is not losing data in the end, its on diffrent places al the time, and its often some lines of data(maby 5-6 that disipares? But the Thread that runns the event? is this Thread, the same as the main thread(the thread that draws the GUI)? Best Regards SnowJim

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Hmm. How are you running OnReceived? Are you using ThreadPool.QueueWorkItem or the BeginInvoke method on a delegate object? If so, your function is running on a separate thread, not the UI thread. However, if you are calling BeginInvoke on an object derived from Control (like Form, TreeView etc..) then the function will run on the main thread (UI thread). Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              S 1 Reply Last reply
              0
              • S S Senthil Kumar

                Hmm. How are you running OnReceived? Are you using ThreadPool.QueueWorkItem or the BeginInvoke method on a delegate object? If so, your function is running on a separate thread, not the UI thread. However, if you are calling BeginInvoke on an object derived from Control (like Form, TreeView etc..) then the function will run on the main thread (UI thread). Regards Senthil _____________________________ My Blog | My Articles | WinMacro

                S Offline
                S Offline
                Snowjim
                wrote on last edited by
                #7

                Yes i am using the SerialPort.DataReceived Event. I have done some more testing now! In the DataReceived event funktioner(the funktion that the DataReceived envet will run) u placed the code that writes the data to the file, like this: lock (this) { if (RUNNING) { RemovePortHandlerRAW.Write(serialPort.ReadExisting()); RemovePortHandlerRAW.Flush(); /* ThreadPool.QueueUserWorkItem(new WaitCallback(onDataReceived)); currentRunningThreads++;*/ } } RUNNING will be true as long as i dont close the port. I have tryed to changes the serialPort.ReceivedBytesThreshold to all between 1 and 512, but there is not much diffrenc. If i dont move around some windows and dont get 100% CPU usage, the tranfere file is a perfect match to the one i sent. But if i move around the window so i get 100% CPU usage, then i will lose data in diffrent places? It simes like that the event will not be fired as is would!? I have thought of going back to a Thread that goes in a loop and check if there is somthing to read, and if, then it will read it, but this take some performance even if there is no data to be read. Any sugestions? BestRegards SnowJim PS hope you understand my english

                S 1 Reply Last reply
                0
                • S Snowjim

                  Yes i am using the SerialPort.DataReceived Event. I have done some more testing now! In the DataReceived event funktioner(the funktion that the DataReceived envet will run) u placed the code that writes the data to the file, like this: lock (this) { if (RUNNING) { RemovePortHandlerRAW.Write(serialPort.ReadExisting()); RemovePortHandlerRAW.Flush(); /* ThreadPool.QueueUserWorkItem(new WaitCallback(onDataReceived)); currentRunningThreads++;*/ } } RUNNING will be true as long as i dont close the port. I have tryed to changes the serialPort.ReceivedBytesThreshold to all between 1 and 512, but there is not much diffrenc. If i dont move around some windows and dont get 100% CPU usage, the tranfere file is a perfect match to the one i sent. But if i move around the window so i get 100% CPU usage, then i will lose data in diffrent places? It simes like that the event will not be fired as is would!? I have thought of going back to a Thread that goes in a loop and check if there is somthing to read, and if, then it will read it, but this take some performance even if there is no data to be read. Any sugestions? BestRegards SnowJim PS hope you understand my english

                  S Offline
                  S Offline
                  Snowjim
                  wrote on last edited by
                  #8

                  Hey! I have now runned some more tests! I have made a Thread with priotity = normal, that runns in a loop as long as RUNNING == true, if serialPort.BytesToRead > 0, then it will read from the port and write it to file. If not, it will Thread.sleep(1). This method is not working bether then the event method :(. I dont realy know how to solve this problem, I must ensure that no data is lost in the transfare, no mather what. Maby all programs will made mistakes when CPU usage is on 100%(when moving around(fast) for example a browser with alot of content)? My program have not realy a GUI, its a "window control library"(homemade control), i am adding this dll(Window control library) to an other project with a form, with two buttsons(open/close). This means that there is no real time display of the incoming data(now adance GUI to redraw). I am sending 100 000 rows that contains up to around 64 chars, when CPU usange is on 100% it misses rows, often up to 20-30 rows i have seen. If you got any ide how to proccead, let me know! Best Regards SnowJim

                  C 1 Reply Last reply
                  0
                  • S Snowjim

                    Hey! I have now runned some more tests! I have made a Thread with priotity = normal, that runns in a loop as long as RUNNING == true, if serialPort.BytesToRead > 0, then it will read from the port and write it to file. If not, it will Thread.sleep(1). This method is not working bether then the event method :(. I dont realy know how to solve this problem, I must ensure that no data is lost in the transfare, no mather what. Maby all programs will made mistakes when CPU usage is on 100%(when moving around(fast) for example a browser with alot of content)? My program have not realy a GUI, its a "window control library"(homemade control), i am adding this dll(Window control library) to an other project with a form, with two buttsons(open/close). This means that there is no real time display of the incoming data(now adance GUI to redraw). I am sending 100 000 rows that contains up to around 64 chars, when CPU usange is on 100% it misses rows, often up to 20-30 rows i have seen. If you got any ide how to proccead, let me know! Best Regards SnowJim

                    C Offline
                    C Offline
                    Carsten Zeumer
                    wrote on last edited by
                    #9

                    Have you tried to create the System.IO.SerialPort in a thread of its own yet? like this:

                    using System;
                    using System.Collections.Generic;
                    using System.Text;
                    using System.Threading;
                    using System.IO;
                    using System.IO.Ports;
                    
                    namespace SerialTest
                    {
                        class SerialReceiver
                        {
                            private System.Threading.ManualResetEvent m_Closed = new System.Threading.ManualResetEvent(false);
                    	    private System.IO.Ports.SerialPort m_Port = null;
                    	    private Thread m_Thread = null;
                    
                    	    public SerialReceiver()
                    	    {	
                    		    this.m_Closed = new ManualResetEvent(false);
                    		    this.m_thread = new Thread(new ThreadStart(this.ThreadEntry));
                    		    this.m_thread.Start();
                    	    }
                    
                    	    public ~SerialReceiver()
                    	    {
                    		    // make sure that the thread can end now
                    		    Close();
                    	    }
                    
                    	    public void Close()
                    	    {
                     		    this.m_Closed.Set();
                    	    }
                    
                            // thread entry point.
                    	    public void ThreadEntry()
                    	    {
                    		    this.m_Port  = new System.IO.Ports.SerialPort("COM1");
                                this.m_Port.ReceivedBytesThreshold=1;
                    
                                SerialDataReceivedEventHandler receivedHandler = new SerialDataReceivedEventHandler(Port_DataReceived);
                    		    // register event handler
                                this.m_Port.DataReceived += receivedHandler;
                    
                    		    // start listening
                    		    this.m_Port.Open();
                    
                    		    // wait for the closed event
                    		    this.m_Closed.WaitOne();
                    
                    		    // deregister event handler
                    		    this.m_Port.OnDataReceived+= receivedHandler;   		
                    	    }
                    
                            void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                            {
                                // get a buffer for all the available data
                                byte[] data = new byte[this.m_Port.BytesToRead];
                                // read the data
                                this.m_Port.Read(data,this.m_Port.BytesToRead);
                    
                                // dispatch the data...
                                foreach(byte b in data)
                                {
                                    //start your data handler thread here...
                                }
                    
                            }
                    
                        }
                    }
                    

                    The code is not testet and a prototype (i do not have the 2.0 framework installed yet) Btw. do you check if more than one byte is avail in your event handler? Even though the threshold for the event is 1 it is quite possible that there has more than one byte arived when the event is finally triggert. /cadi

                    S 1 Reply Last reply
                    0
                    • C Carsten Zeumer

                      Have you tried to create the System.IO.SerialPort in a thread of its own yet? like this:

                      using System;
                      using System.Collections.Generic;
                      using System.Text;
                      using System.Threading;
                      using System.IO;
                      using System.IO.Ports;
                      
                      namespace SerialTest
                      {
                          class SerialReceiver
                          {
                              private System.Threading.ManualResetEvent m_Closed = new System.Threading.ManualResetEvent(false);
                      	    private System.IO.Ports.SerialPort m_Port = null;
                      	    private Thread m_Thread = null;
                      
                      	    public SerialReceiver()
                      	    {	
                      		    this.m_Closed = new ManualResetEvent(false);
                      		    this.m_thread = new Thread(new ThreadStart(this.ThreadEntry));
                      		    this.m_thread.Start();
                      	    }
                      
                      	    public ~SerialReceiver()
                      	    {
                      		    // make sure that the thread can end now
                      		    Close();
                      	    }
                      
                      	    public void Close()
                      	    {
                       		    this.m_Closed.Set();
                      	    }
                      
                              // thread entry point.
                      	    public void ThreadEntry()
                      	    {
                      		    this.m_Port  = new System.IO.Ports.SerialPort("COM1");
                                  this.m_Port.ReceivedBytesThreshold=1;
                      
                                  SerialDataReceivedEventHandler receivedHandler = new SerialDataReceivedEventHandler(Port_DataReceived);
                      		    // register event handler
                                  this.m_Port.DataReceived += receivedHandler;
                      
                      		    // start listening
                      		    this.m_Port.Open();
                      
                      		    // wait for the closed event
                      		    this.m_Closed.WaitOne();
                      
                      		    // deregister event handler
                      		    this.m_Port.OnDataReceived+= receivedHandler;   		
                      	    }
                      
                              void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
                              {
                                  // get a buffer for all the available data
                                  byte[] data = new byte[this.m_Port.BytesToRead];
                                  // read the data
                                  this.m_Port.Read(data,this.m_Port.BytesToRead);
                      
                                  // dispatch the data...
                                  foreach(byte b in data)
                                  {
                                      //start your data handler thread here...
                                  }
                      
                              }
                      
                          }
                      }
                      

                      The code is not testet and a prototype (i do not have the 2.0 framework installed yet) Btw. do you check if more than one byte is avail in your event handler? Even though the threshold for the event is 1 it is quite possible that there has more than one byte arived when the event is finally triggert. /cadi

                      S Offline
                      S Offline
                      Snowjim
                      wrote on last edited by
                      #10

                      Thanks for your time! You solution did unfortunately not solve this problem =( I have thougt of that it maby is the serialPort that cant handle the stress. But i have tryed another serialPort program, and this program generates the exact same file, not data loses! This most with other words have to do with my software, then if it is the MS serialport that cant handle the stress or if its me that makes any thing wrong is hard to tell. Is there anyone else that could test this and comfirme the same problem? Best Regards SnowJim

                      S 1 Reply Last reply
                      0
                      • S Snowjim

                        Thanks for your time! You solution did unfortunately not solve this problem =( I have thougt of that it maby is the serialPort that cant handle the stress. But i have tryed another serialPort program, and this program generates the exact same file, not data loses! This most with other words have to do with my software, then if it is the MS serialport that cant handle the stress or if its me that makes any thing wrong is hard to tell. Is there anyone else that could test this and comfirme the same problem? Best Regards SnowJim

                        S Offline
                        S Offline
                        Snowjim
                        wrote on last edited by
                        #11

                        Some more testing is done! I am no following Cadi“s exampel to let e separate Thread create the serialport, just to be sure that the event is not using the main Thread, but this special serilport creater thread, is this right? Testing: If i open a directore in Windows that contains some files and press and hold F5(update) so i get 100% CPU usage while my program is recieving data in high speed there are no data loses? But if i move around the Firefox(web browser) with a advance page open and get 100 % CPU usage that way, then i lose data? how is this posible? I have notce that i somtimes get that the Thread is interupted, this must have somthing with the GUI/Main Thread to do, right? it is oly coming when moving around the window? Maby this is a problem that i shold not count on? i know that the for example Winamp works fine with this stress test(moving around the window) with out stop playing music, but maby its a diffrent thing with the serialport? Any sugestions is welcome Best Regards SnowJim

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups