Help with threards system

Asked

Viewed 110 times

0

I wrote a small application in C#, where its main process takes a long time to run. I am having a problem with the lack of response of the graphical interface of the application while running this process. Through help obtained here at stackoverflow, I was helped to create a thread for carrying out this process. However, I still have this problem of lack of application response during the long process.

Look how I did:

 private void GeraRelatorio_Click(object sender, RoutedEventArgs e)
 {
      Thread geraRelatorio = new Thread(GeraRelatorio_Thread);
      geraRelatorio.SetApartmentState(ApartmentState.STA);
      geraRelatorio.Start();
      return;
 }

I’ll still study about threads, but the problem came before the time. Does anyone know how to make the interface respond during the process?

  • In the Stackoverflow-En has a very illustrative example.

  • is using Windows Form ?

  • if you are in windows Forms, use the background worker, if so send you example code

2 answers

1

An easier way to work with threads is with Task

Your method would look like this:

private async void GeraRelatorio_Click(object sender, RoutedEventArgs e)
{
    await Task.Run(() => GeraRelatorio_Thread());           
}

With the Task you need to inform that your method is an asynchronous method by inserting in the signature of the word method async.

Within your method you initialize the Task with the method Task.Run() passing a Action and within the Action you call your method that will run in a thread.

The word await, roughly, causes the execution control to be returned to the thread that called the method async.

For more information:

Difference between Task and Thread

MSDN

  • Thank you very much. How I set up Apartment State as STA?

  • I don’t think it’s necessary, but if you think it is, the answer to this question http://stackoverflow.com/q/5971686/4200639 may be useful to you, plus it will lock your screen until the process is over.

  • I am reading data that comes from the Graphical Interface, so the debugger is asking to be STA.

  • The given link does not teach how to set up Apartment State for the Task class.

  • As far as I know, when you use the STA, you are holding the screen to be able to access your objects, and when you think of parallelism that does not block the screen you cannot use the STA, but rather invoke the objects of the screen to be accessed. Your question is how to use the thread and not block the main screen, one of the ways is this that I passed you.

  • To invoke an object that is in another thread that is your case, you can use this example: textbox1.Invoke(new Action(()=>textbox1.Text="Olá"))

  • OK. I was able to rotate. But the screen is still frozen.

  • I needed to know what’s inside the method GeraRelatorio_Thread to give you a more appropriate answer, because whenever you access an object on screen the system will lock even if it is milliseconds. now if you access all the components of the screen and stay always manipulating them the screen will get frozen. Your question on this question is How to not block the screen during an extended processing, in this case my answer does so, does not block the screen.

  • It is only a cross of data from tables. The program is already around, but it seems that no other thread is being created.

  • But you do some screen access while crossing information?

Show 5 more comments

0

In addition to Task, you can use backgroundWorker.

When you access something from the interface, to get or set, you will have to use a Dispatcher to access through the ui thread, if your running thread is first level, I mean, if the main thread was who started it:

private void GeraRelatorio_Click(object sender, RoutedEventArgs e){
    var bw = new BackgroundWorker();    
    bw.DoWork += delegate (object s, DoWorkEventArgs args)
    {
        GeraRelatorio_Thread();
    };
    bw.RunWorkerCompleted += delegate (object s, RunWorkerCompletedEventArgs args)
    {
        //acabou a execução, mas tu pode obter retorno de métodos e etc, fechar um loader...
    };
    bw.RunWorkerAsync();

}


private void GeraRelatorio_Thread(){
    Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                    {
                        //aplica as alterações na ui
            grid.Clear() //por exemplo
                    }));

    ... //outras coisas que seu método executa
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.