MultiThread and File process
-
-
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
Can you provide more details?
-
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
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! -
Can you provide more details?
I need to read, at same time, 3 files that contain info to horse race. This 3 files contain the info for position istant by istant any horse (time info, x and y for draw point on virtual circuit). The start is equal for ALL, and I need to draw the position of any horse at same time ... and know how arrival first, second and three after passing the virtual "end line" ...
Alex
-
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! -
I need to read, at same time, 3 files that contain info to horse race. This 3 files contain the info for position istant by istant any horse (time info, x and y for draw point on virtual circuit). The start is equal for ALL, and I need to draw the position of any horse at same time ... and know how arrival first, second and three after passing the virtual "end line" ...
Alex
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.
-
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.
-
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
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
-
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
-
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!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.
-
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.
-
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
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(); } } } } }
}
-
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!
-
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.
-
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(); } } } } }
}
-
thanks ... this is ok, but if i need to process in real time (string in file passed from PORT COM in real time...)?
Alex
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 } } } }
}
-
Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?
-
Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?
-
Sorry, I failed to understand last statement. You indicated that you use com3 event to drive “real time” animation. Can you provide more details?
Sounds as horse position is embedded inside com3 message. Am I right?