Send information to another form c#
I have an application that uploads files to a server, but when I press upload it freezes until it is done, so I was thinking to make another form pop up tha开发者_StackOverflow中文版t says uploading and does all of the uploading on that form nested of freezing that main form. But to do this I need to be able to send the selected information to that other form.
I have tried using a BackgroundWorker but that doesn't work, the form still freezes.
The reason why its freezing is because you are doing uploading on the same thread as the GUI or main thread. You could create a worker thread to handle the working of the uploading so the GUI doesn't lock while processing the upload.
Example:
private void uploadButton_Click(object sender, EventArgs e)
{
object[] params = new object[] { "your file what ever type this is a generic example"};
Thread uploadThread = new Thread(new ParameterizedThreadStart(processUpload));
uploadThread.IsBackground = true;
uploadThread.Start(params);
}
private void processUpload(object params){
// do upload logic here
object[] _params = (object[])params;
string s = _params[0].ToString();
}
Passing information from one form to another is straight forward, but this form will also result in a lock while processing. If that's what you want to do then just create a constructor to take a param for whatever you'd like to pass. Then call it accordingly.
private string something = null;
public MySecondForm(string Something){
this.something = Something;
MessageBox.Show(this.something);
}
// Call this in the parent form
MySecondForm mySecondForm = new MySecondForm("hello world");
mySecondForm.Show();
If you are using WebClient class, you can use UploadFileAsync method. Also you can pass some information from one form to another as follows.
Form2
Add a simple constructor to Form2.
public Form2(string path) { // ... }
Form1
Form2 frm2 = new Form2("Path");
Take a look at this example on Code Project for some implementation tips.
精彩评论