简体   繁体   English

从另一个线程关闭 main

[英]closing main from another thread

I am trying to close the appliaction some how when another thread finishes.我正在尝试在另一个线程完成时以某种方式关闭应用程序。 I am using c#.我正在使用 c#。 code below is just an example下面的代码只是一个例子

class main
{
  constuctor
  {
    thread t = new thread(open loading screen);

  }

  public void open loading screen()
  {
    if (error) 
     exit program
      application.exit(); // doesn't work
     this.Close; // doesn't work
    thread.abort or mainform.abort doesnt work.
  }
}

if i call a function on the main with application.exit this doesnt work because the call is made from another thread.如果我用 application.exit 在 main 上调用 function 这不起作用,因为调用是从另一个线程进行的。

so how do I exit the program??那我怎么退出程序??

thanks in advance提前致谢

The worker thread should set a variable or signal an event of some kind that the main thread should periodically check - it can shut down in the normal fashion when it is able.工作线程应该设置一个变量或发出某种事件的信号,主线程应该定期检查它——它可以在可以时以正常方式关闭。

Use IsAlive in the main thread to check if the other thread(s) have ended and then exit if they have.在主线程中使用 IsAlive 检查其他线程是否已结束,如果已结束则退出。

http://www.java2s.com/Code/CSharp/Thread/UseIsAlivetowaitforthreadstoend.htm http://www.java2s.com/Code/CSharp/Thread/UseIsAlivetowaitforthreadstoend.htm

Use the background worker class instead of an ordinary thread.使用后台工作者 class 代替普通线程。 The backgroundworker can be used to do work in another thread than the UI thread. backgroundworker 可用于在 UI 线程之外的另一个线程中工作。 It also has some handy events that can be used to send progress updates back to the "owning" thread.它还有一些方便的事件,可用于将进度更新发送回“拥有”线程。 Take a look at:看一眼:

ProgressChanged and RunWorkerCompleted ProgressChangedRunWorkerCompleted

Here the solution in one line of code这是一行代码中的解决方案

mainForm.Invoke((MethodInvoker)(() => mainForm.Close()));

have a Closing event in mainform that closes the thread if it doesnt close by it self after this在 mainform 中有一个 Closing 事件,如果在此之后它没有自行关闭,则关闭线程

after a lot of head scathing i think I have made some progress.经过一番严厉的批评后,我想我已经取得了一些进展。 I am not 100% sure but Can you exit an application before the constructor is finished and the main form loaded?我不是 100% 确定,但你能在构造函数完成并加载主窗体之前退出应用程序吗?

constuctor  {    thread t = new thread(open loading screen);  }

I do the exact same thing with a exit screen using a variable between the main for and another one.我使用主 for 和另一个之间的变量对退出屏幕执行完全相同的操作。 and I have an application exit in the main for if it returns true.如果它返回true,我在main中有一个应用程序退出。

At the start I have a loading screen but the main form isnt loaded yet and the call has been made from the constructor and the construtor hasn't finished yet.一开始我有一个加载屏幕,但尚未加载主窗体,并且已从构造函数进行调用,而构造函数尚未完成。

And one more thing should all the thread/ class / loading // program setup be done in the main constructor or some other way if so please advise.还有一件事应该在主构造函数中完成所有线程/ class / 加载//程序设置,如果是这样,请告知其他方式。

thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM