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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        You must Invoke the method on the thread that owns the control:



        Invoke(new Action(() => f.Close()));


        Take a look at Invoke method.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 11:20









        Alessandro D'Andria

        6,62912227




        6,62912227






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff