News:
|
/// <summary>
/// Forces the UI to update itself.
/// </summary>
///
/// <remarks>
/// This is an analogy to Application.DoEvents() in Windows.Forms.
/// </remarks>
///
protected static void ForceUIUpdate()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(
DispatcherPriority.Background,
new DispatcherOperationCallback(f =>
{
((DispatcherFrame)f).Continue = false;
return null;
}),
frame);
Dispatcher.PushFrame(frame);
}
|
Wie komme ich den zu der DoEvents-Methode?
Wenn ich die einfügen will, dann gibts die nicht. Application ist bekannt, aber nach dem "." kommt dann im Auswahlfeld nichts mit DoEvents. Danke – T.Mann11 24.02.2012
|
||
Ok, 10 sek später gemerkt, dass die Methode in der Form aufgerufen werden muss...
Funktioniert einwandfrei. Thx – T.Mann11 24.02.2012
|
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Encapsulate ShowPopup method into thread start delegate
var work = new ThreadStart(ShowPopup);
// define the thread
var myThread = new Thread(work);
// run the thread
myThread.Start();
// Wait one second before showing message box
Thread.Sleep(1000);
MessageBox.Show("End of code in GUI Thread has been reached.");
}
/// <summary>
/// Shows the popup for 5 seconds
/// </summary>
/// <returns></returns>
private void ShowPopup()
{
frmPopup.ShowAutoClose(5000);
}
}
public partial class frmPopup : Form
{
private Timer closeTimer;
public frmPopup()
{
InitializeComponent();
}
public frmPopup(int closeInterval)
{
// do not forget this line!
InitializeComponent();
// start a internal timer that close your popup on tick
closeTimer = new Timer { Interval = closeInterval };
closeTimer.Tick += closeTimer_Tick;
closeTimer.Start();
}
private void closeTimer_Tick(object sender, EventArgs e)
{
// this gets executed every timer tick, we want to execute this once
// to close the form
closeTimer.Stop();
closeTimer.Dispose();
Close();
}
internal static void ShowAutoClose(int closeInterval)
{
var popupForm = new frmPopup(closeInterval);
popupForm.ShowDialog();
}
}
|
public class Aufruf
{
public int StarteAlles
{
WindowThread threadObject = new WindowThread();
Thread mythread = new Thread(threadObject.ShowWindow);
mythread.Start();
for (int i = 1; i < 40; i++)
{
Thread.Sleep(50);
}
threadObject.CloseWindow();
return 1;
}
class WindowThread
{
private int StopIt = 0;
public void ShowWindow()
{
WindowTimeout ttt = new WindowTimeout();
ttt.ShowInTaskbar = true;
ttt.TopMost = true;
ttt.Focus();
ttt.BringToFront();
ttt.Activate();
ttt.Show();
while (StopIt == 0)
Thread.Sleep(100);
}
public void CloseWindow()
{
StopIt = 1;
//ttt.Hide();
//ttt.Close();
}
}
|
public int WindowTest(int Timeout_s)
{
WindowTimeout ttt = new WindowTimeout();
ttt.ShowInTaskbar = true;
ttt.TopMost = true;
ttt.Focus();
ttt.BringToFront();
ttt.Activate();
ttt.Show();
ttt.Invalidate();
ttt.UpdateForm();
for (int i = 1; i < 40; i++)
{
Thread.Sleep(50);
}
ttt.Hide();
ttt.Close();
return 1;
}
public void UpdateForm()
{
textBox_header.Text = "Please wait...";
this.Invalidate();
this.BringToFront();
this.Activate();
timer1.Interval = 50;
timer1.Start();
Application.DoEvents(); //Daurch wird die Form aktualisiert
}
private void timer1_Tick(object sender, EventArgs e)
{
textBox_header.Text = "timertick läuft";
MessageBox.Show("test");
Application.DoEvents();
}
|
Es ist wohl so, dass durch ttt.Show() die Eventstrukturen nicht gestartet/erstellt werden. Wenn ich ShowDiaolog verwende und den timer1.Start() in den Konstruktur verschiebe dann läuft der timertick.
Allerdings tut nantürlich der ShowDialog das Code-fortsetzen verhindern... Ich brauche also einen Befehl um die Eventstrukturen manuell zu starten bei Verwendung des Show(). – T.Mann11 24.02.2012
|
||
Schau dir mein Beispiel an, es funktioniert ohne Probleme. Ich weiß gar nicht was ihr da für ein Heckmeck mit .DoEvents() veranstaltet ... ;)
– Nicolai Schönberg 24.02.2012
|
||
Ich nehme an, dass dein Beispiel auch nicht funktionieren wird da du auch "ShowDialog" verwendest. Wenn das aufgerufen wird dann bleibt der Code stehen bis er wieder zu ist. Deshalb wollte ich nur "Show()" verwenden.
– T.Mann11 24.02.2012
|
||
Es wäre aber nett, wenn Du die Lösung auch als richtig markieren würdest.