开发者

Where should threads be handled for a query method?

开发者 https://www.devze.com 2023-02-14 04:47 出处:网络
I\'ve created a class library which has a method to query for open ports, and in some cases can take up to 3 seconds to complete. If I were to implement this in a separate thread, would this be handle

I've created a class library which has a method to query for open ports, and in some cases can take up to 3 seconds to complete. If I were to implement this in a separate thread, would this be handled in my class method, or would it be done by the highest entity for example the main form?

Could you provide examples where threads are handled internally within a class?

[EDIT]

Here's the method in question.

 public void QueryOpenPorts(out string[] portNames, out bool[] isOpen)
    {
        // get valid ports on computer
        portNames = QueryPortNames();
        // number of ports
        int count = portNames.Length;
        // initialise isOpen array
        isOpen = new bool[count];

        // iterate through ports and test connection
        for (i开发者_Go百科nt i = 0; i < count; i++)
        {
            using (SerialPort serialPort = new SerialPort(portNames[i]))
            {

                serialPort.Open();
                // port is available 
                isOpen[i] = true;

            }
        }
    }


Architecturally, I'd create a thread from the main form that will call your class library. Who knows, maybe some other class calling your library does not need the call to be in a thread.

Within my main class i have something like:

this._checkSessionTimer = new Timer(intTimer * 2000);
this._checkSessionTimer.Elapsed += (o, e) => CheckSessions();

And CheckSessions goes off to call whatever it needs.

I also use background workers to do my work that requires long delays with the web server:

 BackgroundWorker linkWorker = new BackgroundWorker();
            linkWorker.DoWork += (o, e) =>
                             {
                             ...
                             };
            linkWorker.RunWorkerAsync();

The anonymous function within my worker DoWork actually uses my web service wrapper to make web service calls.

However, before doing things like this, i REALLY recommend you to give this a read:
http://www.albahari.com/threading/


Very rarely would I have multi-threaded code handled within the same class. Try to use an external library when at all possible because this would allow you to use different asynchronous ways to call it (not just via multiple threads).

For instance, let's say I have a DownloadHtmlToString(string url) method. Normally this method would take that URL, go out to the web and then save that information to a string and return it.

By having that in an external library/class/module/etc. you allow yourself to call that synchronously, asynchronously, and with multiple threads/tasks all without having to edit that main functionality. The key is to make the DownloadHtmlToString() method thread-safe.

I get in the habit of making things thread-safe even when they're not originally intended to be called asynchronously because of future maintenance. Who knows, in the future we may have 256-core machines in the next 5 years, yet my code still needs to run in the corporate environment and be able to scale with new hardware.


Create new thread inside your class.

Creating threads, like other subjects should be done under object-oriented rules. Encapsulation rules us to hide the functionality of an object inside it.

I/O operations (like network operations) are good examples of where we need both blocking and non-blocking methods.

If this class is gonna be used only in win-form applications, mark it as STA and prevent sharing it by multiple threads in an unsafe way.

0

精彩评论

暂无评论...
验证码 换一张
取 消