write file need to optimised for heavy traffic

后端 未结 3 788
一个人的身影
一个人的身影 2021-01-23 14:42

i am very new to C#, and this is my first question, please be gentle on me

I am trying to write a application to capture some tick data from the data provider, below is

3条回答
  •  一个人的身影
    2021-01-23 15:47

    You need to create a field for the stream instead of a local variable. Initialize it in constructor once and don't forget to close it somewhere. It's better to implement IDisposable interface and close the stream in Dispose() method.

    IDisposable

    class MyClass : IDisposable {
        private StreamWriter _writer;
    
        MyClass() {
            _writer = File.App.....;
        }
    
        void zf_TickEvent(object sender, ZenFire.TickEventArgs e)
        {
    
            output myoutput = new output();
    
            myoutput.time = e.TimeStamp;
            myoutput.product = e.Product.ToString();
            myoutput.type = Enum.GetName(typeof(ZenFire.TickType), e.Type);
            myoutput.price = e.Price;
            myoutput.volume = e.Volume;
    
    
            _writer.Write(myoutput.time.ToString(timeFmt) + ",");
            _writer.Write(myoutput.product + "," );
            _writer.Write(myoutput.type + "," );
            _writer.Write(myoutput.price + ",");
            _writer.Write(myoutput.volume + ",");
    
        }
    
        public void Dispose() { /*see the documentation*/ }
    }
    

提交回复
热议问题