线程问题:如何实现 [英] Threading Question : How to implement this

查看:88
本文介绍了线程问题:如何实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一个Winform应用程序,该应用程序具有backgroundWorker线程,并且仅通过该线程执行所有操作.

完成一项特定操作(即连接到服务器)后,我想创建一个监视器来检查与服务器的连接是否有效.将执行一个Web服务以找出连通性并检查结果以了解.如果没有,请重新连接.我相信我应该创建一个线程来监视连接性.逻辑类似于:在每个X秒监视器检查连接之后,在X秒后再次返回,并再次执行其工作,即检查连接并了解状态.我认为他们不需要阻塞线程,因为只要有结果我就可以做出反应.我也不需要在此过程中处理任何UI.一旦发现连接断开,可能只需要调用其他函数即可重新连接,然后该线程再次重新启动或类似的操作.

坦率地说,我在Threading工作.我阅读了2-3个教程,但无法确定如何实现此目的.任何人都可以给我一些实现上述建议的提示/想法.

实施的形式规范:

Hello,

I have a Winform app that has a backgroundWorker thread and does all operations via that only.

After completing one particular operation i.e. to connect to the server, I want to create a monitor to check the connectivity with the server is alive or not. A web service will be executed to find out the connectivity and check the results to know. If not reconnect it. I believe I should create a thread for monitoring connectivity. The logic is like : After each X secs monitor checks the connectivity, again comes back after X secs and again does its job i.e check connectivity and know the status. I don''t think their is any need to block the thread as I can react whenever I get the results. I also don''t need to deal with any UI inside this process. Might only need once I find the connectivity is lost, then have to call for other function that reconnects and this thread again restarts or something like that.

To be very frank, I am week in Threading. I read the 2-3 tutorials, but can''t make out how to implement this. Can anyone please give me some hint/idea to implement the above.

Implemented Code of Form :

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (currentState == OPENING)
        LoadAfterLogin();
    else if (currentState == CONNECTING)  
         Connect();    // CONNECTS TO THE SERVER
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   ......
   else if (value == CONNECTED)
   {
    currentState = CONNECTED;
    statusLabel.Text = "CONNECTED Successfully to server";
    //  HERE MONITOR THREAD SHOULD BE STARTED ONCE APP KNOWS FOR SURE, THAT IT IS CONNECTED TO SERVER
    monitorConn = new MonitorConnection();
   } else if (value == EXP)
   {
        currentState = ERR_CONNECTING;
        ExceptionData ed = (ExceptionData)e.UserState;
        .....
   }
}  

////////////////////////////////////////
ANOTHER MonitorConnection thread class 

public class MonitorConnection : Thread
{
    private DateTime startTime, lastCheckedTime;
    private bool conneced;
    private int intervalDuration, nextTime;

    public MonitorConnection()
    {
        intervalDuration = 10000;
    }


    public bool IsConneced
    {
        get { return conneced; }
        set { conneced = value; }
    }

    // SHOULD CALL THIS WHEN THE THREAD IS STARTED
    public void start()
    {
        startTime = DateTime.Now;
        nextTime = DateTime.Now;
    }

    // THIS SHOULD BE SOMEHOW CALLED BY run()
    public void checkConnection()
    {
        // IS CURRENT TIME IS >= SCHEDULED NEXTTIME
        if (DateTime.Now >= nextTime) {
            // Checks if the connection to server is alive or not
            conneced = UltimateLibrary.http.HTTPUtility.isConnectionAvailable();
            lastCheckedTime = DateTime.Now;
            nextTime = lastCheckedTime + intervalDuration;
        }

    }

}



这是已实现以显示我想要实现的逻辑的代码.我同意并且知道代码还不完全符合要求.仍然需要进行更改以使其不被阻塞,以连续的间隔运行等等以使其正确.我认为应该扩展除Thread以外的其他类(不确定哪个适合).

任何帮助都将受到高度赞赏.

谢谢



Here is the code that have implemetned to show the logic that I want to achieve. I agree and know the code is not fully to its mark. There still needs changes to make it proper by not blocking, running on continous intervals and so on. I think some other class than Thread should be extended (not sure which one will be appropriate).

Any help is highly appreciated.

Thanks

推荐答案

您也许应该阅读文章.这有点过时,但是是更好地理解这一点的好方法.
MSDN文档也很好. > 我想我很好奇的是为什么您需要监视连接性.在较新的技术中,最好在开始与数据库的事务时打开连接.多个连接将在后台进行管理和合并,因此我不确定您是只是误解了这一点,还是不确定您是否存在需要您进行思考的逻辑过程.
You should perhaps read this article. It is a little dated, but a good way to understand this a bit better.
The MSDN Documentation is pretty good as well.
I think what I''m curious about is why you need to monitor the connectivity. In the newer technologies, the connection is better served to be opened when beginning a transaction with the database. Multiple connections will be managed and pooled under the hood, so I''m not sure if you are just misunderstanding this or if you have a different logical process that requires your thought process.


否!不要这样做!

数据库连接是稀缺的系统资源,应打开的时间尽可能短:打开它,读取或写入数据库,然后关闭并释放该连接.

在那里只有一个线程来检查它是否打开,如果没有重新打开它就无济于事:如果在主线程中将其关闭,则后台线程可能无法始终进入使用它之前再次打开它.
No! Don''t do it!

Database connections are scarce system resources and should be open for as short a time as possible: Open it, read or write the database, then close and dispose the connection.

Having a thread there just to check it is open and if not re-open it is not going to help anybody: If it is closed in the main thread, the background thread cannot always get in to open it again before it is used...


大家好,

这是解决方案,如果它可能对任何方面都有用的话:
我没有将任何类扩展到MonitorConnection.
在MonitorConnection中添加了一个事件,当连接断开(启动监视器后)并在主应用程序中处理该事件时将触发该事件.

这样,我就可以控制事物,并且它的运行也非常完美.


谢谢大家.
Hi to All,

This is the solution if at all it might be helpful for any :
I didn''t extend any class to MonitorConnection.
Added a event in MonitorConnection that fires when the connection is lost (after starting the monitor) and is handled in main application.

This way I got control of things and its working also perfectly.


Thanks to all.


这篇关于线程问题:如何实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆