国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:20:23
字體:
來源:轉載
供稿:網友
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 線程中訪問。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 津南区| 宝清县| 绥江县| 武穴市| 林芝县| 交城县| 陵水| 奉化市| 定州市| 德昌县| 马尔康县| 彩票| 临泉县| 长子县| 林周县| 漯河市| 福鼎市| 道真| 慈利县| 伊川县| 车险| 榆社县| 明水县| 南江县| 彭阳县| 塔河县| 安国市| 平泉县| 石景山区| 裕民县| 石阡县| 金塔县| 樟树市| 泰来县| 广宗县| 光山县| 聊城市| 天水市| 佳木斯市| 石台县| 葫芦岛市|