简体   繁体   English

在没有ThreadPool的wpf(c#)应用程序中一次又一次地重复使用同一单线程执行同一任务

[英]Reuse same single thread for same task again and again in wpf (c#) application without ThreadPool

Creating An MVVM application where application wants to connect to server on a button click. 单击按钮创建MVVM应用程序,其中应用程序要连接到服务器。 After clicking the button, a thread will be created to connect to the server so that UI doesn't freeze and terminates(TIME OUT is 15 secs). 单击按钮后,将创建一个线程以连接到服务器,以使UI不会冻结并终止(超时为15秒)。 Next time click on the button is creating again new thread to connect to server and terminates. 下次单击该按钮时,将再次创建新线程以连接到服务器并终止。

But first time I want to create a new thread and later I want to reuse that thread(not new) to do the "connect" task if application is not closed and user clicked on the same button. 但是,第一次,我想创建一个新线程,然后,如果应用程序未关闭并且用户单击了同一按钮,那么我想重用该线程(不是新线程)来执行“连接”任务。

Is that possible? 那可能吗?

Below is the code: 下面是代码:

Class ConnectViewModel:BaseViewModel
{
    public void ConnectToServer()
    {
        ConnectButtonEnable = false;
        ConnectingServerText = Properties.Resources.TryingToConnectServer;
        Thread thread = new Thread(new ThreadStart(connect));
        thread.Start(); 
        thread.Join();
    }

    public void connect()
    {
        bool bReturn = false;
        UInt32 iCommunicationServer;
        bReturn = NativeMethods.CommunicateServer(out iCommunicationServer);
        if (!bReturn || NativeMethods.ERROR_SUCCESS != iCommunicationServer)
        {
            ConnectingServerText = Properties.Resources.UnableToConnectToServer;                
        }
        else if (NativeMethods.ERROR_SUCCESS == iCommunicationServer)
        {
            ConnectingServerText = properties.Resources.SuccessfullyConnectedServer;
        }            
        ConnectButtonEnable = true;
        return;
    }
}

You can use the TPL to achieve this. 您可以使用TPL实现此目的。

private Task previous = Task.FromResult(true);
public Task Foo()
{
    previous = previous.ContinueWith(t =>
    {
        DoStuff();
    });
    return previous;
}

By having the operation schedule each operation as the continuation of the previous operation, you ensure that each one doesn't start until the one that came before it finished, while still running all of them in background threads. 通过将每个操作安排为上一个操作的延续,可以确保每个操作直到完成之前都不会开始,同时仍在后台线程中运行所有操作。

不必担心创建和管理线程,只需使用ThreadPool.QueueUserWorkItem它非常有效。

Due to how the question is phrased I would recommend you to read up on MVVM and async patterns, some examples: 由于问题的措辞如何,我建议您阅读MVVM和异步模式,下面是一些示例:

But in general, use async , don't manually create new threads when coding in a GUI-application. 但是通常,请使用async ,在GUI应用程序中编码时不要手动创建新线程。 If the task shouldn't be callable whilst "running", do test and set via Interlocked.CompareExchange and store away some state. 如果在“运行”期间该任务不可调用,请通过Interlocked.CompareExchange进行测试和设置,并保存一些状态。

You use threads for parallel work, not for "waiting on the network". 您将线程用于并行工作,而不是用于“网络等待”。

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

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