how to close all the open form in WPF - wpf

In my Application I have two buttons "open" and "close".
When I click open button window will be opened, when I click close button window will be closed.
When I click open button 3 times, 3 windows will be opened. I want to close all window when I click close button.
Here is my code [Please don't try to Change the Thread because that is my requirement in my Application]
public partial class MainWindow : Window
{
Window ProgressWindow;
Thread ProgressThread;
public MainWindow()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThread = new Thread(() =>
{
ProgressWindow = new Window();
ProgressWindow.Margin = new Thickness(0, 0, 50, 0);
ProgressWindow.WindowState = WindowState.Normal;
ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
ProgressWindow.Height = 180;
ProgressWindow.Width = 180;
ProgressWindow.Content = "Hello WPF";
ProgressWindow.ShowInTaskbar = false;
ProgressWindow.Show();
ProgressWindow.Closed += (sender2, e2) =>
ProgressWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
if (ProgressThread.IsAlive == true)
{
ProgressThread.Abort();
}
}
}

I would recommend to store references to created windows, your code can look like this:
Stack<Window> ProgressWindow=new Stack<Window>();
Thread ProgressThread;
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThread = new Thread(() =>
{
var window = new Window();
window.Margin = new Thickness(0, 0, 50, 0);
window.WindowState = WindowState.Normal;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.Height = 180;
window.Width = 180;
window.Content = "Hello WPF";
window.ShowInTaskbar = false;
window.Show();
window.Closed += (sender2, e2) =>
window.Dispatcher.InvokeShutdown();
ProgressWindow.Push(window);
System.Windows.Threading.Dispatcher.Run();
});
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
while (ProgressWindow.Count > 0)
{
ProgressWindow.Pop().Close();
}
}
thread aborting is not recommended if it is "normal" workflow of your application, i.e. window wasn't closed because of some critical error

I wouldn't recommend what you are doing and actually I don't really know if it works like this, but since you stated that it's your (strange) requirement to use threads like this, I will only comment on the actual problem:
You should save the threads in a List and then close all the threads from this list.
Edit:
public partial class MainWindow : Window
{
Window ProgressWindow;
List<Thread> ProgressThreads = new List<Thread>();
public MainWindow()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThreads.Add(new Thread(() =>
{
ProgressWindow = new Window();
ProgressWindow.Margin = new Thickness(0, 0, 50, 0);
ProgressWindow.WindowState = WindowState.Normal;
ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
ProgressWindow.Height = 180;
ProgressWindow.Width = 180;
ProgressWindow.Content = "Hello WPF";
ProgressWindow.ShowInTaskbar = false;
ProgressWindow.Show();
ProgressWindow.Closed += (sender2, e2) =>
ProgressWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
}));
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
foreach(var ProgressThread in ProgressThreads)
{
if (ProgressThread.IsAlive == true)
{
ProgressThread.Abort();
}
}
}
}

You will need to keep a record of all threads you have opened when you click "Open". Then in your "Close" method loop over that list closing each one.
Member variable:
List<Thread> allThreads = new List<Thread>();
Then in your open handler add:
allThreads.Add(ProgressThread);
Then your close handler becomes:
foreach (Thread thread in allThreads)
{
if (thread.IsAlive)
{
thread.Abort();
}
}
That what you are trying is unorthodox should go without saying.

Related

Simulating a Drag/Drop event in WPF

I want to simulate a drag/drop event in WPF.
For this I'll need to gain access to the data stored in the "Drag/Drop buffer" and also I'll need to create a DragEventArgs.
I noticed that the DragEventArgs is sealed and has no public ctor.
So my questions are:
1. how can I create an instance of DragEventArgs?
2. How can I gain access to the drag/drop buffer?
i recently do this! i simulated drag/drop with MouseDown, MouseMove and MouseUp events. for example for my application, i have some canvases that i want to drag and drop them. every canvas has an id. in MouseDown event, i buffer its id and use it in MouseMove and MouseUp event. Desktop_Canvas is my main Canvas that contains some canvases. these canvases are in my dictionary (dic).
here is my code:
private Dictionary<int, Win> dic = new Dictionary<int, Win>();
private Point downPoint_Drag = new Point(-1, -1);
private int id_Drag = -1;
private bool flag_Drag = false;
public class Win
{
public Canvas canvas = new Canvas();
public Point downpoint = new Point();
public Win()
{
canvas.Background = new SolidColorBrush(Colors.Gray);
}
}
private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
{
try
{
Point movePoint = e.GetPosition(Desktop_Canvas);
if (flag_Drag && downPoint_Drag != new Point(-1, -1))
{
double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
downPoint_Drag = movePoint;
if (x == -1)
x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
if (y == -1)
y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
Canvas.SetLeft(dic[id_Drag].canvas, x);
Canvas.SetTop(dic[id_Drag].canvas, y);
}
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
downPoint_Drag = new Point(-1, -1);
id_Drag =-1;
flag_Drag = false;
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
{
MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
Desktop_Canvas_MouseLeftButtonUp(null, ee);
}
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
downPoint_Drag = e.GetPosition(Desktop_Canvas);
int hoverId = HoverWin(downPoint_Drag);
flag_Drag = true;
id_Drag = hoverId;
dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
}
private int HoverWin(Point p)
{
foreach (int i in dic.Keys)
{
if (dic[i].canvas.IsMouseOver)
return i;
}
return -1;
}

Change the location of the window form in c#?

In window application, using c# i created one form and put visible false minimize, maximize button and formborder to none, i place one panel at top of the form, in that panel i place close, minimize buttons. Now how can i drag the window form. Any reference please. my code is
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.None;
Thank you.
Simply register the MouseDown, MouseMove and MoueUp events for your Panel
bool MouseDownFlag = false;
Point start = new Point(0, 0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
MouseDownFlag = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlag)
{
Point newPoint = new Point();
newPoint.X = this.Location.X - (start.X - e.X);
newPoint.Y = this.Location.Y - (start.Y - e.Y);
this.Location = newPoint;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
MouseDownFlag = false;
}

WPF: Storyboard loop object owned by another thread

Hi all my simple code looks like this
private void flipForeverever(object sender, EventArgs args)
{
moveYs = new DoubleAnimation();
m2oveYs = new DoubleAnimation();
try
{
sf.Remove(this);
sf.Children.Clear();
}
catch (Exception e)
{
}
if (firstPanelAngle == 360)
{
moveYs.To = 180;
moveYs.From = 0;
}
else
{
moveYs.To = 360;
moveYs.From = 180;
}
if (secondPanelAngle == 360)
{
m2oveYs.To = 180;
m2oveYs.From = 0;
}
else
{
m2oveYs.To = 360;
m2oveYs.From = 180;
}
sf = (Storyboard)FindResource("Storyboard1");
Storyboard.SetTargetName(moveYs, "rotatePanel");
Storyboard.SetTargetProperty(moveYs, new thisPropertyPath(AxisAngleRotation3D.AngleProperty));
Storyboard.SetTargetName(m2oveYs, "rotateSecond");
Storyboard.SetTargetProperty(m2oveYs, new PropertyPath(AxisAngleRotation3D.AngleProperty));
sf.Children.Add(moveYs);
sf.Children.Add(m2oveYs);
// sf.RepeatBehavior = RepeatBehavior.Forever;
if (flipForever)
{
sf.Completed += new EventHandler(delaythespin);
sf.Begin(this);
}
}
private void delaythespin(object sender, EventArgs args)
{
sf.Stop(this);
System.Timers.Timer timer = new System.Timers.Timer(500);
timer.Elapsed += new System.Timers.ElapsedEventHandler(flipForeverever);
timer.Enabled = true;
firstPanelAngle = rotatePanel.Angle;
secondPanelAngle = rotateSecond.Angle;
timer.Start();
}
So basically i call flipForeverever through a click call and it is supposed to loop forever until i set flipforever to false... But then it is giving me this error...
The calling thread cannot access this object because a different thread owns it.
Any help will really be appreciated
It sounds like you might be having thread affinity problems. Have you tried using a DispatcherTimer instead of a System.Timers.Timer?
private DispatcherTimer _timer;
private void GoButton_Click(object sender, RoutedEventArgs e)
{
_timer = new DispatcherTimer(); // create timer
_timer.Interval = new TimeSpan(0, 0, 1); // tick every 1s
_timer.Tick += new EventHandler(_timer_Tick); // method to call
_timer.Start(); // start timer
}
void _timer_Tick(object sender, EventArgs e)
{
// we should be on the correct thread now
GoButton.Background = Brushes.Aqua;
}
Or, if you need to use System.Timers.Timer, use Invoke or BeginInvoke to get on the correct thread after the timer fires?
private System.Timers.Timer _timer;
private void GoButton_Click(object sender, RoutedEventArgs e)
{
_timer = new System.Timers.Timer(1000);
_timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// use Dispatcher.Invoke on the UI object that you want to modify
// to get on the correct thread for that UI object
GoButton.Dispatcher.Invoke((ThreadStart)(() =>
{
GoButton.Background = Brushes.Aqua;
}));
}

this is not waiting form, but completion notification of thread is required

private void btnSend_Click(object sender, RoutedEventArgs e)
{
Button obj=(Button)sender;
obj.Content="Cancel";
SendImage send = new SendImage();
Thread t = new Thread(send.Image);
t.Start();
//run separate thread.(very long, 9 hours)
//so dont wait.
//but the button should be reset to obj.Content="Send"
//Can I do this?
}
I want the button to be reset to "Send" (after completion of thread). But form should not wait. Is this possible?
You can do this more elegantly using the BackgroundWorker class.
XAML for the Button:
<Button x:Name="btnGo" Content="Send" Click="btnGo_Click"></Button>
Code :
private BackgroundWorker _worker;
public MainWindow()
{
InitializeComponent();
_worker = new BackgroundWorker();
_worker.WorkerSupportsCancellation = true;
_worker.WorkerReportsProgress = true;
}
private void btnGo_Click(object sender, RoutedEventArgs e)
{
_worker.RunWorkerCompleted += delegate(object completedSender, RunWorkerCompletedEventArgs completedArgs)
{
Dispatcher.BeginInvoke((Action)(() =>
{
btnGo.Content = "Send";
}));
};
_worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
Dispatcher.BeginInvoke((Action)(() =>
{
btnGo.Content = "Cancel";
}));
SendImage sendImage = args.Argument as SendImage;
if (sendImage == null) return;
var count = 0;
while (!_worker.CancellationPending)
{
Dispatcher.BeginInvoke((Action)(() =>
{
btnGo.Content = string.Format("Cancel {0} {1}", sendImage.Name, count);
}));
Thread.Sleep(100);
count++;
}
};
if (_worker.IsBusy)
{
_worker.CancelAsync();
}
else
{
_worker.RunWorkerAsync(new SendImage() { Name = "Test" });
}
}
Make the Button a member of your Window/UserControl class (by giving it a Name in XAML). When the thread eventually finishes, do this before returning from the thread method:
myButton.Dispatcher.BeginInvoke(
(Action)(() => myButton.Content = "Send"));

How to create and use WebBrowser in background thread?

How can I create System.Windows.Forms.WebBrowser in background STA thread? I try use some code like this:
var tr = new Thread(wbThread);
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
private void wbThread()
{
CWebBrowser browser = new CWebBrowser();
var text = browser.Navigate("http://site.com", CWebBrowser.EventType.loadCompleted).Body.InnerHtml;
}
CWebBrowser - custom class, wich delegate System.Windows.Forms.WebBrowser object Navigate method and wait until page completed loads. The problem is LoadCompleted event on System.Windows.Forms.WebBrowser object never raises. I found some solution here, but it does not work (can't find method Application.Run() on my WPF app).
public class CWebBrowser : ContentControl
{
public readonly System.Windows.Forms.WebBrowser innerWebBrowser;
private readonly AutoResetEvent loadCompletedEvent;
private readonly AutoResetEvent navigatedEvent;
public enum EventType
{
navigated, loadCompleted
}
public CWebBrowser()
{
innerWebBrowser = new System.Windows.Forms.WebBrowser();
loadCompletedEvent = new AutoResetEvent(false);
navigatedEvent = new AutoResetEvent(false);
System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost();
host.Child = innerWebBrowser;
Content = host;
innerWebBrowser.DocumentCompleted +=new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(innerWebBrowser_DocumentCompleted);
innerWebBrowser.Navigated += new System.Windows.Forms.WebBrowserNavigatedEventHandler(innerWebBrowser_Navigated);
}
void innerWebBrowser_Navigated(object sender, System.Windows.Forms.WebBrowserNavigatedEventArgs e)
{
navigatedEvent.Set();
}
void innerWebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
if (((sender as System.Windows.Forms.WebBrowser).ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete) || innerWebBrowser.IsBusy)
return;
var doc = innerWebBrowser.Document;
loadCompletedEvent.Set();
}
public System.Windows.Forms.HtmlDocument Navigate(string url, EventType etype)
{
if (etype == EventType.loadCompleted)
loadCompletedEvent.Reset();
else if (etype == EventType.navigated)
navigatedEvent.Reset();
innerWebBrowser.Navigate(url);
if (etype == EventType.loadCompleted)
loadCompletedEvent.WaitOne();
else if (etype == EventType.navigated)
navigatedEvent.WaitOne();
System.Windows.Forms.HtmlDocument doc = null;
Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(
delegate
{
doc = innerWebBrowser.Document;
}));
return doc;
}
}
Thansk for all advices and sorry for my bad english :o(
Why don't you use the default WebBrowser control like this?
public MainPage()
{
InitializeComponent();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(startNavigate);
}
void startNavigate()
{
WebBrowser wb = new WebBrowser();
wb.LoadCompleted += new LoadCompletedEventHandler(wb_LoadCompleted);
wb.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(wb_Navigated);
wb.Navigate(new Uri("http://www.google.com"));
}
void wb_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
// e.Content
}
void wb_LoadCompleted(object sender, NavigationEventArgs e)
{
// e.Content when the document finished loading.
}
Edit: You are using old System.Windows.Forms.WebBrowser control, instead System.Windows.Controls.WebBrowser which is part of WPF.

Resources