關於.net(C#)中的跨進程訪問的問題

來源:互聯網
上載者:User

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()
{
//第一步:發現檔案夾下面的檔案及數量
DirectoryInfo myFolder = new DirectoryInfo("bcd");
FileInfo[] myFiles = myFolder.GetFiles();
this.richTextBox1.Text = "從檔案夾中發現了:" + myFiles.Length.ToString() + " 個檔案!" + "\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;
//進度條
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) + "%";
//上傳實際的資料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;
}
}
}
在此代碼中,對一個控制項的訪問如: this.richTextBox1.Text = myFiles[i].Name + "\n" + this.richTextBox1.Text; 僅限在一個線程中,如果在兩個進程中對控制項訪問將會出錯!如不能在程式自己的線程 和 uploadImageToDB 線程中訪問。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.