Access from another thread.

14 posts, 0 answers
  1. pmoney pmoney
    pmoney pmoney avatar
    23 posts
    Member since:
    Dec 2009

    Posted 21 Dec 2010 Link to this post

    How can I access radDesktopAlert1.show from another thread?
  2. pmoney pmoney
    pmoney pmoney avatar
    23 posts
    Member since:
    Dec 2009

    Posted 21 Dec 2010 Link to this post

    Never mind I got it working correctly. :)
  3. Jonathan Hylton
    Jonathan Hylton avatar
    20 posts
    Member since:
    Sep 2009

    Posted 09 Feb 2011 Link to this post

    Hi Pmoney,

    can you share how you did this? I am looking for the same thing here. My scenario is when my form loads, i spawn a background worker that checks a database table once every 5 minutes in a loop. I want to open a desktop alert if a particular value is returned in the sql query. I would like to then update the content with what was returned from the query and show the alert.

    thanks!

    Jonathan
  4. Brian
    Brian avatar
    15 posts
    Member since:
    Dec 2010

    Posted 11 Feb 2011 Link to this post

    I have had issues with popping an alert from a sepearate thread from the main gui thread. The way that I got around this was by alerting the main thread (with a delegate) to pop the alert and with what information.
  5. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 11 Feb 2011 Link to this post

    Hello Jonathan,

    Thank you for your question.

    I am posting below a sample code that shows how to use a background worker to spawn desktop alerts. The key part is using a MethodInvoker to protect your code from cross-thread operations and show the alerts in the UI thread rather than the background one:
     
    private void Form1_Load(object sender, EventArgs e)
    {
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerAsync();
    }
     
    private void CreateAndShowAlert(string caption, string text)
    {
        RadDesktopAlert desktopAlert = new Telerik.WinControls.UI.RadDesktopAlert();
        desktopAlert.CaptionText = caption;
        desktopAlert.ContentText = text;
        desktopAlert.Show();
    }
     
    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        int i = 0;
        while (true)
        {
            Thread.Sleep(3000);
            i++;
            MethodInvoker mi = new MethodInvoker(delegate() { this.CreateAndShowAlert("Alert " + i, "This is the " + i + "th alert"); });
            this.Invoke(mi);
        }
    }

    Hope this answers your question. If you need further help, do not hesitate to contact me.

    Regards,
    Ivan Todorov
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
  6. Mark
    Mark avatar
    5 posts
    Member since:
    Jul 2012

    Posted 21 Jun 2011 Link to this post

    This assumes you can access the UI thread.

    This seems like a suitable replacement for MessageBox.Show(). MessageBox doesn't need to be on the UI thread.
    If I try to show an alert from outside the UI thread I get a black alert that I can not close and is unresponsive.
  7. Ivan Todorov
    Admin
    Ivan Todorov avatar
    688 posts

    Posted 24 Jun 2011 Link to this post

    Hello Chris,

    If you cannot access the UI thread, you can create a new one by the Application.Run() method. Below is a sample of this.

    The following form hides itself and only displays a RadDesktopAlert. The form is closed when the alert is closed:
    public partial class AlertForm : Form
    {
        public AlertForm()
        {
            InitializeComponent();           
            RadDesktopAlert alert = new RadDesktopAlert();
            alert.Closed += new RadPopupClosedEventHandler(alert_Closed);
            alert.Show();
        }
     
        void alert_Closed(object sender, RadPopupClosedEventArgs args)
        {
            this.Close();
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Hide();
            this.ShowInTaskbar = false;
        }
    }

    The following form runs the previous one in a separate thread. The result is that a RadDesktopAlert is shown without having access to the main form.
    public partial class MainForm: Form
    {
        public MainForm()
        {
            InitializeComponent();
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerAsync();
        }
           
        void bw_DoWork(object sender, DoWorkEventArgs e)
        
            Application.Run(new Form26());
        }
    }

    I hope this is useful. Should you have any additional questions, do not hesitate to ask.

    All the best,
    Ivan Todorov
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  8. Mark
    Mark avatar
    5 posts
    Member since:
    Jul 2012

    Posted 01 Jul 2011 Link to this post

    I just made a static variable and got access to it.

    I like your idea though. I never thought about doing that.
  9. Steve
    Steve avatar
    11 posts
    Member since:
    Jul 2012

    Posted 28 Mar 2014 in reply to Ivan Todorov Link to this post

    Ivan,

    This info was VERY useful.  Thank you.  I was expecting this control to work like MessageBox and have been searching both for a reason it doesn't and a solution.  Still don't know the reason but at least now I know the solution.   Solutions like this should be part of the Telerik documentation, or at least the documentation should note that this control can't be (easily) shown from a thread that's not the main UI thread. 

    Thanks again, 
    Steve
  10. George
    Admin
    George avatar
    500 posts

    Posted 02 Apr 2014 Link to this post

    Hello Steve,

    Thank you for writing.

    The RadMessageBox is essentially showing a RadForm which is a Form. When a Form is shown from a thread with no message pump, it starts its own, temporary message pump. However, RadDesktopAlert is a component which does not have such functionality, that is where the difference is.

    It is a good practice and is in most cases mandatory to access/create/show controls or forms only from the UI thread, that is why we have not considered adding such documentation articles. We have, however a KB article which is concerning RadGridView, however the practices applied in that article can be used with other components. You can find the article here. You can also read the articles at the bottom.

    Let me know if you have further questions.

    Regards,
    George
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  11. Clara
    Clara avatar
    9 posts
    Member since:
    Oct 2016

    Posted 07 Jul Link to this post

    Hi everyone!

    We have a problem with the RadDesktopAlert we are using in our Application (we are using radForm). 

    We have a button in our app, in which we have some specific code to achive some tasks when the user press it, so we want that when users press the button a RadDesktopAlert will be shown in their screen to inform them that the Tool is creating the files and once the files are created, the RadDesktopAlert must be closed. 

    Our problem is that if I add this.radDesktopAlert1.Show(); in the beggining of the button method, the radDesktopAlert1 is shown in my screen but if it is inside of an if-else it is not shown and I have no idea why it is.

    I have added an attachment which shows a part of the code where I have to use the radDesktopAlert1 and it is not working, so please can you be so kind and help me with this? I don't really understand why the radDesktopAlert1  it is working when it is in the begining of the button method and why it is NOT working just a few lines below.

    THANKS A LOT IN ADVANCE!      

  12. Hristo
    Admin
    Hristo avatar
    1059 posts

    Posted 10 Jul Link to this post

    Hello Clara,

    Thank you for writing.

    Can you please check if your if-else clause has been hit and you are able to break there? If you are actually able to break and the desktop alert still does not show I am going to need a sample project to be investigated locally. In order to send us your project, you will need to use the ticketing straight from your Telerik Account.

    I hope this helps. Please let me know if you have other questions.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  13. Clara
    Clara avatar
    9 posts
    Member since:
    Oct 2016

    Posted 17 Jul in reply to Hristo Link to this post

    Hi,

    Yes, I am able to break inside the if/else clause, but the problem is that I have a dialogreult where the User has to press yes or not. In case they press yes, the raddesktopalert should be shown in the screen, but it is not like that. The thing is that if I put the raddesktopalert.show() after de dialogresult.Yes it is not shown. If I put it above the dialogresult it is shown. I don't understand why.

    Any ideas why?

     

    Thanks a lot in advance!

  14. Hristo
    Admin
    Hristo avatar
    1059 posts

    Posted 18 Jul Link to this post

    Hi Clara,

    Thank you for writing back.

    I am not able to reproduce this in a simple scenario. Can you please check the attached project? In case you keep experiencing the issue please open a support ticket and send us a project reproducing the behavior on your end.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top