Esempio n. 1
0
        private void writer(CancellationToken cancellation, ParallelFileWriter fileWriter, int id)
        {
            int index = 0;

            while (!cancellation.IsCancellationRequested)
            {
                string text = string.Format("{0}:{1}\n", id, index++);
                byte[] data = Encoding.UTF8.GetBytes(text);
                fileWriter.Write(data);
            }
        }
Esempio n. 2
0
 private void run()
 {
     // For demo purposes, cancel after a couple of seconds.
     using (var fileWriter = new ParallelFileWriter(@"C:\TEST\TEST.DATA", 100))
         using (var cancellationSource = new CancellationTokenSource(2000))
         {
             const int NUM_THREADS = 8;
             Action[]  actions     = new Action[NUM_THREADS];
             for (int i = 0; i < NUM_THREADS; ++i)
             {
                 int id = i;
                 actions[i] = () => writer(cancellationSource.Token, fileWriter, id);
             }
             Parallel.Invoke(actions);
         }
 }