powered by simpleCommunicator - 2.0.60     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как привильно генерировать события из потока?
4 сообщений из 4, страница 1 из 1
Как привильно генерировать события из потока?
    #32931582
ClonClona
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
есть основной поток программы, ему могут посылаться сообытия как из этого же самого потока, так и из другого потока.
в обработчике события находится функция работы с контролами, так вот когда событие приходит от потока вся главная форма падает
...
Рейтинг: 0 / 0
Как привильно генерировать события из потока?
    #32931604
кузя
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
...
Рейтинг: 0 / 0
Как привильно генерировать события из потока?
    #32931608
Alexey Kudinov
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
ClonClonaесть основной поток программы, ему могут посылаться сообытия как из этого же самого потока, так и из другого потока.
в обработчике события находится функция работы с контролами, так вот когда событие приходит от потока вся главная форма падает Да, интерфейс - он не ThreadSafe. П.э. так (взято из MS примера):
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
private delegate void MarshalEventDelegate( object sender, UpdaterActionEventArgs e );
		/// <summary>
		/// Event handler for Updater event.  This event is fired by the originating thread from "inside" the Updater.  While it is
		/// possible for this same thread to act on our UI, it is NOT a good thing to do--UI is not threadsafe.  
		/// Therefore here we marshal from the Eventing thread (belongs to Updater) to our window thread using the synchronous Invoke
		/// mechanism.
		/// </summary>
		/// <param name="sender">event sender in this case ApplicationUpdaterManager</param>
		/// <param name="e">the UpdaterActionEventArgs packaged by Updater, which gives us access to update information</param>
		private void OnUpdaterFilesValidated( object sender, UpdaterActionEventArgs e )
		{
			//  using the asynchronous "BeginInvoke".  
			//  we don't need/want to block here
			this.BeginInvoke( 
				new MarshalEventDelegate( this.OnUpdaterFilesValidatedHandler ),
				new object[] { sender, e } );
		}

		/// <summary>
		/// This handler gets fired by the Windows UI thread that is the main STA thread for THIS FORM.  It takes the same 
		/// arguments as the event handler below it--sender, e--and acts on them using the main thread NOT the eventing thread
		/// </summary>
		/// <param name="sender">marshalled reference to the original event's sender argument</param>
		/// <param name="e">marshalled reference to the original event's args</param>
		private void OnUpdaterFilesValidatedHandler( object sender, UpdaterActionEventArgs e )
		{
			lblStatus.Text += String.Format("FilesValidated successfully for application '{0}' ", e.ApplicationName) + Environment.NewLine ;
			
			//  ask user to use new app
			DialogResult dialog = MessageBox.Show( "Would you like to stop this application and open the new version?", "Open New Version?", MessageBoxButtons.YesNo );
			if( DialogResult.Yes == dialog )
			{
				StartNewVersion( e.ServerInformation );
			}
		}
Дело происходит в форме, т.е. this - это "основная" форма приложения, а OnUpdaterFilesValidated - обработчик события треда
...
Рейтинг: 0 / 0
Как привильно генерировать события из потока?
    #32931687
ClonClona
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Гость
Alexey Kudinov спасибо Вам огромное!!! вы действительно профессионал ;-)
блин если б не вы я бы вообще застрял на этом моменте надолго :-)
...
Рейтинг: 0 / 0
4 сообщений из 4, страница 1 из 1
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Как привильно генерировать события из потока?
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]