简体   繁体   中英

c# Program wont respond after event fired

I'm developing a user interface for a program, and something very strange is happening.

I have a text view, 2 buttons and a progress bar. I redirectioned the output on the console to my text view. so wen I click the buttons I should receive output messages. in the beginning it was fine, but then I used some longer routines, I'm trying to log in into a web service and use web-requests.

my code works almost as It was supposed to work, I can log in and make my web requests just fine. but because the answers can become slow I created some output messages, and there my problem started.. My interface wont update until all the code I created on my event handler end's running. and when that code ends executing, I receive all the output messages all at once. I cant even move the window while the program is running..

I´m programing on c# for my first time, I had to use it because I need to use dll's.. and this kind of problem never happened before. I usually use Java. It's like the code isn't running on the right order and it doesn´t make sense to me.. because I know my code is right because it runs on the console, and it runs while the program isn't responding..

I cant seem to understand this, should I make my events handling using threads?

class MainClass
{
    public static void Main (string[] args)
    {


        Application.Init ();
        UIMain win = new UIMain ();

        win.ShowAll ();
        Application.Run ();
    }
}



public partial class UIMain : Gtk.Window
{

    public UIMain () : 
        base (Gtk.WindowType.Toplevel)
    {
        System.Windows.Forms.Application.EnableVisualStyles ();
        this.Build ();


        Console.SetOut (new ControlWritter(this.textview1));


    }

    protected void OnButton2Clicked (object sender, EventArgs e)
    {
        if (entry1.Text.Equals(String.Empty) || entry2.Text.Equals(String.Empty)) {
            Console.WriteLine("random output");
        }

        ConstantesSetup.autoSetup ();

        button1.Sensitive = true;

        if (!ConstantesSetup.var1) {
            ConstantesSetup.routine6 ();
            ConstantesSetup.routine5 ();
            ConstantesSetup.routine4 ();
            ConstantesSetup.routine3 ();
            ConstantesSetup.routine2 ();

            ConstantesSetup.var1 = true;
        }
    }

    protected void OnButton1Clicked (object sender, EventArgs e)
    {
        switch (ConstantesSetup.erp) {
        case "ERP":
            eti_scp.autoSync (this);
            break;
        }
    }
}

I'm sorry for the lack of code, but I don't even know were to start looking for the problem..

thanks for your time ;)

You are blocking the UI thread with long running synchronous operations. You need to run these long running operations asynchronously so that the button click event handler can return right away while your tasks run in the background.

There are several options for running tasks asynchronously but one simple option is using a BackgroundWorker . In your event handler you could do something like:

var worker = new BackgroundWorker();

worker.DoWork += (o, args) =>
{
    //call long running processes here
};

worker.RunWorkerAsync();

The BackgroundWorker will also dispatch these operations onto the UI thread for you so you can update controls in the form inside the DoWork callback method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM