關(guān)于.net(C#)中的跨進(jìn)程訪問的問題
更新時(shí)間:2009年04月13日 11:47:57 作者:
C# 跨進(jìn)程訪問實(shí)現(xiàn)代碼。
namespace process_image
{
public partial class jszg_upload : Form
{
static bool stop_flag = false;
public jszg_upload()
{
InitializeComponent();
}
private void upload_button1_Click(object sender, EventArgs e)
{
stop_flag = false;
if (this.checkBox1.Checked)
{
String connectionString = "連接串";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(" update my_jszg set filemime=null, filebody = null", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
new Thread(uploadImageToDB).Start();
}//end upload_button1_Click
void uploadImageToDB()
{
//第一步:發(fā)現(xiàn)文件夾下面的文件及數(shù)量
DirectoryInfo myFolder = new DirectoryInfo("bcd");
FileInfo[] myFiles = myFolder.GetFiles();
this.richTextBox1.Text = "從文件夾中發(fā)現(xiàn)了:" + myFiles.Length.ToString() + " 個(gè)文件!" + "\n";
this.jindutiao_progressBar1.Maximum = myFiles.Length;
//第二步:開始上傳文件
String connectionString = "連接串";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
for (int i = 0; i < myFiles.Length; i++)
{
if(stop_flag)
break;
//進(jìn)度條
this.jindutiao_progressBar1.Value = i + 1;
this.pictureBox1.ImageLocation = myFiles[i].FullName;
this.richTextBox1.Text = myFiles[i].Name + "\n" + this.richTextBox1.Text;
this.baifenbi_label1.Text = ((((i + 1) * 1.0) /myFiles.Length) * 100) + "%";
//上傳實(shí)際的數(shù)據(jù)image/jpeg
SqlCommand cmd = new SqlCommand(" update my_jszg set filemime='image/jpeg', filebody = @myfilebody from my_jszg where 證件號碼=@myzjhm", conn);
byte[] fb = new byte[myFiles[i].Length];
BinaryReader br = new BinaryReader(myFiles[i].OpenRead());
br.Read(fb, 0, (int)myFiles[i].Length);
cmd.Parameters.AddWithValue("@myfilebody", fb);
cmd.Parameters.AddWithValue("@myzjhm", myFiles[i].Name.Substring(0, myFiles[i].Name.LastIndexOf('.')));
cmd.ExecuteNonQuery();
br.Close();
}//end for
conn.Close();
}
MessageBox.Show("所有的文件上傳完畢!");
}
//停止上傳
private void stop_button_Click(object sender, EventArgs e)
{
stop_flag=true;
}
}
}
在此代碼中,對一個(gè)控件的訪問如: this.richTextBox1.Text = myFiles[i].Name + "\n" + this.richTextBox1.Text; 僅限在一個(gè)線程中,如果在兩個(gè)進(jìn)程中對控件訪問將會出錯(cuò)!如不能在程序自己的線程 和 uploadImageToDB 線程中訪問。
{
public partial class jszg_upload : Form
{
static bool stop_flag = false;
public jszg_upload()
{
InitializeComponent();
}
private void upload_button1_Click(object sender, EventArgs e)
{
stop_flag = false;
if (this.checkBox1.Checked)
{
String connectionString = "連接串";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
SqlCommand cmd = new SqlCommand(" update my_jszg set filemime=null, filebody = null", conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
new Thread(uploadImageToDB).Start();
}//end upload_button1_Click
void uploadImageToDB()
{
//第一步:發(fā)現(xiàn)文件夾下面的文件及數(shù)量
DirectoryInfo myFolder = new DirectoryInfo("bcd");
FileInfo[] myFiles = myFolder.GetFiles();
this.richTextBox1.Text = "從文件夾中發(fā)現(xiàn)了:" + myFiles.Length.ToString() + " 個(gè)文件!" + "\n";
this.jindutiao_progressBar1.Maximum = myFiles.Length;
//第二步:開始上傳文件
String connectionString = "連接串";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
for (int i = 0; i < myFiles.Length; i++)
{
if(stop_flag)
break;
//進(jìn)度條
this.jindutiao_progressBar1.Value = i + 1;
this.pictureBox1.ImageLocation = myFiles[i].FullName;
this.richTextBox1.Text = myFiles[i].Name + "\n" + this.richTextBox1.Text;
this.baifenbi_label1.Text = ((((i + 1) * 1.0) /myFiles.Length) * 100) + "%";
//上傳實(shí)際的數(shù)據(jù)image/jpeg
SqlCommand cmd = new SqlCommand(" update my_jszg set filemime='image/jpeg', filebody = @myfilebody from my_jszg where 證件號碼=@myzjhm", conn);
byte[] fb = new byte[myFiles[i].Length];
BinaryReader br = new BinaryReader(myFiles[i].OpenRead());
br.Read(fb, 0, (int)myFiles[i].Length);
cmd.Parameters.AddWithValue("@myfilebody", fb);
cmd.Parameters.AddWithValue("@myzjhm", myFiles[i].Name.Substring(0, myFiles[i].Name.LastIndexOf('.')));
cmd.ExecuteNonQuery();
br.Close();
}//end for
conn.Close();
}
MessageBox.Show("所有的文件上傳完畢!");
}
//停止上傳
private void stop_button_Click(object sender, EventArgs e)
{
stop_flag=true;
}
}
}
在此代碼中,對一個(gè)控件的訪問如: this.richTextBox1.Text = myFiles[i].Name + "\n" + this.richTextBox1.Text; 僅限在一個(gè)線程中,如果在兩個(gè)進(jìn)程中對控件訪問將會出錯(cuò)!如不能在程序自己的線程 和 uploadImageToDB 線程中訪問。
相關(guān)文章
淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結(jié)
這篇文章主要介紹了淺談ASP.NET常用數(shù)據(jù)綁定控件優(yōu)劣總結(jié),以便在實(shí)際的開發(fā)中選用合適的控件進(jìn)行數(shù)據(jù)綁定,以提高開發(fā)效率。2016-12-12.net中自定義錯(cuò)誤頁面的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于.net中自定義錯(cuò)誤頁面實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06Web API身份認(rèn)證解決方案之Basic基礎(chǔ)認(rèn)證
本文詳細(xì)講解了Web API身份認(rèn)證解決方案之Basic基礎(chǔ)認(rèn)證,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03asp.net及javascript判斷是否手機(jī)訪問的方法
這篇文章主要介紹了asp.net及javascript判斷是否手機(jī)訪問的方法,結(jié)合實(shí)例形式對比分析了asp.net及javascript實(shí)現(xiàn)判斷訪問端類型的相關(guān)技巧,需要的朋友可以參考下2016-06-06javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼
javascript實(shí)現(xiàn)listbox左右移動(dòng)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05