[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

thread and delegate problem

hi,
I have two forms (in c# 2005) .....
the first form has main functionality of the application ... and the second one has loading image (loading gif) ...

I create the second form just to show it when the user click generate button ....
I put the code generation serial in loading gif form ....
now the problem, is that the loading gif loading its animation (freezed) ...

I have done threading but it gives me cross thread problem, I search in internet, so I found delegate for event handling,,,, I use delegate with thread, but its freezed again ..

so how can I remove freezing from loading gif ??
any help ??


here the code snippet of loading gif form:




private void GenIDAndActivateOnline_Load(object sender, EventArgs e)
        {
            LicChecking = new Thread(new ThreadStart(ExecuteLicCheck));
            LicChecking.Start();
        }

        public void ExecuteLicCheck()
        {
            if (this.InvokeRequired)
            {
                LicCheckingDelegate LicDelegate = new LicCheckingDelegate(ExecuteLicCheck);
                this.Invoke(LicDelegate);
            }
            else
            {
                if (!Installer_API.GenID(serialNo, "id_gen.exe", out hardwareId))
                {
                    hardwareId = null;
                    this.Close();
                }

                if (!Installer_API.ActivateOnline(hardwareId, out lic))
                {
                    lic = null;
                    this.Close();
                }
           }

Open in new window

1.jpg
2.jpg
0
Ameerh24
Asked:
Ameerh24
  • 5
  • 3
  • 2
2 Solutions
 
Priest04Commented:
You should put the code that generates serial into another async thread, so your UI thread will be able to refresh itself. Also, you should loose the second form and just display picture box over the main form.

Goran
0
 
Priest04Commented:
Here is a great article where you can read about sync and async code execution.
0
 
Priest04Commented:
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
Ameerh24Author Commented:
I replace Invoke() with BeginInvoke() .. but nothing changed !!
0
 
Fernando SotoCommented:
Hi Ameerh24;

It looks like you are running the web request in the UI thread and this will freeze the UI thread. Try placing the web request in a thread. I put together a working threading program with a second form, animation, and it is working. If you have any question please ask.

Fernando
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;
using System.Threading;

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

        // Delegate for any thread to call to modify the UI of the program
        private delegate void UpdateUICaller(int count);
        // Delegate to kill the WaitForm if canceling the process
        private delegate void KillWaitDialogCaller();
        // Reference to the wait form with the animation
        WaitForm wf;
        // Reference to the thread in case user cancel process
        Thread threadLRP;

        private void button1_Click(object sender, EventArgs e)
        {
            // Create a thread to run the web request so not to
            // tie-up the UI thread and freez the animation
            threadLRP = new Thread(LongRunningProcess);
            // Start the new thread, web request
            threadLRP.Start();
            wf = new WaitForm();
            // Show the new form with the animation
            wf.Show();
        }

        private void LongRunningProcess()
        {
            // Simulate a long running process, In your
            // code this should be the web request code
            for ( int i = 0; i < 300; i++)
            {
                Thread.Sleep(100);
                if (i % 5 == 0)
                {
                    // Report to the UI thread that 5 more units has passed
                    UpdateUI(i);
                }
            }
            KillWaitDialog();
        }

        private void UpdateUI(int count)
        {
            // Check to see if this function was called by the UI thread
            // if it was call the else part of the if statement. If not
            // called by UI thread create a dellegate and call this method again
            if (this.InvokeRequired)
            {
                UpdateUICaller update = new UpdateUICaller(UpdateUI);
                // Call this method again with the same parameter passed in
                this.Invoke(update, new object[] { count });
            }
            else
            {
                // We are in the UI thread and so it is OK to modify the UI now.
                // Updating a label control on the main form
                label1.Text = "Count = " + count.ToString();
            }
        }

        private void KillWaitDialog()
        {
            // The long running process was canceled
            if (this.InvokeRequired)
            {
                KillWaitDialogCaller kill = new KillWaitDialogCaller(KillWaitDialog);
                // Call this method again 
                this.Invoke(kill);
            }
            else
            {
                // We are in the UI thread and so it is OK to modify the UI now.
                if (threadLRP.IsAlive)
                {
                    threadLRP.Abort();
                    wf.Close();
                    wf = null;
                }
                else
                {
                    if (wf != null)
                    {
                        wf.Close();
                        wf = null;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            KillWaitDialog();
        }
    }
}

Open in new window

0
 
Ameerh24Author Commented:
hi FernandoSoto,
can you please send the whole project of "WindowsFormsApplication14 " as a zip file ??
0
 
Ameerh24Author Commented:
but there is onething FernandoSoto,

I put the code of web request inside the "wait form" not in main form !!

is it possible to keep it in wait form ??
0
 
Ameerh24Author Commented:
and by the way, is it possible to use ShowDialog() instead of Show() ??
0
 
Ameerh24Author Commented:
hi FernandoSoto,
No need to send anything, everything works fine :)
0
 
Fernando SotoCommented:
Not a problem, glad you were able to work you your issues.  ;=)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now