0

Windows フォーム アプリケーションを使用して .NET プログラムを作成しました。私のアプリケーションはかなり単純です。基本的に、フォームには 2 つのシンプルなボタンがあります。フォームが最初に読み込まれるときに、グローバル変数 (bool run = true) を設定します。

そして、私の最初のボタンは基本的に非常に単純な while ループです。

while(run) { // do some code }

そして、私がやりたいことは、2番目のボタンでブール値の値をfalseに設定することです(bool run = false)。

しかし、最初のボタンをクリックすると、2番目のボタンに触れることさえできません!

私はこれについてかなり読んだことがありますが、私の解決策はマルチスレッドを使用することだと思います。マルチスレッドのサンプルコードをオンラインで見つけて、それらを取り入れようとしましたが、なぜそれが機能しないのかわかりません。ボタン #1 をクリックすると、ボタン #2 をクリックする方法がありません。

4

1 に答える 1

0

Your UI thread should not have any infinite or a wait-on-something - never! You must not block the UI for anything (other than simple calculations, validations, user confirmation etc). You should make a thread for performing length task, and let that thread communicate to UI thread using asynchronous (or synchronous, if you prefer) communication.

On native Windows GUI application, you can use PostMessage (async), or SendMessage (sync). For .NET GUI applications, you can use Control.BeginInvoke (async), or Control.Invoke (sync).

Please read this article on how this is done.

于 2013-01-23T15:52:19.177 に答える