2013-06-14 2 views
2

가난한 디자인이라는 것을 알고 있지만, 제 GUI Form 클래스의 컨트롤을 C# 프로그램의 다른 클래스로 이전했습니다. Form.ActiveForm 가끔씩 작동합니다.

사용하여, 오랜 시간이 지금 잘 작동하고있다 : 내 프로그램이 더 큰 성장하고 양식 더 클래스에서 열립니다

var form = Form.ActiveForm as Form1; 

, 그것은 행동이 랜덤하게합니다. 때로는 오류없이 프로그램을 실행하여 다른 프로그램을 실행하여 임의의 지점을 선택하고 NullReferenceException을 throw합니다.

예를 들어, 다음 두 줄의 코드는 호출되는 8 개의 함수에서 온 것입니다.

var form = Form.ActiveForm as Form1; 
form.richTextBox1.AppendText("Section ID: "); 

프로그램이이 시점에 이르렀으므로 양식을 전달하고 이전에 동일한 필드를 조작 할 수 있음을 알고 있습니다.

정확히 같은 일이 이전에 작동했을 때 왜 던지기를 선택했는지에 대한 아이디어가 있습니까?

내가 아는 한 Form1만이 유일한 양식입니다. 나는 GUI 개발에 100 % 녹색이며, C#/.NET이기 때문에 여기서 틀릴 수도 있습니다.

내가 가진 이론은 다음과 같습니다. Form 개체를 상속하는 Reader와 Analyze의 두 클래스가 있습니다. Reader는 아무 문제없이 처음 상속받습니다. 문제는 분석에서 즉시 발생합니다. Reader 클래스에서 양식을 포기하지 않습니까?

편집 : 내 첫 번째 직감은 ActiveForm 속성을 사용하는 여러 클래스가 문제를 일으키는 것입니다. 내 모든 기능을 Reader 클래스에 넣었습니다. 이것은 문제를 해결하지 못했습니다.

다음은 Reader의 두 가지 방법입니다. 첫 번째, read()는 SectionHeaderMatch() 전에 호출됩니다. Read는 ActiveForm 속성을 사용하며 이전에 호출 된 다른 메서드와 마찬가지로 문제가 발생하지 않습니다. SectionHeaderMatch는 ActiveForm 속성을 사용하는 세 번째 메서드입니다. 이 메서드는 Form1 대신 null로 설정됩니다. 나는 read()에서 뭔가를 망쳐 놓고 있다고 가정하고있다. ActiveForm 항상 Form1 아니다처럼

public static void read() 
    { 
     var form = Form.ActiveForm as Form1; 

     StringBuilder outPut = new StringBuilder(); 
     outPut.Append(form.textBox2.Text); 
     outPut.Append("\\out.txt"); 

     string cardDrive = String.Format("\\\\.\\{0}", form.textBox1.Text); 
     cardDrive = cardDrive.Remove(6); 
     SafeFileHandle fileHandle = CreateFile(cardDrive, 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero); 
     FileStream readStream = new FileStream(fileHandle, FileAccess.Read); 
     BinaryReader reader = new BinaryReader(readStream); 
     FileStream writeStream = File.OpenWrite(outPut.ToString()); //Writing stream opened at the specified directory of output. 

     long gigsRead; //Loop counter that specifies the number of gigabytes read thus far. 
     long megsRead; //Loop counter that specifies the number of megabytes read thus far within the current gigabyte. 

     for (gigsRead = 0; gigsRead < 0; gigsRead++) 
     { 
      for (megsRead = 0; megsRead < 1024; megsRead++) 
      { 
       byte[] buffer = new byte[1048576]; 
       long position = gigsRead * 1073741824 + megsRead * 1048576; 
       reader.BaseStream.Position = position; 
       reader.Read(buffer, 0, 1048576); //Read from SD card to buffer 
       writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file. 
       writeStream.Flush(); 
      } 
     } 

     for (megsRead = 0; megsRead < 432; megsRead++) 
     { 
      byte[] buffer = new byte[1048576]; 
      long gigSize = 1073741824; 
      long position = 7 * gigSize + megsRead * 1048576; 
      reader.BaseStream.Position = position; 
      reader.Read(buffer, 0, 1048576); //Read from SD card to buffer 
      writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file. 
      writeStream.Flush(); 
     } 
     writeStream.Close(); 
     readStream.Close(); 
     reader.Close(); 
     fileHandle.Close(); 
     outPut.Clear(); 
    } 

    public static void SectionHeaderMatch() 
    { 
     var form = Form.ActiveForm as Form1; 
     if (form == null) 
     { 

     } 

     else 
     { 
      StringBuilder outPut = new StringBuilder(); 
      outPut.Append(form.textBox2.Text); 
      outPut.Append("\\out.txt"); 

      FileStream readFile = new FileStream(outPut.ToString(), FileMode.Open, FileAccess.Read); 
      BinaryReader readerFile = new BinaryReader(readFile); 

      //Sector 1 
      readerFile.BaseStream.Position = 1073741824; 
      byte[] sectorOne = new byte[Form1.blockSize]; 
      readerFile.Read(sectorOne, 0, Form1.blockSize); 

      //Sector 2 
      readerFile.BaseStream.Position = 1073741824 + Form1.blockSize; 
      byte[] sectorTwo = new byte[Form1.blockSize]; 
      readerFile.Read(sectorTwo, 0, Form1.blockSize); 

      readerFile.Close(); 
      readFile.Close(); 

      string sector1 = UtilityClass.ByteArrayToString(sectorOne); 
      string sector2 = UtilityClass.ByteArrayToString(sectorTwo); 

      if (String.Compare(sector1, sector2) == 0) //0 if they match 
      { 
       if (headerMatchRunCount == 0) //If this is section 1 
       { 
        form.richTextBox3.AppendText("Section 1 headers match."); 
       } 

       else //Section 2 
       { 
        form.richTextBox4.AppendText("Section 2 headers match."); 
       } 
      } 

      else //Headers did not match 
      { 
       if (headerMatchRunCount == 0) //Section 1 
       { 
        form.richTextBox3.AppendText("Section 1 headers match."); 
       } 

       else //Section 2 
       { 
        form.richTextBox4.AppendText("Section 2 headers match."); 
       } 
      } 
     } 
    } 
+0

음, Reader 클래스와 Analyze 클래스가 * 양식을 상속하는 이유는 무엇입니까?그들은 실제로 형식인가요, 단지 도우미 클래스입니까? –

+0

@CodyGray는 ActiveForm 속성을 사용해야합니다. –

답변

2

보인다. 활성화 된 다른 유형의 양식 (대화 상자, 아마도?)이있을 것입니다. as Form1으로 시도하면 결과는 null입니다. activateForm 가끔 널 왜

+0

가장 확실하게 null로 설정됩니다. 나는 내가 가지고있는 다른 액티브 폼을 말할 수 없다. 위의 코드를 게시 할 것입니다. –

+0

null로 설정되는 것은 무엇입니까? 귀하의'form' 변수 또는'Form.ActiveForm' 속성? 'Form.ActiveForm'의 실제 값은 무엇입니까? 그것이 null이면 그것은 이상합니다. null가 아닌 경우,'as Form1'에 의해'form'가 null로 설정됩니다. –

4

몇 시간 동안 물리 치고 마침내 내가에 설명

activateForm이 "IDE"형식을 취하지 만 C#을 양식 생성 ..

... 발견 예 : 첫 번째 루프, 내 breakcode없이 응용 프로그램을 시작하고 내 "activateForm()"이 실행 된 후 중단 코드를 입력하면 작동합니다.

두 번째 동일한 루프에서 내 중단 점이 있고 activateForm() 더 이상 작동하지 않습니다. 왜? "ActivateForm"이 더 이상 C# 형식이 아니기 때문에 ... 하지만 내 "Visual Studio 코드 페이지"입니다.

귀하의 사례에서 도움이 될지 모르겠지만 내 도움이됩니다.

관련 문제