关于.net(C#)中的跨进程访问的问题
2009-04-13 11:47
1061 查看
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#)中的跨进程访问的问题
- 关于.net(C#)中的跨进程访问的问题
- 关于C#,.net 访问oracle中文乱码问题
- 关于C#,.net 访问oracle中文乱码问题
- 关于在web情况下访问C#客户端,导致C#客户端无法访问网络映射盘问题
- 关于C#远程执行命令后,在远程主机上能看到进程但看不到交互窗口的问题(一)
- 在C#或者SWT上跨进程访问SWT控件的问题
- 4000 c#关于“线程间操作无效: 从不是创建控件的线程访问它”的问题
- 关于访问另外进程窗口的问题
- 关于asp.net和iis的进程/线程问题,假如网站有1000个人访问,会产生多少个进程/线程啊
- C#关于"该进程无法访问文件,因为该文件正由另一进程使用"的错误的一点心得(示例)
- c# 读取其他程序正打开的文件的时“正由另一进程使用,因此该进程无法访问该文件。"的问题解决方法
- 关于解决在.Net中调用Excel对象后关闭Excel进程的问题
- 在WEB程序中使用.NET Remoting的IpcChannel时注意事项(关于“拒绝访问”问题的解决)
- 关于64位操作系统使用C#访问注册表失败的问题
- C# Bitmap保存的问题,eg:GDI+中发生一般性错误 .jpg 正由另一个进程使用 ,该进程无法访问文件
- 关于在.net环境下通过使用LDAP来访问AD,LDAPServer的一些问题?
- 总结:C# Bitmap保存的问题,eg:GDI+中发生一般性错误 .jpg 正由另一个进程使用 ,该进程无法访问文件
- 【求助】关于.NET(C#)调用斑马打印机(ZDesigner GK888t (EPL))换页时退纸的问题
- 关于IIS服务启动失败的问题:“IIS提示‘另一个程序正在使用此文件,进程无法访问’”,的解决方法