how to open another form which will have progress bar while UI thread is busy
up vote
0
down vote
favorite
I tried with background worker. When function with loading database data is called, i call backgroundworker too, and i want to open another form which will have progress bar and when loading data is finished, i want to close that form and let user use app normaly. I tried this
private void SearchBtn_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//loading data
}
this is function which takes long time to execute so i call background worker.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
f.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
f.Close();
}
f is my form global varibale , when i write like this, i get error that i cant close form which has been opened in another thread. So what im doing wrong.
c# multithreading backgroundworker
add a comment |
up vote
0
down vote
favorite
I tried with background worker. When function with loading database data is called, i call backgroundworker too, and i want to open another form which will have progress bar and when loading data is finished, i want to close that form and let user use app normaly. I tried this
private void SearchBtn_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//loading data
}
this is function which takes long time to execute so i call background worker.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
f.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
f.Close();
}
f is my form global varibale , when i write like this, i get error that i cant close form which has been opened in another thread. So what im doing wrong.
c# multithreading backgroundworker
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
1
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I tried with background worker. When function with loading database data is called, i call backgroundworker too, and i want to open another form which will have progress bar and when loading data is finished, i want to close that form and let user use app normaly. I tried this
private void SearchBtn_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//loading data
}
this is function which takes long time to execute so i call background worker.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
f.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
f.Close();
}
f is my form global varibale , when i write like this, i get error that i cant close form which has been opened in another thread. So what im doing wrong.
c# multithreading backgroundworker
I tried with background worker. When function with loading database data is called, i call backgroundworker too, and i want to open another form which will have progress bar and when loading data is finished, i want to close that form and let user use app normaly. I tried this
private void SearchBtn_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
//loading data
}
this is function which takes long time to execute so i call background worker.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
f.Show();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
f.Close();
}
f is my form global varibale , when i write like this, i get error that i cant close form which has been opened in another thread. So what im doing wrong.
c# multithreading backgroundworker
c# multithreading backgroundworker
edited Nov 8 at 13:23
Rob
1,048920
1,048920
asked Nov 8 at 10:48
mikisa
1
1
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
1
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29
add a comment |
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
1
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
1
1
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You must Invoke
the method on the thread that owns the control:
Invoke(new Action(() => f.Close()));
Take a look at Invoke method.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You must Invoke
the method on the thread that owns the control:
Invoke(new Action(() => f.Close()));
Take a look at Invoke method.
add a comment |
up vote
1
down vote
You must Invoke
the method on the thread that owns the control:
Invoke(new Action(() => f.Close()));
Take a look at Invoke method.
add a comment |
up vote
1
down vote
up vote
1
down vote
You must Invoke
the method on the thread that owns the control:
Invoke(new Action(() => f.Close()));
Take a look at Invoke method.
You must Invoke
the method on the thread that owns the control:
Invoke(new Action(() => f.Close()));
Take a look at Invoke method.
answered Nov 8 at 11:20
Alessandro D'Andria
6,62912227
6,62912227
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206143%2fhow-to-open-another-form-which-will-have-progress-bar-while-ui-thread-is-busy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You must use the Invoke method .
– Alessandro D'Andria
Nov 8 at 10:53
The form and progress bar are created and shown by the main thread and the background worker is for the actual database loading.
– i486
Nov 8 at 10:53
1
Only ever write code like that when you know how to do this kind of debugging. Since just about nobody does, nobody should ever do this. Focus on the real problem, never hang the UI thread. It is never necessary.
– Hans Passant
Nov 8 at 13:29