ボタンとテキスト ボックスを含むフォーム Form1 があります。ボタンをクリックすると、USB デバイスからデータを取得する必要があります。何らかの理由で、約 2% しか正しく機能しません (100 回のクリックのうち 2 回の正しい応答を得ることができました)。Form1 のコードは次のとおりです。
namespace Test_onForm1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Lib1.FindHID.TransferInputAndOutputReports(0xC0); //request specific data from USB device
}
}
}
USB 通信を処理するコードは DLL Lib1 にあります (以下のコードの一部):
namespace Lib1
{
public static class FindHID
{
private static void TransferInputAndOutputReports(UInt16 repType)
{
//some code here sending request to USB device... and then read what came from USB
ReadInput();
//some code here
}
// Read an Input report.
private static void ReadInput()
{
Byte[] inputReportBuffer = null;
inputReportBuffer = new Byte[MyHid.Capabilities.InputReportByteLength];
IAsyncResult ar = null;
if (fileStreamDeviceData.CanRead)
{
// RUNS UP TO THIS POINT and then Form1 freezes most of the time
fileStreamDeviceData.BeginRead(inputReportBuffer, 0, inputReportBuffer.Length, new AsyncCallback(GetInputReportData), inputReportBuffer);
}
}
private static void GetInputReportData(IAsyncResult ar)
{
// RARELY GETS HERE
Byte[] inputReportBuffer = null;
inputReportBuffer = (byte[])ar.AsyncState;
fileStremDeviceData.EndRead(ar); //waits for read to complete
// then code to update Form1
}
}
}
}
動作しない場合、fileStreamDeviceData.BeginRead 付近で停止し、Form1 がフリーズします。
テストのために、まったく新しいプロジェクトを作成し、DLL を使用する代わりに、すべての DLL コードを Form1 にコピーしました。このオプションは、100% の確率で問題なく機能します。私の質問は、なぜ DLL で動作しないのですか?
更新:運が良ければ動作し始め、アプリケーションを閉じるまで無期限に動作します。それから、私はそれを再び機能させるために努力し続けなければなりません。この問題のトラブルシューティング方法は?