1

I'm trying to learn Vala so I'm making a small GUI application. My main language before has been C# so things are going pretty well.

However, I've hit the wall now. I need to connect to an external network server (using GIO) which doesn't answer my client immediately. This makes the GUI freeze up while the program is connecting and doing its thing.

In C# I would probably use a BackgroundWorker in this case. I can't seem to find anything like it for Vala though.

Basically, I have a MainWindow.vala where I have hooked up a signal for clicking a certain button to a method that is creating a new instance of ProcessingDialog.vala. This shows a dialog over the MainWindow that I want the user to see while the program is doing the work (connecting to the server, communicating).

What are my alternatives to make this scenario work?

4

2 に答える 2

3

GIOは非同期メソッドを提供します。たとえば、非同期クライアントを参照してください:https ://live.gnome.org/Vala/GIONetworkingSample

Valaの非同期メソッドに気付いていない場合は、チュートリアルを見てみてください:https ://live.gnome.org/Vala/Tutorial#Asynchronous_Methods

于 2012-04-08T08:34:28.263 に答える
0

上記のリーサルマンの答えはおそらく最も理にかなっています。ネットワーク呼び出しを行っている場合は、非同期要求が実際に最善の策になります。それ以外の場合は、Vala の組み込みスレッド サポートを使用して、バックグラウンド タスクを実行できます。すぐに、より良いライブラリが利用可能になるように見えますが、これは安定しています。

// Create the function to perform the task
public void thread_function() {
    stdout.printf("I am doing something!\n");
}

public int main( string[] args ) {
    // Create the thread to start that function
    unowned Thread<void*> my_thread = Thread.create<void*>(thread_function, true);

    // Some time toward the end of your application, reclaim the thread
    my_thread.join();

    return 1;
}

「--thread」オプションでコンパイルすることを忘れないでください。

于 2012-04-26T22:41:26.637 に答える