Questions about cross-process access in. NET (C #) _ Practical Tips

Source: Internet
Author: 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 = "Connection string";
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 ()
{
The first step: Find the file and number of folders below
DirectoryInfo myFolder = new DirectoryInfo ("BCD");
fileinfo[] myfiles = Myfolder.getfiles ();
This.richTextBox1.Text = "Found from folder:" + myFiles.Length.ToString () + "File!" "+" \ n ";
This.jindutiao_progressBar1.Maximum = Myfiles.length;
Step two: Start uploading files
String connectionString = "Connection string";
using (SqlConnection conn = new SqlConnection (connectionString))
{
Conn. Open ();
for (int i = 0; i < myfiles.length; i++)
{
if (Stop_flag)
Break
Progress bar
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) + "%";
Uploading the actual data image/jpeg
SqlCommand cmd = new SqlCommand ("Update my_jszg set filemime= ' Image/jpeg ', filebody = @myfilebody from MY_JSZG where certificate number = @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 ("All files uploaded!") ");
}
Stop uploading
private void Stop_button_click (object sender, EventArgs e)
{
Stop_flag=true;
}
}
}
In this code, access to a control is like: This.richTextBox1.Text = Myfiles[i]. Name + "\ n" + this.richTextBox1.Text; In only one thread, if access to the control in two processes will be an error! If you cannot access it in your program's own thread and Uploadimagetodb thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.