Share via


非同期のユーザー定義関数

適用対象: Excel 2013 | Office 2013 | Visual Studio

Microsoft Excel 2013 can call user-defined functions asynchronously. Calling functions asynchronously can improve performance by allowing several calculations to run at the same time. ユーザー定義関数を計算クラスターで実行する場合は、関数を非同期で呼び出すことにより、複数のコンピューターを使用して計算を実行することが実現されます。

非同期ユーザー定義関数を使用する場合

Some user-defined functions must wait for external resources. While they wait, the Excel calculation thread is blocked. In Excel 2013, user-defined functions can run asynchronously. This frees the calculation thread to run other calculations while the user-defined function waits.

In Excel 2007, programmers could run multiple user-defined functions at the same time by increasing the number of threads used in multiple-thread recalculations. This method has drawbacks primarily because the number of threads is a setting scoped to an application and cannot be controlled at the level of a single function or an add-in.

関数が外部リソースを待つ必要がある場合、プログラマーは非同期のユーザー定義関数の呼び出しを使用する必要があります。 たとえば、インターネット上で SOAP 要求を送信する関数は、ネットワークで要求が配信され、リモート サーバーで要求が完了し、ネットワークで結果が返されるのを待つ必要があります。 この場合、重要なコンピューティングは発生せず、Excel で他の計算を継続して行うことができます。

関数が計算クラスターに要求を送信している場合、プログラマーは非同期のユーザー定義関数を使用することもできます。 この場合、ネットワークの遅延を待つだけでなく、クラスターは個々の呼び出しを別々のサーバーで実行できます。 個々の呼び出しが完了するまで待たないことにより、呼び出しを重複することができるためパフォーマンスが改善します。 場合によっては、大幅に改善します。

注:

[!����] ���[�U�[��`�֐��́A�񓯊��ƃN���X�^�[ �Z�[�t�̗����Ƃ��ēo�^���邱�Ƃ͂ł��܂���B

非同期ユーザー定義関数の記述

Asynchronous user-defined functions must keep track of a handle and use that handle when informing Excel that the function call is finished. An asynchronous user-defined function is split into two pieces. The first piece is the standard UDF entry point, which will launch a second, separate asynchronous operation. Callbacks into Excel should be made during the UDF entry point. The first launching portion of the function will then return control of its calculation thread to Excel, which will continue calculation. When the second asynchronous operation is complete, it must call back into Excel and provide Excel with its result.

注:

[!����] �񓯊������ŕK�v�� UDF �ɓn���ꂽ���ׂĂ̈����ɂ‚��ẮAUDF �G���g�� �|�C���g���Ԃ��ꂽ�Ƃ��� Excel �������̈����������邽�߁A�֐��̏ڍׂ��R�s�[����Ȃ���΂Ȃ�܂���B

Excel では、非同期の UDF 呼び出しのライフ サイクルを管理するため、XLL アドインが使うことのできるイベントのセットが提供されます。 これらのイベントは、Excel での計算が完了したか、計算が取り消されたかを示します。

非同期関数の宣言

非同期のユーザー定義関数の登録時に、それを非同期として宣言する必要があります。 これは、XLOPER12 構造 (登録タイプの文字列で "X" と表示される) をポイントするパラメーターを UDF パラメーターの一覧の任意の場所に追加することによって実行されます。 Excel では、このパラメーターを使用して非同期の呼び出しハンドルを渡します。 XLL アドインは、非同期の呼び出しハンドルと、関数の結果の準備ができたらその結果を Excel に渡す必要があります。 さらに、UDF の戻り値の型は void で、型文字列の最初の文字として ">" で指定する必要があります。 UDF の同期部分は Excel に値を返さないため、戻り値の型は void です。 代わりに、XLL アドインがコールバックによって値を非同期で返します。

�񓯊��̊֐���X���b�h �Z�[�t�Ƃ��Đ錾����ƁAUDF �̓����������}���`�X���b�h�̍Čv�Z�Ŏg�p����܂��B

���̃R�[�h��́A">QX" ��o�^�^�C�v�̕�����Ƃ��Ďg�p���ēo�^���ꂽ�񓯊��̃��[�U�[��`�֐�������܂��B

void MyAsyncUDF(LPXLOPER12 arg1, LPXLOPER12 pxAsyncHandle)
{
…
}

値を返す

�񓯊��̌Ăяo���̌��ʂ̏������ł�����A�^ xlAsyncReturn �̃R�[���o�b�N����s���邱�Ƃɂ���� XLL �A�h�C���͂��̌��ʂ� Excel �ɕԂ��܂��B

xlAsyncReturn は、再計算中に計算以外のスレッドに使用できる唯一のコールバックです。 したがって、非同期の UDF の非同期部分で他のコールバックは実行しないでください。

イベントを処理する

Excel 2010 以降では、XLL は非同期関数のライフ サイクルを管理するように設計されたイベントを受信できます。 詳しくは、「イベントの処理」をご覧ください。

関連項目