2

I have written a C# DLL COM-library which is accessed by VBScript. All the library stuff is written in the main program without any form.

Later I have added a form which only shows a status (like "49% done"). This worked very well, however when the DLL has to calculate a lot of stuff the form "hangs". I want to have the form accessible and movable all the time.

So I decided to make an own thread for the form. It was easy to create the thread and the form, but I have big problems to access this form from the main program. I just want to set the title for example.

I call the form like this:


namespace PART1.PART2
{
    public class CLASSNAME
    {
        private Thread fStatus;
        private frm_Status fStatusForm;

        public void runShowStatus()
        {
            if (fStatus == null)
            {
                fStatus = new Thread(new ThreadStart(threadForm));
                fStatus.Start();
            }
        }

        private void threadForm()
        {
            fStatusForm = new frm_Status();
            Application.Run(fStatusForm);
        }
    }
}

The form itself is a standard form. No special code so far.

There are similar post to this but there are two main differences:

  • The form is in a thread - the form is NOT in the main program and the form is NOT the main form.
  • The main program tries to access the form - the form does NOT try to access the main program.
4
  • You went from bad to worse, VBScript doesn't support threading at all. Use BackgroundWorker to perform expensive calculations, its ReportProgress method to get UI updated. Nov 27, 2012 at 14:17
  • Thank you for the reply. I do not know anything about BackgroundWorker. Maybe you can give me an example? VBScript does not access the form or any other thread. It does not even know that there is another thread. (This is intended)
    – Klaus F.
    Nov 27, 2012 at 14:54
  • 1
    Half a million Google hits, start from the top. Nov 27, 2012 at 15:00
  • I have solved it with a really easy thing. It was not even necessary to make any changes to my thread or caller. I just added a new class named "Status". One instance was created globally. Now I can access the members of this class from main thread and from subthread.
    – Klaus F.
    Feb 15, 2013 at 17:01

1 Answer 1

0

You can add a volatile variable to the main class CLASSNAME like this:

private static volatile int progress = 0;

Now you can change its value from the main thread, and read the value from the form thread.

1
  • How can I access this variable from another class (the form class)?
    – Klaus F.
    Nov 27, 2012 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.