Deutsch
Germany.ruФорумы → Архив Досок→ Программирование

​Немножко вне рутины...

22.05.17 11:25
Re: ​Немножко вне рутины...
 
Murr патриот
Murr
в ответ Murr 19.05.17 14:30

Поменял пока так:


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ThreadStaticTest
{
class A
{
[ThreadStatic]
static StreamWriter sw = null;
public StreamWriter Sw
{
get
{
if (sw == null)
{
// get from class where instantiated/specified.
throw new Exception("Output stream is null or not specified.");
}
return sw;
}
set { sw = value; }
}
}


class B1 : A
{
String b1 = "B1";
public void GetResult()
{
Sw.Write(b1);


B2 b2 = new B2();
b2.GetResult();
}
}


class B2 : A
{
String b2 = "B2";
public void GetResult()
{
Sw.Write(b2);
}
}

class Process
{
static int threads = 1;

public void Execute()
{
Thread newThread = new Thread(new ThreadStart(this.Generate));
newThread.Name = threads.ToString();
newThread.Start();
}

private void Generate()
{
using (StreamWriter sw = new StreamWriter("Data_" + threads.ToString() + ".dat"))
{
B1 b1 = new B1();
b1.Sw = sw;
b1.GetResult();
}
}
}

class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.Name = "Main";

Process p = new Process();
p.Execute();
}
}
}




Вроде работает. По крайней мере по ексептиону пока не вылетает.

Многопоточку - пока не гонял - надо сделать более-мение нормальный "источник" инстансов Б1 и прикрутить пулл для стреамов.


Будут какие замечания/пожелания/советы?

 

Перейти на