Solved

Code run - after instantiate GUI-Controls in Thread - is not executed ???

Posted on 2009-05-10
4
217 Views
Last Modified: 2012-05-06
Hello

I have 2 Form. In the MainWindow I start a thread which creates the second Form -ProgressWindow - and I try to set a value in the ProgressBar but nothings happens, WHY?

I get no exception just nothing happens after the Dialog is shown.
FORM_1:
 

public partial class MainWindow : Form

    {

        public MainWindow()

        {

            InitializeComponent();

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            var myThread = new Thread(Calculate_Tread);

            myThread.Start();

        }
 

        public void Calculate_Tread()

        {

            var myProgressWindow = new ProgressWindow();

            myProgressWindow.ShowDialog();

            Console.WriteLine("code after ShowDialog");

            myProgressWindow.SetValue(80);
 

        }

    }
 

FORM_2:
 

public partial class ProgressWindow : Form

    {

        public ProgressWindow()

        {

            InitializeComponent();

        }
 

        public void SetValue(int value)

        {

            progressBar1.Value = value;

        }

    }

Open in new window

0
Comment
Question by:MSFanboy
  • 3
4 Comments
 
LVL 4

Expert Comment

by:somnatic
ID: 24349056
Might be because you are calling a GUI object from within another thread (though you whould get an exception). Try using a BackgroundWorker and the according events.
0
 

Author Comment

by:MSFanboy
ID: 24349786
I call the GUI object from the thread it was created so there should NOT be a problem and if there would be the typical problem I would get such a message:
"The calling thread cannot access this object because a different thread owns it."

But I did NOT get such a message! To make sure 100% I even invoked the whole creation of the progresswindow object and there is no difference...
Backgroundworker sucks and I have already implemented my threads so I do NOT want to refactor whole code. Til now I havent had any problems with gui access and threads only now its the first time that I have outsourced my progressbar in another Form instead of using the progressBar in the Form where the thread runs.
0
 

Author Comment

by:MSFanboy
ID: 24352948
one thing I found out is that form.ShowDialog() is blocking the thread somehow because if I use form.Show() the code after its call is executed.
Someone knows more?
0
 

Accepted Solution

by:
MSFanboy earned 0 total points
ID: 24371890
above is my solution

using System;

using System.Threading;

using System.Windows.Forms;
 

namespace ThreadTest

{

    public partial class MainWindow : Form

    {

        /// <summary>

        /// Hält eine Instanz der Progressform-Klasse zur

        /// Darstellung während des Longrunning-Task.

        /// </summary>

        private ProgressWindow progressForm;
 
 

        /// <summary>

        /// Delegate um die ProgressForm Thread-safe zu schliesen

        /// </summary>

        delegate void CloseDialogCallback();
 
 

        public MainWindow()

        {

            InitializeComponent();

            this.progressForm = new ProgressWindow();
 
 

          

        }  
 

        /// <summary>

        /// Dummy-Job

        /// </summary>

        /// <param name="obj">

        /// Dummy-Objekt um ParameterizedThreadStart zu simulieren.

        /// </param>

        private void DoSomething(object obj)

        {

           

            {

                // 3 Sekunden im Kreis fahren

                int counter = 0;

                while (counter < 100)

                {

                    Thread.Sleep(100);

                    counter++;
 

                   this.IncreaseProgressBar();

                }

            };

                
 
 

            // Kreisfahrt fertig, Progressfenster schliesen

            this.CloseDialog();

        }
 

        private void IncreaseProgressBar()

        {

            // prüfen ob von unterschiedlichen Threads aufgerufen wird

            if (this.progressForm.InvokeRequired)

            {

                // Threads unterschiedlich, Delegaten initialisieren

                CloseDialogCallback callBack = new 

CloseDialogCallback(this.IncreaseProgressBar);
 
 

                // Delegaten asynchron, im Kontext des UI-Thread, aufrufen

                this.Invoke(callBack);

            }

            else

            {

                // jetzt asynchron im UI-Kontext das Progressfenster 

schliesen.

                this.progressForm.IncreaseValue();

            }

        }
 
 

        /// <summary>

        /// Schliest das Progress-Fenster Thread-safe

        /// </summary>

        private void CloseDialog()

        {

            // prüfen ob von unterschiedlichen Threads aufgerufen wird

            if (this.progressForm.InvokeRequired)

            {

                // Threads unterschiedlich, Delegaten initialisieren

                CloseDialogCallback callBack = new 

CloseDialogCallback(this.CloseDialog);
 
 

                // Delegaten asynchron, im Kontext des UI-Thread, aufrufen

                this.Invoke(callBack);

            }

            else

            {

                // jetzt asynchron im UI-Kontext das Progressfenster 

schliesen.

                this.progressForm.Close();

            }

        }
 

        private void button1_Click(object sender, EventArgs e)

        {

            // erst den longrunning Thread starten da die Progressform 

das UI blockiert.

            var thread = new Thread(this.DoSomething);

            thread.Start(new object());
 
 

            // Progressform anzeigen

            this.progressForm.ShowDialog(this);

        }

    }

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 159
Converting C# Objects into JSON file 7 65
Eagerly loading related objects in entity framework 5 24
Error on link 14 36
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now