WinForm form calling WCF service form stuck

  • 2020-05-09 19:06:50
  • OfStack

Form start starts a main program thread, and if a service is called in the From_Load() method, the calling service action blocks the main program.

You can solve this problem by simply putting the operation that calls the service into another thread for processing.

Such as:
 
Thread ServiceThread=null; 
public void TestForm_Load(object sender, EventArgs e) 
{ 
CheckForIllegalCrossThreadCalls = false; 
ServiceThread = new Thread(new ThreadStart(RegService)); 
ServiceThread.Start(); 
} 
private void RegService() 
{ 
// The registration service invocation service operation is implemented here  
} 

Note: when a thread is started, remember to close it, otherwise the main process will not close when the form is closed, because there are still threads. You can close the thread in the FromClosed() method.
 
private void TestForm_FormClosed(object sender, FormClosedEventArgs e) 
{ 
ServiceThread.Abort(); 
Application.Exit(); 
} 

Related articles: