SyN/AcK has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Monks!

So, I'm having this problem when working with threading. I'm hoping someone can help me resolve it. So, what I have is a GUI written in Visual Perl .NET that based on some user input runs a particular set of operations. These operations take a large chunk of time to complete, and they appear to be causing my GUI to freeze.

So, what I was thinking I would do is make a seperate thread for the actions that take place, hoping that this would keep the GUI from freezing. The problem here is, the actions need to communicate their results back to the GUI. Unfortunately, when I try to output to the GUI, it will not allow me to. It simply doesn't show anything, there's no errors, it just doesn't show anything.

I've tried several different threading methods, including those from .NET, as well as perl's threads. Basically I'm creating a thread that is associated with a function (or subroutine), and I'm hoping that when I click go on my GUI, that control will be passed to the thread, or the function (which it is), but I still need that thread to be able to communicate its results to the GUI.

Is there a better way to do this? What am I missing? Sorry, I'm a bit clueless when it comes to threads.

Replies are listed 'Best First'.
Re: Threading in Perl
by Cine (Friar) on Jul 29, 2003 at 22:26 UTC
    We could use some sample code here, but here is some general pointers:
    For functions which take a long time to process there are a few ways of doing it. You found the two obvious ones, namely let it run and thus block the interface, second start a thread and let that do the work. A third option to consider is to break up the work in smaller bits, thus in your idle loop calculate some of the work required, short enough so that the interface does not block and then return the control to the interface. In many cases, this is the simplest option.
    But if you really want threads, then make you gui the primary thread and when one of your actions starts running, give it a shared variable (threads::shared) so that it can report back progress, which you can then check in the idle loop.

    T I M T O W T D I

      Some Tk widget options will accept a reference to a variable (example: -textvariable in an Entry or LabEntry widget) so when the varible changes, the widget updates with nbo further work. So if the process thread updates a shared variable that the widget references, this may work for you.

      --Bob Niederman, http://bob-n.com
        Is Tk available on winblows?

        T I M T O W T D I
Re: Threading in Perl
by BrowserUk (Patriarch) on Jul 29, 2003 at 22:49 UTC

    Without sight of your code your question is a tall order, but chances are even if you posted some minimal code I wouldn't have the requisite to run it.

    So, generic advice, don't try and access/maintain your gui from anywhere except the main thread. When you have a "long running operation", use a thread to do the processing, but don't try and display the results directly, but instead post a message with information required to display the results to keyboard/mouse input processing queue. Have code in your main thread to look for and process the message posted, and update the display accordingly.

    Sketchy answer, but so was the question:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Threading in Perl
by SyN/AcK (Scribe) on Jul 30, 2003 at 02:55 UTC

    Yes, sorry guys. I wanted to post the code, but its pretty huge, and its Visual Perl .NET, so I didn't know if anyone would be familiar with it anyways. I think I will attempt the first response and try to use shared variables in my threads. Does anyone have a good example of doing this?

    BrowserUK, I considered what you said, and I don't think that I can seperate the messages from the GUI. I really want the results to appear on the GUI. I'd like to hope that I could make the entire GUI a shared variable, .NET uses $this as a reference (or I guess it could be $self), I'd like to share $this, I guess, if that's possible, so that I could output results to the GUI, possibly add a progress bar to the GUI, etc.

    I'm not sure if this is exactly possible.

    Thanks for the responses!

      Going Up - threads article on perl.com ..

      Note: You can't share Objects using ithreads, so probably not your GUI either.

      Note2: What BrowserUK meant was to write your results to some shared variable somewhere, and get the GUI to check for them and update itself in the normal process of things.

      C.

      PS: What the h*** is Visual Perl .NET?

        Haha, its a new product from ActiveState. It works with Visual Studio .NET's framework. I decided to grab it and play with it for a little while. A friend of mine is a .NET fanatic and bought a full version of it. I actually kind of like it. Its sorta like Perl mixed with C#. I'm a Java programmer at the heart of things, so it's a better transition for me than learning Perl/Tk.

        This article looks interesting, hopefully I will find my answers there.

        You know, I have some more questions on threading, specifically, how to thread a job to multiple machines, think I should just post another question?