Cross-process access in. net (C #)

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_button#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_button#click
Void uploadImageToDB ()
{
// Step 1: Find the files and quantity in the folder
DirectoryInfo myFolder = new DirectoryInfo ("bcd ");
FileInfo [] myFiles = myFolder. GetFiles ();
This. richTextBox1.Text = "found from the folder:" + myFiles. Length. ToString () + "files! "+" \ N ";
This. jindutiao_progressBar1.Maximum = myFiles. Length;
// Step 2: 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) + "% ";
// Upload the actual data image/jpeg
SqlCommand cmd = new SqlCommand ("update my_jszg set filemime = 'image/jpeg ', filebody = @ myfilebody from my_jszg where ID 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
Conn. Close ();
}
MessageBox. Show ("all files have been uploaded! ");
}
// Stop upload
Private void stop_button_Click (object sender, EventArgs e)
{
Stop_flag = true;
}
}
}
In this Code, access to a control is as follows: this. richTextBox1.Text = myFiles [I]. name + "\ n" + this. richTextBox1.Text; only in one thread, If you access the control in two processes, an error will occur! For example, it cannot be accessed in the 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.