Hỏi đáp

Chia sẻ kiến thức, cùng nhau phát triển

File Stream Error

14:23 12-01-2018 400 lượt xem 1 bình luận 18:39 12-01-2018

Khi em dùng file stream đã có lệnh Close() mà sao nó vẫn nói là Bị dùng bởi 1 tiến trình khác

Code

namespace Sticky_Note
{
    public partial class Form1 : Form
    {
#region This isnt my code (QuangamingVn) I copy it from Stack Overflow :(
        //This isnt my code (QuangamingVn) I copy it from Stack Overflow :(
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();
#endregion

        
        public Form1(object tag)
        {
            Tag = tag;
            InitializeComponent();
            LoadCustomComponent();
            WriteLocation();
        }

        //For controls I want to add by code
        void LoadCustomComponent()
        {
            //Defalut Location
            Location = new Point(100, 100);

            //Menu Strip
            fakeControl.BackColor = Color.Indigo;

            //Item On Menu Strip
            xToolStripMenuItem.Alignment = ToolStripItemAlignment.Right;
            xToolStripMenuItem.ForeColor = Color.Orange;
            toolStripMenuItem1.ForeColor = Color.Orange;
            //Timer
            timer1.Start();
            //File
            if (!Directory.Exists(SharedIems.path + @"fileID" + Tag + ".dat"))
            {
                FileStream fs = new FileStream(SharedIems.path + @"fileID" + Tag + ".dat", FileMode.Create);
                fs.Close();
            }
        }
        
        
        
        private void fakeControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
            WriteLocation();
        }
        private void WriteLocation()
        {
            FileStream saveFile = new FileStream(SharedIems.path + @"fileID" + Tag + ".dat", FileMode.Open);
            saveFile.Position = 0;
            string save = "Location: " + Location.X.ToString() + " , " + Location.Y.ToString() + "\r\n";
            byte[] saveByte = Encoding.Default.GetBytes(save);
            saveFile.Write(saveByte, 0, saveByte.Length);
            saveFile.Close();

            WriteSize();
        }
        private void WriteSize()
        {
            FileStream saveFile = new FileStream(SharedIems.path + @"fileID" + Tag + ".dat", FileMode.Open);
            saveFile.Position = 21;
            string save = "Size: " + Size.Width.ToString() + " , " + Size.Height.ToString() + "\r\n";
            byte[] saveByte = Encoding.Default.GetBytes(save);
            saveFile.Write(saveByte, 0, saveByte.Length);
            saveFile.Close();
        }
        //To close/delete the note
        private void xToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Do you want to delete this note?", "Delete Note", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                Close();
                File.Delete(SharedIems.path + @"fileID" + Tag + ".dat");
                File.Delete(SharedIems.path + @"fileID" + Tag + ".txt");
                SharedIems.openForm--;
                
                SharedIems.ZeroForm();
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            SharedIems.CreateNote();
        }

        private void txbInput_TextChanged(object sender, EventArgs e)
        {
            byte[] realTimeSave = Encoding.Default.GetBytes(txbInput.Text);
            FileStream fs = new FileStream(SharedIems.path + @"fileID" + Tag + ".txt", FileMode.Create);
            fs.Write(realTimeSave, 0, realTimeSave.Length);
            fs.Close();
        }


        #region Resize On stack overflow
        // Again, I didnt code this
        protected override void WndProc(ref Message m)
        {
            const int RESIZE_HANDLE_SIZE = 10;

            switch (m.Msg)
            {
                case 0x0084/*NCHITTEST*/ :
                    base.WndProc(ref m);

                    if ((int)m.Result == 0x01/*HTCLIENT*/)
                    {
                        Point screenPoint = new Point(m.LParam.ToInt32());
                        Point clientPoint = this.PointToClient(screenPoint);
                        if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)13/*HTTOPLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)12/*HTTOP*/ ;
                            else
                                m.Result = (IntPtr)14/*HTTOPRIGHT*/ ;
                        }
                        else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)10/*HTLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)2/*HTCAPTION*/ ;
                            else
                                m.Result = (IntPtr)11/*HTRIGHT*/ ;
                        }
                        else
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)16/*HTBOTTOMLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)15/*HTBOTTOM*/ ;
                            else
                                m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                        }
                    }
                    return;
            }
            base.WndProc(ref m);
            
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= 0x20000; // <--- use 0x20000
                return cp;
            }
        }
        //
        #endregion

        bool t = false;
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (t)
            {
                //Make the textbox follow the form size
                txbInput.Size = new Size(Size.Width - 22, Size.Height - 41);

                
                
                    WriteSize();
                    string sizeDetail = SharedIems.GetInFile(Convert.ToInt32(Tag), "Size");
                    //Get the file information to varibles
                    sizeDetail = sizeDetail.Substring(6);
                    string[] sp = sizeDetail.Split(',');
                    int x = Convert.ToInt32(sp[0].Trim());
                    int y = Convert.ToInt32(sp[1].Trim());
                
            }

        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            t = true;
            timer1.Stop();
        }
    }
}

P/s: Từ đầu code vẫn chạy ổn như sau khi Copy/Paste cái code từ Stack OverFlow thứ 2 thì nó lại bị cái lỗi đấy

Là cái này

protected override void WndProc(ref Message m)
        {
            const int RESIZE_HANDLE_SIZE = 10;

            switch (m.Msg)
            {
                case 0x0084/*NCHITTEST*/ :
                    base.WndProc(ref m);

                    if ((int)m.Result == 0x01/*HTCLIENT*/)
                    {
                        Point screenPoint = new Point(m.LParam.ToInt32());
                        Point clientPoint = this.PointToClient(screenPoint);
                        if (clientPoint.Y <= RESIZE_HANDLE_SIZE)
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)13/*HTTOPLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)12/*HTTOP*/ ;
                            else
                                m.Result = (IntPtr)14/*HTTOPRIGHT*/ ;
                        }
                        else if (clientPoint.Y <= (Size.Height - RESIZE_HANDLE_SIZE))
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)10/*HTLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)2/*HTCAPTION*/ ;
                            else
                                m.Result = (IntPtr)11/*HTRIGHT*/ ;
                        }
                        else
                        {
                            if (clientPoint.X <= RESIZE_HANDLE_SIZE)
                                m.Result = (IntPtr)16/*HTBOTTOMLEFT*/ ;
                            else if (clientPoint.X < (Size.Width - RESIZE_HANDLE_SIZE))
                                m.Result = (IntPtr)15/*HTBOTTOM*/ ;
                            else
                                m.Result = (IntPtr)17/*HTBOTTOMRIGHT*/ ;
                        }
                    }
                    return;
            }
            base.WndProc(ref m);
            
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.Style |= 0x20000; // <--- use 0x20000
                return cp;
            }
        }

Bình luận

Để bình luận, bạn cần đăng nhập bằng tài khoản Howkteam.

Đăng nhập
C# learner đã bình luận 18:07 12-01-2018

Tình hình này chắc phải lên stack overflow hỏi lại thằng viết code rồi

Mình viết chi tiết hơn trên Stack Overflow

Câu hỏi mới nhất