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
  1. Home
  2. General Programming
  3. C#
  4. MultiThread and File process

MultiThread and File process

Scheduled Pinned Locked Moved C#
helptutorialquestion
19 Posts 5 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.
  • I Ilia Blank

    How the drawing is initiated? For example: I have to update my animation when one of the files is updated on the disk. Or I have to update my animation with specific time intervals.

    A Offline
    A Offline
    AlexB47
    wrote on last edited by
    #7

    in 2 mode: - In real time from event receive from 3 comPort - in post processing by read all files saved on hard disk (read time field saved internal)

    Alex

    I 1 Reply Last reply
    0
    • A AlexB47

      in 2 mode: - In real time from event receive from 3 comPort - in post processing by read all files saved on hard disk (read time field saved internal)

      Alex

      I Offline
      I Offline
      Ilia Blank
      wrote on last edited by
      #8

      Correct me if I am wrong, in both cases the files will contain full history of the race for given horse. For example: 0 milliseconds 0 meters 2 milliseconds 0.3 meters 8 milliseconds 0.8 meters 12 milliseconds 1.1 meters … 88354 milliseconds 2000 meters //Finish

      A 1 Reply Last reply
      0
      • I Ilia Blank

        Correct me if I am wrong, in both cases the files will contain full history of the race for given horse. For example: 0 milliseconds 0 meters 2 milliseconds 0.3 meters 8 milliseconds 0.8 meters 12 milliseconds 1.1 meters … 88354 milliseconds 2000 meters //Finish

        A Offline
        A Offline
        AlexB47
        wrote on last edited by
        #9

        Yes, in theory ... but I don't know what problem can generate a COM PORT EVENT ...

        Alex

        I 1 Reply Last reply
        0
        • W wjp_auhtm

          Hi: I just write a simple example for you. I hope it was useful for you. /// <summary> /// example of MultiThread /// </summary> class MultiThread { Thread td_example = null; System.Windows.Forms.Control _ctl; public MultiThread(System.Windows.Forms.Control ctl) { td_example = new Thread(new ThreadStart(CalledMethod)); _ctl = ctl; } /// <summary> /// start the thread /// </summary> public void startThread() { td_example.Start(); } /// <summary> /// pause the thread /// </summary> public void suspendThread() { td_example.Suspend(); } /// <summary> /// stop the thread /// Don't forget release the thread by this method /// sometimes, the thread willn't exit and still run after the form closed /// so, I suggest that to call this method before you want to close the main form. /// </summary> public void abortThread() { td_example.Abort(); } int i = 0; private void CalledMethod() { // // TODO:write your code here. // while (true) { i++; System.Windows.Forms.MessageBox.Show(i.ToString()); Thread.Sleep(1000); } } } good luck!

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #10

          Hi, I fail to see what MultiThread is bringing, it only exposes some methods of Thread. BTW: it is a bad idea to abort a Thread, threads should be canceled in a cooperative way as much as possible; and Thread.Suspend is obsolete. Anyway, the heart of the matter will be reading and timing the COM ports as accurately as possible, which depends on the protocol or message format being used. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Happy New Year to all.
          We hope 2010 soon brings us automatic PRE tags!
          Until then, please insert them manually.


          1 Reply Last reply
          0
          • A AlexB47

            Yes, in theory ... but I don't know what problem can generate a COM PORT EVENT ...

            Alex

            I Offline
            I Offline
            Ilia Blank
            wrote on last edited by
            #11

            Assuming that you have three files with full history of the race for each horse from start to finish and you are requested to create animation that simulates the race while preserving proper timing of the race. That what I would do 1. Load each file into corresponding list. Each list element represents position of the horse and timing of this position. Each list is sorted by time. 2. Merge three arrays into one list. Each list element represent time and position of all three horses at this time. The list is sorted by time. 3. Develop UI control that is capable of painting one race frame based on individual element from array created on step 2. Development of such control might be challenging but in general you have to assure that method called to draw particular frame “HorseRaceArenaControl::DrawRaceFrame (horse1Position, horse2Position, horse3Position ) “ can be called from arbitrary thread. The topics that you have to explore are Control::Invoke, Control::OnPaint, “implementing own-drown control”, 4. To call DrawRaceFrame method for each element in the list created in step 2. The method has to be called with time intervals as recorded in each element. You can use .Net System.Threading.Timer class to assure proper timing. I realize that this is not detailed instruction but rather general guidelines. Hopefully it can serve as starting point.

            A 1 Reply Last reply
            0
            • A AlexB47

              HI All, I need to write a routine that read, at same time, 3 files and raise each string on 3 separate event handler. I think that the correct way to solve this problem is multithread. It's correct? Can you help me with an example? Thanks a lot.

              Alex

              A Offline
              A Offline
              Alaric Dailey
              wrote on last edited by
              #12

              Is something like this what you are looking for? (this example will allow you to read as many files as you like)

              using System;
              using System.IO;
              using System.Threading;
              using System.Windows.Forms;

              namespace MultiThreadedFileRead
              {
              public partial class Form1 : Form
              {
              private readonly object _syncRoot = new object();

                  public Form1()
                  {
                      InitializeComponent();
                  }
              
                  private void button1\_Click(object sender, EventArgs e)
                  {
                      if (openFileDialog1.ShowDialog() == DialogResult.OK)
                      {
                          listBox1.Items.Add(openFileDialog1.FileName);
                      }
                  }
              
                  private void button2\_Click(object sender, EventArgs e)
                  {
                      foreach (object file in listBox1.Items)
                      {
                          ThreadPool.QueueUserWorkItem(ThreadFunc, file);
                      }
                  }
              
              
                  private void WouldBeEventHandler(string s)
                  {
                      if (InvokeRequired)
                      {
                          Action<string> d = WouldBeEventHandler;
                          BeginInvoke(d, new object\[\]{s});
                          return;
                      }
              
                      lock (\_syncRoot)
                      {
                          listBox2.Items.Add(s);
                      }
                  }
              
                  private void ThreadFunc(object state)
                  {
                      string filename = state as string;
                      if (!string.IsNullOrEmpty(filename))
                      {
                          using (StreamReader fs = File.OpenText(filename))
                          {
                              while (!fs.EndOfStream)
                              {
                                  WouldBeEventHandler(fs.ReadLine());
                                  Application.DoEvents();
                              }
                          }
                      }
                  }
              }
              

              }

              A 1 Reply Last reply
              0
              • A AlexB47

                the class is 1 for all the 3 istance?

                Alex

                W Offline
                W Offline
                wjp_auhtm
                wrote on last edited by
                #13

                You can instantiate the class to a variable. Then use the variable which instantiated to open each files. I just write a simple example for you. And, this is only a guiding idea. I consider that the more problems solved by oneself the more he/she learned. There was an old sentence, in our country, said that,"Teaching someone fishing than giving he/she fishes." I suggest that trying each one's answer and google it,I think you will learn more by yourself. I am sorry for my poor English. Good Luck!

                1 Reply Last reply
                0
                • I Ilia Blank

                  Assuming that you have three files with full history of the race for each horse from start to finish and you are requested to create animation that simulates the race while preserving proper timing of the race. That what I would do 1. Load each file into corresponding list. Each list element represents position of the horse and timing of this position. Each list is sorted by time. 2. Merge three arrays into one list. Each list element represent time and position of all three horses at this time. The list is sorted by time. 3. Develop UI control that is capable of painting one race frame based on individual element from array created on step 2. Development of such control might be challenging but in general you have to assure that method called to draw particular frame “HorseRaceArenaControl::DrawRaceFrame (horse1Position, horse2Position, horse3Position ) “ can be called from arbitrary thread. The topics that you have to explore are Control::Invoke, Control::OnPaint, “implementing own-drown control”, 4. To call DrawRaceFrame method for each element in the list created in step 2. The method has to be called with time intervals as recorded in each element. You can use .Net System.Threading.Timer class to assure proper timing. I realize that this is not detailed instruction but rather general guidelines. Hopefully it can serve as starting point.

                  A Offline
                  A Offline
                  AlexB47
                  wrote on last edited by
                  #14

                  Thanks, this is good solution for post-processing files ... but in real time?

                  Alex

                  I 1 Reply Last reply
                  0
                  • A Alaric Dailey

                    Is something like this what you are looking for? (this example will allow you to read as many files as you like)

                    using System;
                    using System.IO;
                    using System.Threading;
                    using System.Windows.Forms;

                    namespace MultiThreadedFileRead
                    {
                    public partial class Form1 : Form
                    {
                    private readonly object _syncRoot = new object();

                        public Form1()
                        {
                            InitializeComponent();
                        }
                    
                        private void button1\_Click(object sender, EventArgs e)
                        {
                            if (openFileDialog1.ShowDialog() == DialogResult.OK)
                            {
                                listBox1.Items.Add(openFileDialog1.FileName);
                            }
                        }
                    
                        private void button2\_Click(object sender, EventArgs e)
                        {
                            foreach (object file in listBox1.Items)
                            {
                                ThreadPool.QueueUserWorkItem(ThreadFunc, file);
                            }
                        }
                    
                    
                        private void WouldBeEventHandler(string s)
                        {
                            if (InvokeRequired)
                            {
                                Action<string> d = WouldBeEventHandler;
                                BeginInvoke(d, new object\[\]{s});
                                return;
                            }
                    
                            lock (\_syncRoot)
                            {
                                listBox2.Items.Add(s);
                            }
                        }
                    
                        private void ThreadFunc(object state)
                        {
                            string filename = state as string;
                            if (!string.IsNullOrEmpty(filename))
                            {
                                using (StreamReader fs = File.OpenText(filename))
                                {
                                    while (!fs.EndOfStream)
                                    {
                                        WouldBeEventHandler(fs.ReadLine());
                                        Application.DoEvents();
                                    }
                                }
                            }
                        }
                    }
                    

                    }

                    A Offline
                    A Offline
                    AlexB47
                    wrote on last edited by
                    #15

                    thanks ... this is ok, but if i need to process in real time (string in file passed from PORT COM in real time...)?

                    Alex

                    A 1 Reply Last reply
                    0
                    • A AlexB47

                      thanks ... this is ok, but if i need to process in real time (string in file passed from PORT COM in real time...)?

                      Alex

                      A Offline
                      A Offline
                      Alaric Dailey
                      wrote on last edited by
                      #16

                      This is a serialport example, since you keep mentioning COM PORTS I am supposing this is what you are looking for. I will be happy to help out more, but if this still isn't what you need, a better description of what you need in is order.

                      using System;
                      using System.IO.Ports;
                      using System.Threading;
                      using System.Windows.Forms;

                      namespace MultiThreadedFileRead
                      {
                      public partial class Form1 : Form
                      {
                      private readonly object _syncRoot = new object();

                          public Form1()
                          {
                              InitializeComponent();
                          }
                      
                      
                          private void Form1\_Load(object sender, EventArgs e)
                          {
                              serialPort1.DataReceived += SerialPort1DataReceived;
                              serialPort2.DataReceived += SerialPort2DataReceived;
                              serialPort3.DataReceived += SerialPort3DataReceived;
                          }
                      
                          private void SerialPort1DataReceived(object sender, SerialDataReceivedEventArgs e)
                          {
                              ThreadPool.QueueUserWorkItem(ThreadFunc, sender);
                          }
                      
                          private void SerialPort2DataReceived(object sender, SerialDataReceivedEventArgs e)
                          {
                              ThreadPool.QueueUserWorkItem(ThreadFunc, sender);
                          }
                      
                          private void SerialPort3DataReceived(object sender, SerialDataReceivedEventArgs e)
                          {
                              ThreadPool.QueueUserWorkItem(ThreadFunc, sender);
                          }
                      
                          private void ThreadFunc(object state)
                          {
                              SerialPort port = state as SerialPort;
                              if (port != null)
                              {
                                  try
                                  {
                                      //do processing here.
                                      
                                  }
                                  catch (Exception)
                                  {
                                      //don't let the thread throw directly
                                  }
                              }
                          }
                      }
                      

                      }

                      1 Reply Last reply
                      0
                      • A AlexB47

                        Thanks, this is good solution for post-processing files ... but in real time?

                        Alex

                        I Offline
                        I Offline
                        Ilia Blank
                        wrote on last edited by
                        #17

                        Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?

                        A I 2 Replies Last reply
                        0
                        • I Ilia Blank

                          Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?

                          A Offline
                          A Offline
                          AlexB47
                          wrote on last edited by
                          #18

                          example ... 3 GPS-GSM that trasfer horse position in real time. I connect a modem receiver to my PC, open a COM PORT for it, ad using, in real time, a sentences that I receive. For any horse.

                          Alex

                          1 Reply Last reply
                          0
                          • I Ilia Blank

                            Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?

                            I Offline
                            I Offline
                            Ilia Blank
                            wrote on last edited by
                            #19

                            Sounds as horse position is embedded inside com3 message. Am I right?

                            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