많은 UI 구성 요소가이를 필요로하기 때문에 호출 스레드는 STA이어야합니다.
http://www.codeproject.com/KB/IP/Facebook_API.aspx를 사용하고 있습니다 .
WPF를 사용하여 만든 XAML 을 호출하려고합니다 . 그러나 그것은 나에게 오류를 준다 :
많은 UI 구성 요소에 필요하기 때문에 호출 스레드는 STA이어야합니다.
어떻게해야할지 모르겠습니다. 나는 이것을하려고합니다 :
FacebookApplication.FacebookFriendsList ffl = new FacebookFriendsList();
그러나 그것은 저에게 그 오류를주고 있습니다.
백그라운드 작업자를 추가했습니다.
static BackgroundWorker bw = new BackgroundWorker();
static void Main(string[] args)
{
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync("Message to worker");
Console.ReadLine();
}
static void bw_DoWork(object sender, DoWorkEventArgs e)
{
// This is called on the worker thread
FacebookApplication.FacebookFriendsList ffl = new FacebookFriendsList();
Console.WriteLine(e.Argument); // Writes "Message to worker"
// Perform time-consuming task...
}
디스패처 에서 코드를 호출하십시오 .
Application.Current.Dispatcher.Invoke((Action)delegate{
// your code
});
메인 스레드에서 호출하는 경우 이전 답변에서 설명한대로 STAThread 속성을 Main 메서드에 추가해야합니다.
별도의 스레드를 사용하는 경우 STA (단일 스레드 아파트)에 있어야하며 백그라운드 작업자 스레드의 경우에는 해당되지 않습니다. 다음과 같이 스레드를 직접 작성해야합니다.
Thread t = new Thread(ThreadProc);
t.SetApartmentState(ApartmentState.STA);
t.Start();
ThreadProc는 ThreadStart 유형의 대리자입니다.
백그라운드 스레드에서 UI 구성 요소에 콜백을 받고 있다고 의심됩니다. UI 스레드를 인식하므로 BackgroundWorker를 사용하여 호출하는 것이 좋습니다.
BackgroundWorker의 경우 기본 프로그램은 [STAThread]로 표시되어야합니다.
당신은 또한 이것을 시도 할 수 있습니다
// create a thread
Thread newWindowThread = new Thread(new ThreadStart(() =>
{
// create and show the window
FaxImageLoad obj = new FaxImageLoad(destination);
obj.Show();
// start the Dispatcher processing
System.Windows.Threading.Dispatcher.Run();
}));
// set the apartment state
newWindowThread.SetApartmentState(ApartmentState.STA);
// make the thread a background thread
newWindowThread.IsBackground = true;
// start the thread
newWindowThread.Start();
나 에게이 오류는 null 매개 변수가 전달되어 발생했습니다. 변수 값을 확인하면 코드를 변경하지 않고도 문제가 해결되었습니다. BackgroundWorker를 사용했습니다.
프로그램에 [STAThread]
속성을 표시하면 오류가 사라집니다! 그것은 마술이다 :)
If you call a new window UI statement in an existing thread, it throws an error. Instead of that create a new thread inside the main thread and write the window UI statement in the new child thread.
'development' 카테고리의 다른 글
새 Rails 앱을 만들 때 Rails에게 테스트 단위 대신 RSpec을 사용하도록 지시하려면 어떻게해야합니까? (0) | 2020.06.13 |
---|---|
Convert.ToString ()과 .ToString ()의 차이점 (0) | 2020.06.13 |
Python 목록을 CSV 파일로 작성 (0) | 2020.06.13 |
ConstraintLayout을 사용하여 균일 한 간격의 뷰 (0) | 2020.06.13 |
jQuery없이 부모 절대 div의 자식 요소를 가리킬 때 onmouseout 방지 (0) | 2020.06.13 |