[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mono / tests / cas / threads / swf-control1.cs
1 //
2 // swf-control1.cs (based on swf-begininvoke.cs)
3 //
4 // Authors:
5 //      Jackson Harper (jackson@ximian.com)
6 //      Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // Copyright (c) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10
11 using System;
12 using System.Diagnostics;
13 using System.Drawing;
14 using System.Security;
15 using System.Security.Permissions;
16 using System.Threading;
17 using System.Windows.Forms;
18
19 namespace System.Windows.Forms {
20
21         public class BeginInvokeDemo : Form {
22
23                 private Label label;
24
25                 public BeginInvokeDemo ()
26                 {
27                         label = new Label ();
28                         label.Dock = DockStyle.Fill;
29                         label.TextAlign = ContentAlignment.MiddleCenter;
30                         Controls.Add (label);
31                 }
32
33                 private void InfoUpdaterAllow ()
34                 {
35                         string s = String.Format ("SecurityManager.SecurityEnabled: {0}{1}{2}{1}",
36                                 SecurityManager.SecurityEnabled, Environment.NewLine, DateTime.Now.ToLongTimeString ());
37                         try {
38                                 s += String.Format ("Wake up {0}!", Environment.UserName);
39                         }
40                         catch (SecurityException) {
41                                 s += "SecurityException";
42                         }
43                         label.Text = s;
44                         label.Refresh ();
45
46                         if (debug)
47                                 Console.WriteLine ("Delegate: {0}\n{1}", Thread.CurrentThread.Name, new StackTrace ().ToString ());
48                 }
49
50                 private delegate void updater ();
51
52                 private void InfoUpdaterDeny ()
53                 {
54                         InfoUpdaterAllow ();
55                 }
56
57                 private void UpdateLabelAllow ()
58                 {
59                         if (debug)
60                                 Console.WriteLine ("Allow: {0}", Thread.CurrentThread.Name);
61
62                         while (counter++ < 10) {
63                                 lock (this) {
64                                         label.BeginInvoke (new updater (InfoUpdaterAllow));
65                                 }
66                                 Thread.Sleep (500);
67                         }
68
69                         if (debug)
70                                 Console.WriteLine ("Application.Exit ();");
71                         Application.Exit ();
72                 }
73
74                 [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
75                 private void UpdateLabelDeny ()
76                 {
77                         if (debug)
78                                 Console.WriteLine ("Deny: {0}", Thread.CurrentThread.Name);
79
80                         while (counter++ < 10) {
81                                 lock (this) {
82                                         label.BeginInvoke (new updater (InfoUpdaterDeny));
83                                 }
84                                 Thread.Sleep (500);
85                         }
86
87                         if (debug)
88                                 Console.WriteLine ("Application.Exit ();");
89                         Application.Exit ();
90                 }
91
92                 static bool debug = false;
93                 static int counter = 0;
94
95                 public static void Main (string[] args)
96                 {
97                         Thread.CurrentThread.Name = "Main";
98                         BeginInvokeDemo demo = new BeginInvokeDemo ();
99                         demo.CreateHandle ();
100
101                         ThreadStart thread_start = new ThreadStart (demo.UpdateLabelDeny);
102                         if (args.Length > 0) {
103                                 debug = true;
104                                 if (args [0] == "allow") {
105                                         thread_start = new ThreadStart (demo.UpdateLabelAllow);
106                                 }
107                         }
108                         Thread worker = new Thread (thread_start);
109                         worker.Name = "Updater";
110                         worker.IsBackground = true;
111                         worker.Start ();
112
113                         Application.Run (demo);
114                 }
115         }
116 }