We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Threading issue in C#.

jazzIIIlove
jazzIIIlove asked
on
Medium Priority
258 Views
Last Modified: 2012-06-22
Hi there;

I have an application that requires multi-threading as I use the following links code for a fade effect in my application. So, how and where should I create and use the thread, any tips?

Kind regards.
Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
What code are you using?...
CERTIFIED EXPERT

Author

Commented:
Ah sorry, I forgot to add the link, here you go:

https://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26963561.html

Kind regards
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Really not much benefit to threading this over a timer...but here it is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        bool fadingComplete = false;
        System.Threading.Thread T = null;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!fadingComplete)
            {
                if (T == null)
                {
                    T = new System.Threading.Thread(new System.Threading.ThreadStart(Fade));
                    T.Start();
                }
                e.Cancel = true;
                return;
            }

            base.OnClosing(e);
        }

        private void Fade()
        {
            while (this.Opacity > 0)
            {
                this.BeginInvoke((MethodInvoker)delegate() { this.Opacity -= 0.1; });
                System.Threading.Thread.Sleep(50);
            }
            fadingComplete = true;
            this.BeginInvoke((MethodInvoker)delegate() { this.Close(); });
        }

    }
}

Open in new window

CERTIFIED EXPERT

Author

Commented:
Hi there;

Ok, you did the stuff onclosing method of the form, I try to did it after a button click, I moved the code to my button's click action and start the thread there but for some reason, during debugging, the main thread continues to execute, that's the reason I try to lock the thread we created, but it seems it's not locked, what should be done is, the thread we created finishes, its work, which fading and then the form should be destroyed. What should I do?

Here you the code:

   static readonly object _locker = new object();
        private void myBtn_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            if (!fadingComplete)
            {
                if (T == null)
                {
                    ThreadStart threadFade = new ThreadStart(Fade);
                    T = new Thread(threadFade);
                   
                    lock (_locker)
                    {                    
                        T.Start();
                    }

Kind regards.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
You are really talented :)

Yes, this works too, thanks but after the initiation of the form, simply, when I put the mouse cursor on the form, the mouse cursor is as hourglass/sandglass shape, not as regular, as if it suffers in the initiation of something, but there is only 1 button and 1 picture, that's all.

You already deserved 500 pts by the way.

Kind regards.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You mean when the form is simply open (not during closing), and before you have pressed the button, you have an hourglass cursor?

How are you displaying the form?  What else is happening in the form?
CERTIFIED EXPERT

Author

Commented:
>>You mean when the form is simply open (not during closing), and before you have pressed the >>button, you have an hourglass cursor?
yep, exactly you said. Here you go,

  MainForm mf = new MainForm(a,b,c);


            FormBeforeMainForm fbmf = new FormBeforeMainForm();
            bF.ShowDialog();
                       
            Application.Run(fM);

Kind regards.
CERTIFIED EXPERT

Author

Commented:
typo:
should be:

MainForm mf = new MainForm(a,b,c);


            FormBeforeMainForm fbmf = new FormBeforeMainForm();
            fbmf.ShowDialog();
                       
            Application.Run(mf );

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
I'm not getting that symptom.  What is happening inside FormBeforeMainForm()?
CERTIFIED EXPERT

Author

Commented:
One label, one button and one picture box is initialized, that's all...but there is the hourglass/sandglass shape of mouse.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT

Author

Commented:
wow, you are right but I am sure that I didn't do, and for some reason, I couldn't revert to standard cursor, every control is under WaitCursor???

Why?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
You should be able to select "Default" for the Cursor...that's not working?
CERTIFIED EXPERT

Author

Commented:
ok, did it...wow, weird, the problem was from the very first, the cursor!
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Hehe...glad we were able to figure it out together...  =)
CERTIFIED EXPERT

Author

Commented:
Ok, obvious that in this question, you rocked!

I hope we will be meeting in another C# question as I am asking too many C# questions lately and you are really talented, maybe you can help if I have another.

Thanks for real effort!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.