Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have problem in this code in the comment area "hang the form"

my program will hang there!!!! what is problem??

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication28
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        BackgroundWorker backgw = new BackgroundWorker();
        private void button1_Click(object sender, EventArgs e)          
        {

            backgw.DoWork += new DoWorkEventHandler(k_DoWork);         
          
            ParameterizedThreadStart start = new ParameterizedThreadStart(startthread);
            System.Threading.Thread u;                                                         
            int i = 0;
            while (i < 100)
            {
                //u = new System.Threading.Thread(start);
                //u.Start(i);                                   //1.with thread way

                backgw.RunWorkerAsync(i);                       //2.with backgw way
                
                Thread.Sleep(1000);

                lock (y)
                {
                    Thread.Sleep(1000);
                }
                lock(h)
                i++;
            }
        }

        delegate void j(int h);
        j b;

        object h = new object();
        object y = new object();

        void startthread(object m)
        {
            Monitor.Enter(h);
            Monitor.Enter(y);
            p1((int)m); 
           
            Monitor.Exit(h);
        }

        void p1(int h)
        {
                b = delegate(int q)
                {
                    label1.Text = string.Format("Step is :{0}", h.ToString());
                };
                Monitor.Exit(y);          
                label1.Invoke(b);       //hang the form????
        }

        void k_DoWork(object sender, DoWorkEventArgs e)
        {

            Monitor.Enter(h);
            Monitor.Enter(y);
            p1((int)e.Argument);
            Monitor.Exit(h);
        }
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 5-Feb-11 22:35pm    
The problems here are quire apparent, and the code is a wild mess, now with threading. In disbelieve I reviewed previous answers by mehdi_csharp and found that many good answers were ignored or not understood. I want to give up.

I recommend everyone who wish to answer review those questions, too and decide carefully about posting an answer.

Sorry.
--SA
[no name] 6-Feb-11 14:17pm    
I solve it.
Sergey Alexandrovich Kryukov 6-Feb-11 21:58pm    
To my surprise.
Thank you for reporting back the resolution.
Please see my answer to your last question: http://www.codeproject.com/Answers/154373/Use-of-Invoke-in-a-locked-thread.aspx -- I decided to answer again. I would be happy to know that I was wrong and you're gradually moving in right direction, so wish you good luck!
--SA

hi,
i suspect some deadlock like situation
 
Share this answer
 
i use of this code i thinking correct.

using System;
using System.Threading;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication37
{
    public partial class Form1 : Form
    {
        public delegate void MyDelegate(int o);
        MyDelegate myDelegate;
        IAsyncResult j;
        object a = new object();


        void RunInThread(object u)
        {
            Monitor.Enter(this);
            Thread.Sleep(100);

            myDelegate = delegate(int b)
            {
                label1.Text = u.ToString();
                Thread.Sleep(10);
            };

            j = this.BeginInvoke(myDelegate, (int)u);
            //MessageBox.Show("Hello");

            //Add your code that needs to be executed in separate thread 
            //except UI updation
            Monitor.Exit(this);

        }

        void AddControl(int r)
        {
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            while (i < 100)
            {

                Thread.CurrentThread.Priority = ThreadPriority.Lowest;
                Thread t = new Thread(new ParameterizedThreadStart(RunInThread));

                t.Start(i);
                Thread.Sleep(10);

                Monitor.Enter(this);
                EndInvoke(j);
                label1.Invalidate();
                i++;
                
                Monitor.Exit(this);
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900