private void button2_Click(object sender, EventArgs e) // запуск таймера класс BackgroundWorker
{
// создаем новый экземпляр BackgroundWorker
bw = new BackgroundWorker();
// остальной код
key = true;
// ...
}
EventHandlerList events = (EventHandlerList)typeof(Component).GetProperty
(
"Events",
BindingFlags.NonPublic | BindingFlags.Instance
).GetValue(bw, null);
var k = typeof(BackgroundWorker).GetField
(
"doWorkKey",
BindingFlags.NonPublic | BindingFlags.Static
).GetValue(null);
var handlers = events[k];
Console.WriteLine
(
"Обработчиков {0}",
handlers.GetInvocationList().Length
);
bool key;
Timer time; /*вынес класс*/
volatile int i; /*вынес i и пометил как volotile, но если у вас Intel и один поток, то это не обязательно*/
BackgroundWorker bw = new BackgroundWorker();
private void button2_Click(object sender, EventArgs e) // запуск таймера класс BackgroundWorker
{
bw.DoWork += (o, eo) =>
{
for (;;)
{
i = time.Time(i);
Thread.Sleep(1000);
// https://msdn.microsoft.com/ru-ru/library/zyzhdc6b.aspx
Invoke(new Action(() => {
Text = "Таймер. Время: " + i;
}));
}
};
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<BitmapImage x:Key="MyImageSource" UriSource="H:\Temp\WpfApplication1\WpfApplication1\bin\Debug\icon1.png" />
</Application.Resources>
</Application>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Image Name="Fast" Width="15" Height="15" HorizontalAlignment="Stretch" Margin="0,0,170,0" Source="{DynamicResource MyImageSource}"/>
<Button Name="button" Content="Button" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
</Grid>
</Window>
using System;
using System.Windows;
using System.Windows.Media.Imaging;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources["MyImageSource"] =
new BitmapImage(new Uri(@"H:\Temp\WpfApplication1\WpfApplication1\bin\Debug\icon2.png"));
}
}
}
this.IsEnabled="False";
var bw = new BackgroundWorker();
bw.DoWork += (o, eo) =>
{
Directory.Delete("Путь каталога", true);
};
bw.RunWorkerCompleted += (o, eo) =>
{
IsEnabled = true;
Mouse.OverrideCursor = Cursors.Arrow;
};
IsEnabled = false;
Mouse.OverrideCursor = Cursors.Wait;
bw.RunWorkerAsync();
private readonly Window1 window1;
public Window2(Window1 window1)
{
InitializeComponent();
this.window1 = window1;
}
Window2 open = new Window2(this);
window1.Focus();
<MenuItem Name="Setting" Header="Настройки">
<MenuItem.Style>
<Style TargetType="MenuItem" BasedOn="{StaticResource {x:Type MenuItem}}">
<Setter Property="Foreground" Value="Lime"></Setter>
</Style>
</MenuItem.Style>
<MenuItem Header="Свойства"/>
<MenuItem Header="О программе"/>
<MenuItem Header="Выход"/>
</MenuItem>
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
<Border Name="Border" >
<Grid>
<ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
<Popup Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
<Border Name="SubmenuBorder" SnapsToDevicePixels="True" Background="Transparent">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
</Border>
</Popup>
</Grid>
</Border>
</ControlTemplate>