Access from another thread.

10 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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
  5. 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.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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
  11. 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.

     
Back to Top
UI for WinForms is Visual Studio 2017 Ready