1

これはかなり一般的な作業のように思えますが、答えを探すのに苦労しています。

データベース ルーチンの dll に大きく依存する WPF/C# アプリケーションがあります。dll でいくつかの GUI 要素 (進行状況バーなど) を更新したいと考えています。

これまでのところ、答えは System.ComponentModel と backgroundworker の作成のどこかにあるようです。しかし、それは私が得ることができる限りです。

誰かがこのタスクを達成する方法について提案してもらえますか? リンク、サンプルコード、励ましのお言葉、ありがとうございます!

どうもありがとう、

ジェリー

4

3 に答える 3

3

最善の策は、UI がデータベース DLL を呼び出すときにコールバック関数を渡すように設定することです。データベース DLL はそのコールバックを「定期的に」呼び出し、UI に実装されたコールバックが UI 要素を更新します。

データベース DLL が UI をいじらないようにするのが最善です。1 つには、DLL が UI の仕様に大きく依存するようになり、DLL が他の exe で実際に使用できなくなるという点です。

于 2010-07-23T01:34:17.330 に答える
1

It's a little hard to tell exactly how your code works now, and I think the answer depends on whether or not your current implementation is blocking the UI (i.e., by making a synchronous call to your DB DLL).

Now, if your call to the DLL is a synchronous one and it's blocking the UI then, yes, you might want to consider using a BackgroundWorker to do that work and allow your main UI thread to avoid hanging. You can refer to How do I implement a progress bar in C#? for some answers.

But you still need some mechanism for knowing the progress of your DB call, and it doesn't sound like the DLL exposes anything like that yet.

Just reading between the lines of your question, it sounds like you have some ability to modify the DLL. If so, then you should try to use a pattern that preserves decoupling between the DLL and your UI app when providing progress information. Your UI code should really be responible for updating itself based on some data that the DLL can provide in a loosely coupled way. I would suggest using either:

  • A subscription model by listening on an event published by some class in the DLL; or
  • A polling model to actively monitor some data object implemented in the DLL on a periodic basis (e.g., during a Timer Tick handler).

Either way, you want to avoid tightly coupling the two layers together by coding the DLL to have intimate knowledge of your UI layer. Both of the aforementioned possibilities should allow you to keep your UI updating code on the main UI thread of your app executable in most circumstances, which is important if you want to avoid multithreading issues.

于 2010-07-23T03:05:55.843 に答える
0

ABackgroundWorkerは、Taskオブジェクトと同様に機能します (Task個人的には のほうが好みです)。私のブログには、キャンセルと段階的な進行を完全にサポートして、これらの両方がバックグラウンドで作業している例があります。このレベルの複雑さ (キャンセルなど) は必要ない場合があります。

于 2010-07-23T01:34:08.923 に答える