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 线程中访问。
{
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 线程中访问。
标签:
.net,C#,跨进程访问
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“关于.net(C#)中的跨进程访问的问题”评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。