0

I have a window that contains popup.when the operation was performed, this popup called.now i want to for example when user continuous insert data and show popup , previous called popup closed.because collisions occur. my code is :

public partial class AvinPopup : Window
{
    static AvinPopup _popup;
    static int timePopup = 0;
    static string textPopUp = "";

    private AvinPopup()
    {
        InitializeComponent();
    }
    private static void StartCloseTimer()
    {
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds((double)timePopup);
        timer.Tick += TimerTick;
        timer.Start();
    }

    private static void TimerTick(object sender, EventArgs e)
    {
        DispatcherTimer timer = (DispatcherTimer)sender;
        timer.Stop();
        timer.Tick -= TimerTick;
        _popup.Close();
        _popup.popup.IsOpen = false;

    }
    public static void Show(string _textPopup, int _timePopup = 3)
    {
        timePopup = _timePopup;
        textPopUp = _textPopup;

        Thread newWindowThread = new Thread(ThreadStartPopup);
        newWindowThread.SetApartmentState(ApartmentState.STA);
        newWindowThread.IsBackground = true;
        newWindowThread.Start();
    }

    private static void ThreadStartPopup()
    {

        _popup = new AvinPopup();
        _popup.popup.VerticalOffset = System.Windows.SystemParameters.PrimaryScreenHeight - 200;
        _popup.popup.HorizontalOffset = 100; /*System.Windows.SystemParameters.PrimaryScreenWidth +100;*/
        _popup.txtPopup.Text = textPopUp;
        _popup.Show();
        StartCloseTimer();
        System.Windows.Threading.Dispatcher.Run();
    }
Mohadeseh
  • 360
  • 4
  • 19

1 Answers1

0
private static readonly ManualResetEventSlim _Blocker = new ManualResetEventSlim(false);

private static void ThreadStartPopup()
{
    _Blocker.Reset();
    System.Windows.Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
    {
        if (_popup != null && _popup.IsOpen)
            _popup.IsOpen = false;

         _Blocker.Set();
    }));

    _Blocker.Wait();
    _popup = new AvinPopup();
    _popup.popup.VerticalOffset = System.Windows.SystemParameters.PrimaryScreenHeight - 200;
    _popup.popup.HorizontalOffset = 100; /*System.Windows.SystemParameters.PrimaryScreenWidth +100;*/
    _popup.txtPopup.Text = textPopUp;
    _popup.Show();
    StartCloseTimer();
    System.Windows.Threading.Dispatcher.Run();
}
Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77
  • I get error : The calling thread cannot access this object because a different thread owns it. – Mohadeseh May 30 '17 at 06:12
  • Ok I have edited my example. Now the new `Thread` is activating a `ManualResetEventSlim` Blocker. Then call a `Dispatcher Operation` and after it has finished, the new `Thread` is continuing. – Dominic Jonas May 30 '17 at 06:21
  • Again get this error.Error is from this code : `if (_popup != null && _popup.popup.IsOpen)` and `_popup.popup.IsOpen` is null. Description error : **StackTrace " at System.Windows.Threading.Dispatcher.VerifyAccess() at System.Windows.Threading.DispatcherObject.VerifyAccess() at System.Windows.DependencyObject.GetValue(DependencyProperty dp) at System.Windows.Controls.Primitives.Popup.get_IsOpen() at <>x.<>m0(<>c <>4__this)string** – Mohadeseh May 30 '17 at 07:37