Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
In my Application I had A requirement of Notifying the user about the Pending applications
So in the mdiParent I set a Background worker which keeps quering the database to get any pending application and if it finds any display it on the Tooltip on the MdiParent
 


 
 private void button1_Click(object sender, EventArgs e)
            {
                backgroundWorker1.RunWorkerAsync(2000);
            }
       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                fillnotification();
            }
    
    
            public void fillnotification()
            {
                int pending = 0;
                if( Program.USERPK!=0){
    
                    DataTable dt = nftrans.getnotification();
                    pending = dt.Rows.Count;
    
                    String message = "You Have  " + pending + " Applications Pending For Approval";
                 
                 //   toolTip1.SetToolTip(lblStatus , message);
                   toolTip1.Show(message , this, lblStatus.Location);
    
    
    
                  
                }
            }
 
but when Iam running the solution I am getting an exception
 
Cross-thread operation not valid: Control 'MainForm' accessed from a thread other than the thread it was created on.
I understood its due to two different thread but Cannot sort this out can any one suggest a help .I tried the ideas I read in related questions But cannot find a correct solution
Posted 10-May-13 20:49pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can only access Controls from the thread they were created on - the UI thread. Attempting to do so from another thread (such as the BackgroundWorker you are using) will give you an error.
 
There are two ways to do this:
1) You could use Invoke to move the access back to the UI thread.
2) You could tell the main thread it has info.
 
Personally, I would do the second, via the BackgroundWorker Progress reporting mechanism:
Set the WorkerReportsProgress property to true.
Handle the worker ProgressChanged event.
In the handler, get the ProgressChangedEventArgs.ProgressPercentage value and set your tool tip.
In your worker method, replace the tooltip code with a call to the worker.ReportProgress(pending) instead.
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 Maciej Los 270
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 202
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web02 | 2.8.1411019.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100