EDN Admin
Well-known member
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.<br/>
<br/>
It is source code:
<pre id="x_pre0" style=" <span class="x_code-keyword using System;
<span class="x_code-keyword using System.Collections.Generic;
<span class="x_code-keyword using System.ComponentModel;
<span class="x_code-keyword using System.Data;
<span class="x_code-keyword using System.Drawing;
<span class="x_code-keyword using System.IO;
<span class="x_code-keyword using System.Net.Sockets;
<span class="x_code-keyword using System.Net;
<span class="x_code-keyword using System.Text;
<span class="x_code-keyword using System.Windows.Forms;
<span class="x_code-keyword using System.Threading;
<span class="x_code-preprocessor #pragma warning disable
<span class="x_code-keyword namespace GuiClient
{
<span class="x_code-keyword public <span class="x_code-keyword partial <span class="x_code-keyword class Client : Form
{
<span class="x_code-keyword public <span class="x_code-keyword delegate <span class="x_code-keyword void UpdateListBoxCallBack(<span class="x_code-keyword string s);
<span class="x_code-keyword public <span class="x_code-keyword delegate <span class="x_code-keyword void UpdateLabelCallBack(<span class="x_code-keyword string s);
<span class="x_code-keyword private Stream stmReader = <span class="x_code-keyword null;
<span class="x_code-keyword private NetworkStream nwkStream = <span class="x_code-keyword null;
<span class="x_code-keyword private Stream stmWriter = <span class="x_code-keyword null;
<span class="x_code-keyword private TcpClient tcpClient = <span class="x_code-keyword null;
SaveFileDialog saveFileDialog = <span class="x_code-keyword new SaveFileDialog();
<span class="x_code-keyword public Client()
{
InitializeComponent();
}
<span class="x_code-keyword private <span class="x_code-keyword void Save(<span class="x_code-keyword object sender, EventArgs e)
{
Thread t = <span class="x_code-keyword new Thread(<span class="x_code-keyword new ThreadStart(SaveFile));
t.Start();
}
<span class="x_code-keyword public <span class="x_code-keyword void SaveFile()
{
SaveFileDialog saveFileDialog1 = <span class="x_code-keyword new SaveFileDialog();
<span class="x_code-keyword if (saveFileDialog1.ShowDialog(<span class="x_code-keyword this) == DialogResult.OK)
{
txtFileName.Text = saveFileDialog1.FileName;
<span class="x_code-keyword set(<span class="x_code-keyword null);
}
<span class="x_code-keyword try
{
nwkStream = tcpClient.GetStream();
stmReader = nwkStream;
stmWriter = File.OpenWrite(txtFileName.Text);
<span class="x_code-keyword byte[] buff = <span class="x_code-keyword new <span class="x_code-keyword byte[<span class="x_code-digit 1024];<span class="x_code-comment //<span class="x_code-comment 1073741824
<span class="x_code-keyword int len = <span class="x_code-digit 0;
<span class="x_code-keyword set(<span class="x_code-string "<span class="x_code-string Receiving");
<span class="x_code-keyword while ((len = stmReader.Read(buff, <span class="x_code-digit 0, <span class="x_code-digit 1024)) > <span class="x_code-digit 0)
{
stmWriter.Write(buff, <span class="x_code-digit 0, len);
stmWriter.Flush();
}
<span class="x_code-keyword set(<span class="x_code-string "<span class="x_code-string File has received succesfully!");
}
<span class="x_code-keyword catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
<span class="x_code-keyword finally
{
nwkStream.Close();
stmWriter.Close();
stmReader.Close();
}
}
<span class="x_code-keyword public <span class="x_code-keyword void Start(<span class="x_code-keyword object sender, EventArgs e)
{
txtFileName.Clear();
IPEndPoint ipe = <span class="x_code-keyword new IPEndPoint(IPAddress.Parse(ipserver.Text), <span class="x_code-keyword int.Parse(port.Text));
tcpClient = <span class="x_code-keyword new TcpClient();
tcpClient.Connect(ipe);
StreamReader sr = <span class="x_code-keyword new StreamReader(tcpClient.GetStream());
StreamWriter sw = <span class="x_code-keyword new StreamWriter(tcpClient.GetStream());
<span class="x_code-keyword string duongdan = sr.ReadLine() ;
textBox1.Text = <span class="x_code-string "<span class="x_code-string Server send file : " + duongdan;
saveFileDialog1.FileName = duongdan;
saveFileDialog1.Title = <span class="x_code-string "<span class="x_code-string Save file from Server";
saveFileDialog1.Filter = <span class="x_code-string "<span class="x_code-string Text Files (*.txt)|*.txt|Word Documents" + <span class="x_code-string "<span class="x_code-string (*.doc)|*.doc|All Files (*.*)|*.*";
saveFileDialog1.ShowHelp = <span class="x_code-keyword true;
}
<span class="x_code-keyword void <span class="x_code-keyword set(<span class="x_code-keyword string s)
{
<span class="x_code-keyword if (InvokeRequired)
{
<span class="x_code-keyword object[] pList = { s };
lblMessage.BeginInvoke(<span class="x_code-keyword new UpdateListBoxCallBack(OnUpdateLabel), pList);
}
<span class="x_code-keyword else
{
OnUpdateLabel(s);
}
}
<span class="x_code-keyword private <span class="x_code-keyword void OnUpdateLabel(<span class="x_code-SDKkeyword String s)
{
lblMessage.Text = s;
}
<span class="x_code-keyword private <span class="x_code-keyword void clearip(<span class="x_code-keyword object sender, EventArgs e)
{
ipserver.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void clearport(<span class="x_code-keyword object sender, EventArgs e)
{
port.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void clearfilename(<span class="x_code-keyword object sender, EventArgs e)
{
txtFileName.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void exit(<span class="x_code-keyword object sender, EventArgs e)
{
Close();
}
<span class="x_code-keyword private <span class="x_code-keyword void disconectToolStripMenuItem_Click(<span class="x_code-keyword object sender, EventArgs e)
{
nwkStream.Close();
stmWriter.Close();
stmReader.Close();
}
<span class="x_code-keyword private <span class="x_code-keyword void Client_Load(<span class="x_code-keyword object sender, EventArgs e)
{
}
}
}[/code]
It is my project:<br/>
http://www.mediafire.com/?xprt10z40wrjvw7 http://www.mediafire.com/?xprt10z40wrjvw7
View the full article
in line "if (saveFileDialog1.ShowDialog(this) == DialogResult.OK" So I need a help to fix it, Please help me, Thank very much.<br/>
<br/>
It is source code:
<pre id="x_pre0" style=" <span class="x_code-keyword using System;
<span class="x_code-keyword using System.Collections.Generic;
<span class="x_code-keyword using System.ComponentModel;
<span class="x_code-keyword using System.Data;
<span class="x_code-keyword using System.Drawing;
<span class="x_code-keyword using System.IO;
<span class="x_code-keyword using System.Net.Sockets;
<span class="x_code-keyword using System.Net;
<span class="x_code-keyword using System.Text;
<span class="x_code-keyword using System.Windows.Forms;
<span class="x_code-keyword using System.Threading;
<span class="x_code-preprocessor #pragma warning disable
<span class="x_code-keyword namespace GuiClient
{
<span class="x_code-keyword public <span class="x_code-keyword partial <span class="x_code-keyword class Client : Form
{
<span class="x_code-keyword public <span class="x_code-keyword delegate <span class="x_code-keyword void UpdateListBoxCallBack(<span class="x_code-keyword string s);
<span class="x_code-keyword public <span class="x_code-keyword delegate <span class="x_code-keyword void UpdateLabelCallBack(<span class="x_code-keyword string s);
<span class="x_code-keyword private Stream stmReader = <span class="x_code-keyword null;
<span class="x_code-keyword private NetworkStream nwkStream = <span class="x_code-keyword null;
<span class="x_code-keyword private Stream stmWriter = <span class="x_code-keyword null;
<span class="x_code-keyword private TcpClient tcpClient = <span class="x_code-keyword null;
SaveFileDialog saveFileDialog = <span class="x_code-keyword new SaveFileDialog();
<span class="x_code-keyword public Client()
{
InitializeComponent();
}
<span class="x_code-keyword private <span class="x_code-keyword void Save(<span class="x_code-keyword object sender, EventArgs e)
{
Thread t = <span class="x_code-keyword new Thread(<span class="x_code-keyword new ThreadStart(SaveFile));
t.Start();
}
<span class="x_code-keyword public <span class="x_code-keyword void SaveFile()
{
SaveFileDialog saveFileDialog1 = <span class="x_code-keyword new SaveFileDialog();
<span class="x_code-keyword if (saveFileDialog1.ShowDialog(<span class="x_code-keyword this) == DialogResult.OK)
{
txtFileName.Text = saveFileDialog1.FileName;
<span class="x_code-keyword set(<span class="x_code-keyword null);
}
<span class="x_code-keyword try
{
nwkStream = tcpClient.GetStream();
stmReader = nwkStream;
stmWriter = File.OpenWrite(txtFileName.Text);
<span class="x_code-keyword byte[] buff = <span class="x_code-keyword new <span class="x_code-keyword byte[<span class="x_code-digit 1024];<span class="x_code-comment //<span class="x_code-comment 1073741824
<span class="x_code-keyword int len = <span class="x_code-digit 0;
<span class="x_code-keyword set(<span class="x_code-string "<span class="x_code-string Receiving");
<span class="x_code-keyword while ((len = stmReader.Read(buff, <span class="x_code-digit 0, <span class="x_code-digit 1024)) > <span class="x_code-digit 0)
{
stmWriter.Write(buff, <span class="x_code-digit 0, len);
stmWriter.Flush();
}
<span class="x_code-keyword set(<span class="x_code-string "<span class="x_code-string File has received succesfully!");
}
<span class="x_code-keyword catch (Exception ex) {
MessageBox.Show(ex.ToString());
}
<span class="x_code-keyword finally
{
nwkStream.Close();
stmWriter.Close();
stmReader.Close();
}
}
<span class="x_code-keyword public <span class="x_code-keyword void Start(<span class="x_code-keyword object sender, EventArgs e)
{
txtFileName.Clear();
IPEndPoint ipe = <span class="x_code-keyword new IPEndPoint(IPAddress.Parse(ipserver.Text), <span class="x_code-keyword int.Parse(port.Text));
tcpClient = <span class="x_code-keyword new TcpClient();
tcpClient.Connect(ipe);
StreamReader sr = <span class="x_code-keyword new StreamReader(tcpClient.GetStream());
StreamWriter sw = <span class="x_code-keyword new StreamWriter(tcpClient.GetStream());
<span class="x_code-keyword string duongdan = sr.ReadLine() ;
textBox1.Text = <span class="x_code-string "<span class="x_code-string Server send file : " + duongdan;
saveFileDialog1.FileName = duongdan;
saveFileDialog1.Title = <span class="x_code-string "<span class="x_code-string Save file from Server";
saveFileDialog1.Filter = <span class="x_code-string "<span class="x_code-string Text Files (*.txt)|*.txt|Word Documents" + <span class="x_code-string "<span class="x_code-string (*.doc)|*.doc|All Files (*.*)|*.*";
saveFileDialog1.ShowHelp = <span class="x_code-keyword true;
}
<span class="x_code-keyword void <span class="x_code-keyword set(<span class="x_code-keyword string s)
{
<span class="x_code-keyword if (InvokeRequired)
{
<span class="x_code-keyword object[] pList = { s };
lblMessage.BeginInvoke(<span class="x_code-keyword new UpdateListBoxCallBack(OnUpdateLabel), pList);
}
<span class="x_code-keyword else
{
OnUpdateLabel(s);
}
}
<span class="x_code-keyword private <span class="x_code-keyword void OnUpdateLabel(<span class="x_code-SDKkeyword String s)
{
lblMessage.Text = s;
}
<span class="x_code-keyword private <span class="x_code-keyword void clearip(<span class="x_code-keyword object sender, EventArgs e)
{
ipserver.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void clearport(<span class="x_code-keyword object sender, EventArgs e)
{
port.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void clearfilename(<span class="x_code-keyword object sender, EventArgs e)
{
txtFileName.Clear();
}
<span class="x_code-keyword private <span class="x_code-keyword void exit(<span class="x_code-keyword object sender, EventArgs e)
{
Close();
}
<span class="x_code-keyword private <span class="x_code-keyword void disconectToolStripMenuItem_Click(<span class="x_code-keyword object sender, EventArgs e)
{
nwkStream.Close();
stmWriter.Close();
stmReader.Close();
}
<span class="x_code-keyword private <span class="x_code-keyword void Client_Load(<span class="x_code-keyword object sender, EventArgs e)
{
}
}
}[/code]
It is my project:<br/>
http://www.mediafire.com/?xprt10z40wrjvw7 http://www.mediafire.com/?xprt10z40wrjvw7
View the full article