I want to use MediaElement to play music, and when music played to some position, do some action. The code is like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
game_pose_poller.RunWorkerAsync(); // game_pose_poller is a BackgroundWorker object
button1.IsEnabled = false;
}
private void game_pose_poller_DoWork(object sender, DoWorkEventArgs e)
{
while(true)
{
if (mediaElement1.Position >= sometime)
{
// do something
However I found the program did nothing at all. When debugging I found that mediaElement1.Position is always zero. Why is it always zero even after Play() called? mediaElement1.Source is an mp3 file which included as resource in project, and LoadedBehavior is Manual(or the Play() raise exception).
I've made something similar some times ago using a DispatchTimer instead of a BackgroundWorker. This is how I've got it to work:
private DispatcherTimer positionTimer;
private void button1_Click(object sender, RoutedEventArgs e)
{
// Create a timer that will check your condition (every second for example)
positionTimer= new DispatcherTimer();
positionTimer.Interval = TimeSpan.FromSeconds(1);
positionTimer.Tick += new EventHandler(positionTimerTick);
positionTimer.Start();
mediaElement1.Play();
button1.IsEnabled = false;
}
void positionTimerTick(object sender, EventArgs e)
{
if (mediaElement1.Position.TotalSeconds >= sometime)
{
// do something
}
}
Related
I think this should be working but I do not know why I try and do anything to set the current position or return the current position of a playing audio using the WMPLib inside of VS 2019 here is my code (sorry spaghetti) but basically I'm trying to make a pause function in my windows form thing
PictureOfWindowsFormApp
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(paused);
playsound();
if(paused == true)
{
wmpPlayer.controls.currentPosition = currenttime;
paused = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
double currenttime = wmpPlayer.controls.currentPosition;
wmpPlayer.controls.stop();
paused = true;
}
}
}
I've been looking for hours for different methods seeing if there were current position changes or some shit like that
Instead of trying to keep the currentPosition and setting it back, you can use the Pause() and Play() functions. Using Play(), when it is paused, will just resume where it was currently at.
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine(paused);
playsound();
wmpPlayer.controls.play();
}
private void button3_Click(object sender, EventArgs e)
{
wmpPlayer.controls.pause();
}
How to use timers to trigger click button event every 3 seconds?
I'm trying to rotate 2 pictures in pictureboxes by triggering the rotate button automaticly using timer but it seems doesnt works. I never used timer before so this is my first time. Anyone know whats wrong with my code or any other code suggestion for it? Thanks
Code I'm using
private void timer1_Tick(object sender, EventArgs e)
{
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
private void timerStartButton_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timerStopButton_Click(object sender, EventArgs e)
{
timer1.Stop();
}
It's even possible (and more simple) with tasks
public partial class Form1 : Form
{
// variable to keep track if the timer is running.
private bool _timerRunning;
public Form1()
{
InitializeComponent();
}
private async Task StartTimer()
{
// set it to true
_timerRunning = true;
while (_timerRunning)
{
// call the rotateRightButton_Click (what you want)
rotateRightButton_Click(this, EventArgs.Empty);
pictureBox1.Refresh();
pictureBox2.Refresh();
// wait for 3 seconds (but don't block the GUI thread)
await Task.Delay(3000);
}
}
private void rotateRightButton_Click(Form1 form1, EventArgs empty)
{
// do your thing
}
private async void buttonStart_Click(object sender, EventArgs e)
{
// if it's already started, don't start it again.
if (_timerRunning)
return;
// start it.
await StartTimer();
}
private void buttonStop_Click(object sender, EventArgs e)
{
// stop it.
_timerRunning = false;
}
}
timer1.Interval = 3000; // set interval to 3 seconds and then call Time Elapsed event
timer1.Elapsed += Time_Elapsed;
//Event
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
// will be triggered in every 3 seconds
rotateRightButton_Click(null, null);
pictureBox1.Refresh();
pictureBox2.Refresh();
}
Hope this helps!
Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}
By following code(the interval of timer2 is 1000)
private void timer1_Tick(object sender, EventArgs e) {
timer7.Enabled=false;
timer8.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
we can not create a delay in a for-loop
for(int i=1; i<=Max_Step; i++) {
// my code...
// I want delay here:
timer1.Interval=60000;
timer1.Enabled=true;
timer2.Enabled=true;
// Thread.Sleep(60000); // makes no change even if uncommenting
}
Whether I uncomment the line Thread.Sleep(60000); or not, we see nothing changed with lblTimer_Value_InBuildings in timer2_Tick.
Would you please give me a solution(with or without timers)?
Your timer is your loop, you don't need a for loop. You just keep track of your loop variables outside of the function calls. I would recommend wrapping all of this functionality into a class, to keep it separate from your GUI code.
private int loopVar = 0;
public void Form_Load()
{
// Start 100ms after form load.
timer1.Interval = 100;
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
// My Code Here
loopVar++;
if (loopVar < Max_Step)
{
// Come back to the _tick after 60 seconds.
timer1.Interval = 60000;
timer1.Enabled = true;
}
}
When you do Thread.Sleep(60000), you are telling the UI thread to sleep for 60 seconds. What this also does is prevent the execution of the timer, because the UI thread is hung up sleeping instead of processing events such as the timer sleep.
The code totally made me feel boring
methods:
private void timer1_Tick(object sender, EventArgs e) {
timer2.Enabled=false;
timer1.Enabled=false;
lblTimer_Value_InBuildings.Text="0";
}
private void timer2_Tick(object sender, EventArgs e) {
lblTimer_Value_InBuildings.Text=(int.Parse(lblTimer_Value_InBuildings.Text)+1).ToString();
}
private void timer3_Tick(object sender, EventArgs e) {
if(0!=(int)timer3.Tag) {
// your code goes here and peformed per step
timer1.Enabled=true;
timer2.Enabled=true;
}
timer3.Tag=(1+(int)timer3.Tag)%Max_Step;
}
initials:
var delayedInterval=60000;
timer1.Interval=60000;
timer2.Interval=1000;
timer3.Interval=delayedInterval+timer1.Interval;
lblTimer_Value_InBuildings.Text="0";
timer3.Tag=1;
timer3.Enabled=true;
Your original code never makes timer1 and timer2 stopped, so I think you should correct timer7 and timer8 to timer1 and timer2, otherwise they don't make sense here.
I would like to delay an action by several seconds after a mouse pointer has entered and remained for period of time in a winform graphics rectangle. What would be a way to do this?
Thanks
c#, .net 2.0, winform
private Timer timer;
private void rect_MouseEnter(object sender, EventArgs e)
{
timer = new Timer();
timer.Interval = 3000;
timer.Start();
timer.Tick += new EventHandler(t_Tick);
}
private void rect_MouseLeave(object sender, EventArgs e)
{
timer.Dispose();
}
void t_Tick(object sender, EventArgs e)
{
timer.Dispose();
MessageBox.Show(#"It has been over for 3 seconds");
}
Something such as:
static void MouseEnteredYourRectangleEvent(object sender, MouseEventArgs e)
{
Timer delayTimer = new Timer();
delayTimer.Interval = 2000; // 2000msec = 2 seconds
delayTimer.Tick += new ElapsedEventHandler(delayTimer_Elapsed);
}
static void delayTimer_Elapsed(object sender, EventArgs e)
{
if(MouseInRectangle())
DoSomething();
((Timer)sender).Dispose();
}
Probably could be done more efficiently, but should work :D
Two ways to set up MouseInRectangle -> one is to make it get the current mouse coordinates and the position of the control and see if it's in it, another way would be a variable which you would set to false on control.mouse_leave.
Try using the Timer control (System.Windows.Forms.Timer).
Please notice System.Windows.Forms.Timer is not Exact and you cannot rely it will act on exactly the interval given.
it would be prefeable to use System.Times.timer and use the Invoke action to return to the GUI thread.