2014-09-30 78 views
0

在我的解决方案中,我得到了一个用户界面,其中一些词自动操作是通过buttonclick(让我们调用按钮wordStart)来启动的。我想用另一个buttonclick来打破这个词的自动化(让我们称之为wordBreak)。是否可以停止从UI单线程执行代码?

但是,当我点击wordStart时,用户界面在执行工作时冻结,我无法点击wordBreak按钮。 我对编程还是有点新意,所以对我来说这一定是因为应用程序是单线程的,或者至少我可以用多线程来解决它。

所以这是一个2合1的问题。 1.是否有可能停止使用单个线程应用程序执行代码? 2.如何停止执行代码?

对于题号2我看了看周围的互联网了一下,发现这些方法,我认为会的工作,但其他建议,欢迎:

Application.Exit 

Application.Shutdown 

Environment.Exit 

编辑: 因为我认为这应该是多完成穿线。我没有那么多的经验,所以我已经添加了这个代码,如果有人想在这里帮助我。同时我会自己寻找解决方案。

private void generateButton_Click(object sender, EventArgs e) 
    { 
     //Thread or backgroundworker should handle this event? 
     commandsChosed(); //Event to be throwed - this starts the word automation 
    } 

    private void stopButton_Click(object sender, EventArgs e) 
    { 
     //Stop/pause the working thread 
    } 
+0

你不能在单线程上做到这一点,因此多线程是最好的选择。你提到的方法与你试图达到的目标无关!如果您使用的是.NET 4或更高版本,则可以查看TPL(任务)以使多线程更轻松。 http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx – Belogix 2014-09-30 11:54:01

+0

按钮单击开始一个新线程,http://msdn.microsoft.com/en-in/library /aa645740(v=vs.71).aspx – CodingDefined 2014-09-30 11:54:24

+0

你能显示你的代码吗? – 2014-09-30 11:55:43

回答

0

不,只用一个线程就会被阻塞,直到执行结束。如果您想要取消/暂停它,则需要使用另一个线程进行操作。例如,您可以使用BackgroundWorker

+0

就像我曾经想过的那样,尽管我希望它可以通过一个单独的线程完成。我现在必须重新考虑我的程序的体系结构。 – 2014-09-30 11:55:55

+1

如果您试图指导某人学习正确方向的多线程和异步编程,Task Parallel Library是一个更明智的选择,因为它绝对不会在任何时间任何地点(不同于已经从PCL中删除的'BackgroundWorker'和Windows商店应用程序AFAIK)。 – 2014-09-30 13:05:14

+0

@KirillShlenskiy,你在哪里得到关于在这些情况下无法使用BW的信息? MSDN并没有说这些内容AFAIK和BW通常比TPL更易于使用和理解初学者。另外请注意我只提供BW作为例子,而不是一个明确的解决方案。最重要的部分是那些前2句.. – walther 2014-09-30 16:54:51

0

只是想发布我的答案在我自己的问题,以防万一任何人有类似的问题。 正如其他人在这个问题上所建议的,我无法使用backgroundworker实现它,因为它不允许使用剪贴板等OLE函数 - 但这是我的线程正在处理的具体内容。后台工作人员在很多情况下肯定会有用 - 但由于它来自线程池,因此无法将其设置为STA。

Thread workerThread; 

private void generateButton_Click(object sender, EventArgs e) 
    { 
     generateButton.Visible = false; 
     stopButton.Visible = true; 

     //Setting up a background thread 
     workerThread = new Thread(new ThreadStart(handleGenerateButtonClick)); 
     workerThread.SetApartmentState(ApartmentState.STA); //In STA state the thread can use OLE functions like clipboard and handle some UI components as well. 
     workerThread.IsBackground = true; //It shuts down if the mainthread shuts down 
     workerThread.Start(); 

     try 
     { 
      //Checking whether the currentThread is not the workerThread before blocking the currentThread until workerThread has terminated 
      if (Thread.CurrentThread != workerThread) 
      { 
       //Wait until workerThread has terminated 
       workerThread.Join(); 
      } 
      //Sets the window buttons after workerThread has finished 
      if (!workerThread.IsAlive) 
      { 
       generateButton.Visible = true; 
       stopButton.Visible = false; 
      } 
     } 
     catch 
     { 
     } 
    } 

    private void stopButton_Click(object sender, EventArgs e) 
    { 
     generateButton.Visible = true; 
     stopButton.Visible = false; 

     //Stops the worker thread 
     workerThread.Abort(); 
    } 
相关问题