Threading issue in C#.

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.
LVL 12
jazzIIIloveAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
I couldn't reproduce the problem.  Are you sure you didn't accidentally change the Cursor() property of the Form being shown as a dialog?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
What code are you using?...
0
 
jazzIIIloveAuthor Commented:
Ah sorry, I forgot to add the link, here you go:

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

Kind regards
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
 
jazzIIIloveAuthor 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.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Why not simplify?
private void myBtn_Click(object sender, EventArgs e)
        {
            while (this.Opacity > 0)
            {
                this.Opacity -= 0.1;
                Application.DoEvents();
                System.Threading.Thread.Sleep(50);
            }
            this.DialogResult = DialogResult.OK;
        }

Open in new window

0
 
jazzIIIloveAuthor 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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
 
jazzIIIloveAuthor 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.
0
 
jazzIIIloveAuthor Commented:
typo:
should be:

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


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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm not getting that symptom.  What is happening inside FormBeforeMainForm()?
0
 
jazzIIIloveAuthor Commented:
One label, one button and one picture box is initialized, that's all...but there is the hourglass/sandglass shape of mouse.
0
 
jazzIIIloveAuthor 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?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You should be able to select "Default" for the Cursor...that's not working?
0
 
jazzIIIloveAuthor Commented:
ok, did it...wow, weird, the problem was from the very first, the cursor!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hehe...glad we were able to figure it out together...  =)
0
 
jazzIIIloveAuthor 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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.