Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Is my code bugged cause it stalls sometimes???

$
0
0

Hey all,

 Longtime no see and problems untill now. I am really enjoying C#, so much less mess than C++ and kinda smarter

than C. Well, anyways... I am once again in need of all your Guru expertise; Here are my concerns/problems...

1. How to change STA to MTA once debug is finalized for Release or is it automatic?

2. My current project, code below, has something wrong with it. Sometimes works fine and other times stalls, why?

3. As above, please evoluate and recommend, comment, suggest, purpose as you see fit about my code. But, please

 keep the flaming to a minimal amount.

Thanks and here it is, using VS 2005 C#, yeah I know :p And I included a pic of the Form for you to mockup.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Scale
{
    public struct ByteTable
    {
        private bool   flag;
        private byte   data;
        private UInt32 count;
        private UInt32 index;

        public bool Flag
        {
            get { return flag; }
            set { flag = value; }
        }

        public byte Data
        {
            get { return data; }
            set { data = value; }
        }

        public UInt32 Count
        {
            get { return count; }
            set { count += value; }
        }

        public UInt32 Index
        {
            get { return index; }
            set { index = value; }
        }
    }

    public partial class Form1 : Form
    {
        public int bufferSize;
        public byte[] buffer;
        public ByteTable[] BT;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bufferSize = 8192;
            buffer = new byte[bufferSize];

            CenterToScreen();

            label1.Text = "";
            label2.Text = "Block: ";
            label3.Text = "Block Size: " + bufferSize.ToString() + " (bytes)";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = Environment.SpecialFolder.Desktop.ToString();
            openFileDialog1.Filter = "All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                label1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int  i, x, y;
            UInt32 avg, upper, k, lower;
            long f, m, n, o, p;
            float a;
            string s = label1.Text.ToString();
            Graphics g;

            if (s != "")
            {
                BinaryReader BR = new BinaryReader(File.OpenRead( s));
                ByteTable[] BT = new ByteTable[256];

                a = 0.0f;

                f = BR.BaseStream.Length;

                o = m = f / bufferSize;
                n = f - (m * bufferSize);

                progressBar1.Minimum = progressBar1.Value = 0;
                progressBar1.Maximum = (int) o;
                progressBar1.Step = 1;                

                do
                {
                    buffer = BR.ReadBytes(bufferSize);

                    PopulateByteTableMajor( BT );

                    progressBar1.PerformStep();

                    p = o - m;

                    a = ((float) progressBar1.Value / (float) progressBar1.Maximum) * 100;

                    label2.Text = "Block: " + p.ToString() + " (" + a.ToString() + " %)   ";
                    label2.Refresh();

                    m--;

                } while (m > 0);

                buffer = BR.ReadBytes( (int) n);

                populateByteTableMinor( n, BT );

                BR.Close();

                avg = upper = lower = BT[0].Count;

                for (i = 1; i < 256; i++)
                {
                    avg += BT[i].Count;

                    if (BT[i].Count < lower)
                        lower = BT[i].Count;

                    if (BT[i].Count > upper)
                        upper = BT[i].Count;
                }

                avg /= 256;

                m = upper - lower;

                k = (uint) m / avg;

                g = pictureBox1.CreateGraphics();

                for (i = 0; i < 256; i++)
                {
                    x = (BT[i].Data * 2) + 2;

                    y = (int) (BT[i].Count / k);

                    g.DrawLine(Pens.Red, x, pictureBox1.Height - y, x, pictureBox1.Height);
                }

                g.Dispose();
            }
        }

        private void updateByteTable( ByteTable[] BT, int i )
        {
            byte j;

            j = buffer[ i ];

            if ( BT[ j ].Flag )
            {
                BT[ j ].Count = 1;
            }
            else
            {
                BT[ j ].Count = 1;
                BT[ j ].Data = j;
                BT[ j ].Flag = true;
            }

        }        

        private void populateByteTableMinor( long n, ByteTable[] BT )
        {
            int i;

            for ( i = 0; i < (int)n; i++ )
            {
                updateByteTable( BT, i );
            }
        }

        private void PopulateByteTableMajor( ByteTable[] BT )
        {
            int  i;

            for ( i = 0; i < bufferSize; i++ )
            {
                updateByteTable( BT, i );
            }
        }        
    }
}

Hope all this info helps, I just can not see where my code has problems. This project does read huge

amounts of data, 8K chunks. So, maybe its an I/O issue. Well, thanks for looking and helping.


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>