?
Solved

Threading issue in C#.

Posted on 2011-04-21
17
Medium Priority
?
239 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.
0
Comment
Question by:jazzIIIlove
  • 9
  • 8
17 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35440162
What code are you using?...
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35446881
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35447543
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35462625
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 35462655
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35462699
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35462726
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463033
>>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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463044
typo:
should be:

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


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

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35463144
I'm not getting that symptom.  What is happening inside FormBeforeMainForm()?
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463259
One label, one button and one picture box is initialized, that's all...but there is the hourglass/sandglass shape of mouse.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 35463274
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463328
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35463339
You should be able to select "Default" for the Cursor...that's not working?
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463354
ok, did it...wow, weird, the problem was from the very first, the cursor!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35463359
Hehe...glad we were able to figure it out together...  =)
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35463363
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 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