Form1에 단추와 텍스트 상자가 있습니다. 버튼을 클릭하면 USB 장치에서 일부 데이터를 가져와야합니다. 웬일인지 정확히 2 % 만 제대로 작동합니다 (100 회 클릭 중 2 번 응답을 얻을 수있었습니다).dll에서 Asynccallback이 발생하면 양식이 고정됩니다.
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 주위 중지 여기서 는 Form1의 코드 .BeginRead 및 Form1 고정합니다.
테스트를 위해 완전히 새로운 프로젝트를 만들었고 DLL 대신 모든 DLL 코드를 Form1에 복사했습니다. 이 옵션은 100 % 완벽하게 작동합니다. 내 질문에 DLL로 작동하지 않는 이유는 무엇입니까?
업데이트 : 운이 좋으면 작동이 시작되어 응용 프로그램을 종료 할 때까지 무기한으로 작동합니다. 그런 다음 다시 작동하도록 노력해야합니다. 이 문제를 해결하는 방법?
아마도 'inputReportBuffer.Length' 때문에 더 많은 바이트를 읽을 수 있습니다. 사용 가능한 바이트보다 많습니다. 어떤 타입이'fileStreamDeviceData'이고'Available' 속성을 가지고 있습니까? – Silvermind
바이트 수는 정확합니다. 이 프로그램은 완벽하게 잘 작동하는 원본 프로그램의 복사본입니다. 나는 여기서 아무 것도 변하지 않았다. 내가 한 유일한 방법은 코드의 일부를 새로 생성 된 DLL로 옮기는 것입니다. 아이디어는 자신의 필요에 따라 자신의 응용 프로그램을 만들 수 있도록 DLL을 내 친구에게 배포한다는 것입니다. – mike5ocns
fileStreamDeviceData는 private static FileStream이며 .Available 속성을 가지고 있지 않습니다. – mike5ocns