My C# application starts minimised and shows the video stream of a webcam for 5 seconds when a certain event occurs. After that it is minimised again. This works well, but only from the 2nd event. On the first call, only an empty form is displayed.
Is there an event in which I can add a videoSourcePlayer1.Show() so that something is also displayed in the 1 event? It does nothing in the Form1_Shown event.
My code:
...
private readonly UdpClient udp = new UdpClient(port);
public IntPtr myHandle;
public void wait(int milliseconds)
{
...
}
private void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Parse(ip), port);
byte[] bytes = udp.EndReceive(ar, ref ip);
string message = Encoding.ASCII.GetString(bytes);
if (message == "ON")
{
ShowWindow(myHandle, SW_RESTORE);
Thread.Sleep(50);
SetForegroundWindow(myHandle);
wait(5000);
ShowWindow(myHandle, SW_SHOWMINIMIZED);
}
StartListening();
}
private void Form1_Load(object sender, EventArgs e)
{
myHandle = this.Handle;
videoSourcePlayer1.VideoSource = new MJPEGStream("http://ip/video.mjpg");
videoSourcePlayer1.Start();
this.WindowState = FormWindowState.Minimized;
StartListening();
}
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
notifyIcon1.Visible = true;
videoSourcePlayer1.Hide();
}
if (this.WindowState == FormWindowState.Normal)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
videoSourcePlayer1.Show();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
videoSourcePlayer1.Stop();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
}
}
What can I do?
Got it. I changed the execution order in Form1_Resize and added a delay.
if (this.WindowState == FormWindowState.Normal)
{
videoSourcePlayer1.Show();
Thread.Sleep(250);
this.WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false;
Show();
}
Add this method to continue the stream
private void bntContinue_Click(object sender, EventArgs e)
{
videoSourcePlayer1.Continue();
}
Related
I have a notify icon in my Winforms form and it seems that when any kind of event happens the tray icon is duplicated.
I have debugged one of the issues, being that it is duplicated when the dialog box is closed after using it.
It happens in debug and when released.
The other issue it with a timer that runs method.
I cannot see why this happens. My timer ran 60 times last night and each time it has four methods to run and there were hundreds of icons in the tray.
My code is as follows:
public Form1()
{
InitializeComponent();
notifyIcon1.BalloonTipText = "Mappi CSV Manager is running.";
notifyIcon1.BalloonTipTitle = "Mappi CSV Manager";
notifyIcon1.Text = "Mappi CSV Manager";
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowIcon = false;
ShowInTaskbar = false;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(1000);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowInTaskbar = true;
notifyIcon1.Visible = false;
WindowState = FormWindowState.Normal;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// Call Dispose to remove the icon out of notification area of Taskbar.
notifyIcon1.Dispose();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (CloseCancel() == false)
{
e.Cancel = true;
};
}
//When closing the form
public static bool CloseCancel()
{
const string message = "If you close the program, no files will be generated!";
const string caption = "Stop!";
var result = MessageBox.Show(message, caption,
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
return true;
else
return false;
}
//Set new value for timer
private void UdTimerValue_ValueChanged(object sender, EventArgs e)
{
timer1.Interval = Convert.ToInt32(udTimerValue.Value) * 60000;
}
//Start generating CSV's
private void Timer1_Tick(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (AutoGenerateEnabled)
{
richLogWindow.AppendText("CSV Created at: " + DateTime.Now + "\r\n");
var startdate = "";
if(DateTime.Now.Hour == 1)
{
richLogWindow.Clear();
startdate = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
else
{
startdate = DateTime.Today.ToString("yyyy-MM-dd");
CSVGenerator.GenerateCSV(startdate, this);
}
}
else
{
return;
}
}
}
Why is this code producing another tray icon every time a button is clicked or an event happens.
TIA
I found the error. I have put RichTextBoxAppend.AddNewText("test me", new Form1()); the new form was created each time a process was run. I am an idiot!
How to program wait Custom MessageBox OK,Cancel,Yes,No responses? i have been using custom messageBox because of my natural language because messagebox has only english ok,yes,no i have found this one: Customizing MessageBox on Windows Phone 7
private MessageBoxService CustomMsgBox= new MessageBoxService();
public async void Handle(RecordRequest message)
{
AutoResetEvent waitHandle = new AutoResetEvent(false);
Dispatcher.BeginInvoke(() =>
{
CustomMsgBox.Closed += CustomMsgBox_Closed;
CustomMsgBox.Closed += (s, e) => waitHandle.Set();
CustomMsgBox.Show(
"Login Please! ",
"Kayıt",
MessageBoxServiceButton.OKCancel);
});
waitHandle.WaitOne();
// Must Wait MessageBox Ok or Cancel like Normal MessageBox!!!
Login.IsOpen= true;
// do something ...
}
void CustomMsgBox_Closed(object sender, EventArgs e)
{
this.CustomMsgBox.Closed -= this.CustomMsgBox_Closed;
string rst = this.CustomMsgBox.Result.ToString();
}
Normally wp8 messagebox is locked while clicking OK-Cancel. But My Costom MessageBox is not waiting. How to lock my CustomMsgBox until clicking ok-Cancel.
SAMPLE USAGE:
private PixelLab.Common.MessageBoxService _service = new PixelLab.Common.MessageBoxService();
private void Button_Click(object sender, RoutedEventArgs e)
{
this._service.Closed += this.MessageBoxService_Closed;
this._service.Show(
"test",
"alo!",
MessageBoxServiceButton.OKCancel);
//if (_service.IsOpen == true)
//{
// MessageBoxResult rslt = _service.Result;
// if (rslt == MessageBoxResult.No)
// {
// MessageBox.Show("işlem iptal edildi");
// }
// else
// {
// MessageBox.Show("işlem onaylandı");
// }
//}
}
private void MessageBoxService_Closed(object sender, EventArgs e)
{
this._service.Closed -= this.MessageBoxService_Closed;
string rst = this._service.Result.ToString();
}
I have to create an aplication which reads the registers from a PLC every 200ms. To do that I am using something like:
SerialPort port = new SerialPort();
private void Form1_Load(object sender, EventArgs e)
{
port.ReceivedBytesThreshold = 21;
timer1.Interval = 200;
timer1.Start();
}
private void ProcessTimer_Tick(object sender, EventArgs e)
{
if (dataReceived)
{
dataReceived = false;
port.Read(buffer_rx, 0, 21);
TranslateValues();
}
if (port.IsOpen && connectected)
{
// sends command for a new reading
port.Write(buffer_tx, 0, length);
}
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Invoke(new EventHandler(DoUpdate));
}
void DoUpdate(object sender, EventArgs e)
{
dataReceived = true;
}
The problem is that my application freezes from time to time. When I debug this, the debugger points me to port.Read(), but doesn't throw any exceptions.
I have tried using a try-catch block but nothing was caught, and also made time1.Interval = 2000, but it didn't work either.
My question is, why is my application freezing at port.Read(), but not catching any exceptions? How can I fix this?
I create a new MdiChild from MainForm using this method:
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Why when i close my child, using in chld form "this.close()" using that method i cant open it anymore?
there i call close();
private void cancelLogInButton_Click(object sender, EventArgs e)
{
this.MdiParent.Activate();
if(this.MdiParent!=null)
((MainForm)this.MdiParent).LogInAsAdminMenuItem.Enabled = true;
this.Close();
}
by the way to make work that I asked before I hed to plase this.Close(); after all statements .
By closing the form you are not making adminForm instance to null (Which is what your if condition will check when you try to open it next time.)
On diposal of your form make adminForm = null and then your if condition will work next time.
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null)
{
adminForm = new AdminLogInForm(this);
adminForm.Disposed += new EventHandler(adminForm_Disposed); //Add Disposed EventHandler
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
void adminForm_Disposed(object sender, EventArgs e)
{
adminForm = null;
}
As Described by Marshal that the closing of a form makes it disposed you should add a condition for disposing as well like this
AdminLogInForm adminForm;
private void LogInAsAdminMenuItem_Click(object sender, EventArgs e)
{
if (adminForm == null || adminForm.IsDisposed)
{
adminForm = new AdminLogInForm();
adminForm.MdiParent = this;
adminForm.Show();
adminForm.Dock = DockStyle.Fill;
adminForm.BringToFront();
LogInAsAdminMenuItem.Enabled = false;
}
else
{
adminForm.Activate();
adminForm.BringToFront();
}
}
Or you can also create a function to use a form as mdi
like this
I have a NotifyIcon method, although I would like the timeout to happen, before disposing of the BaloonTip.
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(30000);
<wait until timeout occurs>
notifyIcon1.Dispose();
}
notifyIcon1.BalloonTipClosed += delegate {notifyIcon1.Dispose ();};
I would rather hide the NotifyIcon instead of recreating/disposing a new instance of it.
Try using a timer.
Should be something like...:
private Timer taskTimer;
private NotifyIcon notifyIcon1;
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(30000);
taskTimer = new Timer(TimerCallback, notifyIcon1, 30000, System.Threading.Timeout.Infinite);
}
and...
void TimerCallback(object notifyIcon1Obj)
{
lock (notifyIcon1Obj)
{
NotifyIcon notifyIcon1 = (NotifyIcon)notifyIcon1Obj;
notifyIcon1.dispose();
notifyIcon1 = null;
}
}
HTH