C#의 스레딩 문제가 있습니다.C#의 스레딩 문제 (필드 초기화자가 비 정적 필드를 참조 할 수 없습니다.)
public partial class Form1 : Form
{
public Thread scrape = new Thread(() => scrapeStart()); //This is where the error happens
public About about = new About();
public Form1()
{
InitializeComponent();
}
public void appendOutput(String s)
{
output.AppendText(s);
output.SelectionStart = output.Text.Length;
output.ScrollToCaret();
output.Refresh();
}
public void scrapeStart(){
Button button1 = new Button();
appendOutput("");
button1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
about.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
scrape.Start();
}
private void button2_Click(object sender, EventArgs e)
{
scrape.Abort();
button1.Enabled = true;
}
}
난 기능 한 경우 실현 : I는 에러 코드를 사용하는 경우 "A 필드 이니셜 ') (Scraper.Form1.scrapeStart'는 비 정적 필드, 메소드 또는 속성을 참조 할 수있다"를 얻을 scrape 정적으로 작동하지만, 그렇게하면 appendOutput (""); 및 button1.Enabled = true 오류를 throw합니다. 그리고 새 스레드를 시작한 곳 (button1_Click)에 놓으면 button2_Click에서 중단 할 수 없습니다.
저는 조금은 C#으로 알고 있었기 때문에 모든 것을 잘못했거나 작은 문제 일 수도 있습니다. 그러나 어느쪽으로 든 나를 도울 수 있겠습니까?
스레드 코드를 작동 시키려면 UI가 아닌 스레드에서 UI를 변경하려고하므로 런타임 오류가 발생합니다. 자세한 내용은 다음을 참조하십시오 : http://www.codeproject.com/Articles/18702/Threading-in-NET-and-WinForms –
나는 그 직후에 수정했습니다 :) – AlphaDelta