关于.net(C#)中的跨进程访问的问题
程序员文章站
2024-03-11 21:32:43
namespace process_image { public partial class jszg_upload : form { static bool stop_f...
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 线程中访问。
上一篇: Java 多线程使用要点分析
下一篇: PHP实现防盗链的方法分析