Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all I have a client program about send and recieve file on LAN Network between 2 PC, but it get an error below: "Cross-thread operation not valid: Control 'Client' accessed from a thread other than the thread it was created on" in function SaveFile in line "if (saveFileDialog1.ShowDialog(this) == DialogResult.OK" So I need a help to fix it, Please help me, Thank very much.

It is source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Threading;

#pragma warning disable

namespace GuiClient
{
    public partial class Client : Form
    {
        public delegate void UpdateListBoxCallBack(string s);
        public delegate void UpdateLabelCallBack(string s);
        private Stream stmReader = null;
        private NetworkStream nwkStream = null;
        private Stream stmWriter = null;
        private TcpClient tcpClient = null;
        SaveFileDialog saveFileDialog = new SaveFileDialog();
        public Client()
        {
            InitializeComponent();
        }
        private void Save(object sender, EventArgs e)
        {
           Thread t = new Thread(new ThreadStart(SaveFile));
            t.Start();
        }
        public void SaveFile()
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                txtFileName.Text = saveFileDialog1.FileName;
                set(null);
            }
            try
            {
                nwkStream = tcpClient.GetStream();
                stmReader = nwkStream;
                stmWriter = File.OpenWrite(txtFileName.Text);
                byte[] buff = new byte[1024];//1073741824
                int len = 0;
                set("Receiving");

                while ((len = stmReader.Read(buff, 0, 1024)) > 0)
                {
                    stmWriter.Write(buff, 0, len);
                    stmWriter.Flush();
                }
                set("File has received succesfully!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
            }
            
            finally
            {
                nwkStream.Close();
                stmWriter.Close();
                stmReader.Close();
            }
        }

        public void Start(object sender, EventArgs e)
        {
            txtFileName.Clear();
            IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(ipserver.Text), int.Parse(port.Text));
            tcpClient = new TcpClient();
            tcpClient.Connect(ipe);
            StreamReader sr = new StreamReader(tcpClient.GetStream());
            StreamWriter sw = new StreamWriter(tcpClient.GetStream());
            string duongdan = sr.ReadLine() ;
            textBox1.Text = "Server send file : " + duongdan;
            saveFileDialog1.FileName = duongdan;
            saveFileDialog1.Title = "Save file from Server";
            saveFileDialog1.Filter = "Text Files (*.txt)|*.txt|Word Documents" + "(*.doc)|*.doc|All Files (*.*)|*.*";
            saveFileDialog1.ShowHelp = true;
        }
        void set(string s)
        {
            if (InvokeRequired)
            {
                object[] pList = { s };
                lblMessage.BeginInvoke(new UpdateListBoxCallBack(OnUpdateLabel), pList);
            }
            else
            {
                OnUpdateLabel(s);
            }
        }
        private void OnUpdateLabel(String s)
        {
            lblMessage.Text = s;
        }

        private void clearip(object sender, EventArgs e)
        {
            ipserver.Clear();
        }

        private void clearport(object sender, EventArgs e)
        {
            port.Clear();
        }

        private void clearfilename(object sender, EventArgs e)
        {
            txtFileName.Clear();
        }

        private void exit(object sender, EventArgs e)
        {
            Close();
        }

        private void disconectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            nwkStream.Close();
            stmWriter.Close();
            stmReader.Close();
        }

        private void Client_Load(object sender, EventArgs e)
        {

        }
    }
}


It is my project:
http://www.mediafire.com/?xprt10z40wrjvw7[^]
Posted

1 solution

By passing
C#
this
to
C#
saveFileDialog1.ShowDialog(this)
you are using an object on 1 thread that was created on another thread.

You'll need to write a thread-safe method in the Client class to give your worker thread access to anything you want to use in the Client class.
 
Share this answer
 
Comments
Nelek 5-Dec-12 16:53pm    
OP's comment moved here from non-solution
I haven't still understand, can you guide in a slow way and clear or evident ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900