I cannot get the ui thread to update the ui while the file copy thread is running. My end goal is to have the animation continue to rotate until the large file copy finally completes to let the user know that the program is not frozen. It's a very simple server to server file copy program.
Can someone tell me what I'm doing wrong?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ResetProgress()
{
lblStep1.Image = null;
}
private void SetupProgress()
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}
private void fileCopy()
{
File.Copy("large file source", "large file destination", true);
}
private void Form1_Load(object sender, EventArgs e)
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}
private async void button1_Click(object sender, EventArgs e)
{
SetupProgress();
await Task.Run(() => fileCopy());
ResetProgress();
}
private void btnStop_Click(object sender, EventArgs e)
{
// unhandled currently
}
}
}
* Original version *
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Threading.Tasks;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Thread workItemsProducerThread;
private Thread workItemsCopyThread;
public Form1()
{
InitializeComponent();
}
private void ResetProgress()
{
lblStep1.Image = null;
}
private void SetupProgress()
{
this.BeginInvoke((MethodInvoker)delegate ()
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
});
}
private void fileCopy()
{
File.Copy("Large file source", "Large file destination", true);
this.BeginInvoke((MethodInvoker)delegate ()
{
MessageBox.Show("Done");
});
}
private void Form1_Load(object sender, EventArgs e)
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
}
private void btnStart_Click(object sender, EventArgs e)
{
this.workItemsProducerThread = new Thread(new ThreadStart(this.SetupProgress));
this.workItemsProducerThread.IsBackground = true;
this.workItemsProducerThread.Start();
this.SetupProgress();
this.workItemsCopyThread = new Thread(new ThreadStart(this.fileCopy));
this.workItemsCopyThread.IsBackground = true;
this.workItemsCopyThread.Start();
while (workItemsCopyThread.IsAlive)
{
Thread.Sleep(1000); // wait
}
MessageBox.Show("Done");
}
private void btnStop_Click(object sender, EventArgs e)
{
if (this.workItemsProducerThread != null)
{
this.workItemsProducerThread.Abort();
lblStep1.Image = global::animation1.Properties.Resources.animation;
}
}
private void btnTest_Click(object sender, EventArgs e)
{
fileCopy();
}
}
}
Don’t sleep in your click handler. That freezes the UI thread. Just let the clock handler exit. In your file copy thread, when the copy is don’t. Use Invoke(or BeginInvoke) to cause the done messagebox to pop up on the UI thread.
Try this oldy style
private void SetupProgress()
{
Invoke((MethodInvoker) delegate
{
lblStep1.Image = global::animation1.Properties.Resources.animation;
});
}
private Thread TDoSomeWork()
{
var t = new Thread(() => DoSomeWork());
t.Start();
return t;
}
TDoSomeWork();
Related
I am trying to write a code for taking fps at label while taking view from the camera.
So, I used task for that and I used timer but I got an error. I fixed the problem with using CheckForIllegalCrossThreadCalls = false; but as far as I know it is not an effective way.
I want to fix this with using invoke. How should I write the code properly?
Here is the code:
using System;
using System.Drawing;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace projectimg
{
public partial class Form1 : Form
{
Stopwatch watch = new Stopwatch();
System.Timers.Timer timerFPS;
public Form1()
{
InitializeComponent();
}
CameraConnection connnection;
private bool btn_GetviewWasClicked = false;
private void Form1_Load(object sender, EventArgs e)
{
btn_Getview.Click += Btn_Getview_Click;
timerFPS = new System.Timers.Timer(1000);
timerFPS.Elapsed += TimerFPS_Elapsed;
timerFPS.Start();
}
private void Btn_Getview_Click(object sender, EventArgs e)
{
btn_GetviewWasClicked = true;
Task.Run(() =>
{
connnection = new CameraConnection();
connnection.Connect();
});
CameraConnection.DataReceived += CameraConnection_DataReceived;
}
private void TimerFPS_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
txt_fps.Text = Convert.ToString(CameraConnection.count);
CameraConnection.count = 0;
}
}
}
I am new to C# and I want to create a windows forms application, which shows (it must be visible!) one window with some information and buttons and also it loads a page from internet (with selenium and phantom.js - though it is deprecated) every minute. I've written something like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
namespace WindowsFormsApplication1
{
public partial class Someclass : Form
{
private void label1_Click(object sender, EventArgs e)
{
}
private void Someclass_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Shows some text "Hello friend"
MessageBox.Show("Hello friend!");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public Someclass()
{
InitializeComponent();
while (!IsDisposed)
{
MessageBox.Show("Now the page will be downloaded");
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
using (var driver = new PhantomJSDriver(driverService))
{
driver.Navigate().GoToUrl("http://stackoverflow.com/");
MessageBox.Show("Here we are going to open StackOverflow");
var questions = driver.FindElements(By.ClassName("fs-display2"));
foreach (var question in questions)
{
// This will display some text from stackoverflow main page.
Console.WriteLine(question.Text);
question.Click();
MessageBox.Show("This is stackoverflow: " + question.Text);
}
MessageBox.Show("Here we go");
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
}
}
My problem is that if I use "while", my window does not appear (but the page from internet loads correctly - every 1 minute), and if I use "if" instead of "while", my window appears well, but, of cource, the page loading goes only one time. What can solve my problem?
It's a quite simple solution: your while loop is preventing your UI from Update. You have to bring your loop in an extra Thread, or just use a backgroundworker
Yes, friends, you were right. I have put (using panel of instruments-> Background Worker):
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
while (!IsDisposed)
{
......................
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
inside my public partial class someClass : Form after public Someclass()
and also added:
backgroundWorker1.RunWorkerAsync(2000);
inside my public Someclass()
So now the code looks like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
namespace WindowsFormsApplication1
{
public partial class Someclass : Form
{
private void label1_Click(object sender, EventArgs e)
{
}
private void Someclass_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Shows some text "Hello friend"
MessageBox.Show("Hello friend!");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello again", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public Someclass()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync(2000);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
while (!IsDisposed)
{
MessageBox.Show("Now the page will be downloaded");
var driverService = PhantomJSDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
using (var driver = new PhantomJSDriver(driverService))
{
driver.Navigate().GoToUrl("http://stackoverflow.com/");
MessageBox.Show("Here we are going to open StackOverflow");
var questions = driver.FindElements(By.ClassName("fs-display2"));
foreach (var question in questions)
{
// This will display some text from stackoverflow main page.
Console.WriteLine(question.Text);
question.Click();
MessageBox.Show("This is stackoverflow: " + question.Text);
}
MessageBox.Show("Here we go");
}
System.Threading.Thread.Sleep(60000); // delay in microseconds
}
}
}
and works fine.
I'm trying to make a windows form application in Visual Studio in C# and the purpose is to be able to communicate with an Arduino microcontroller. Right now I have 2 forms (Form1 and Form2) and I need to send and receive data from both windows to the microcontroller. I have defined a class SerialComms.cs in which there I start my serialPort and I have been able to send messages from both forms to the Arduino.
The problem is I don't know how to go about to receiving data from the Arduino as soon as the data is received. If I were to create the serialPort from one of the forms i would be able to create a function for DataReceived that will run anytime data is received but I don't know how to do that when I start my serialPort from a class. Any help?
Here is my code for both forms and my serialComms class:
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
connectToArduino();
}
void connectToArduino()
{
try { SerialComms.SerialPort.Close(); }
catch { }
getAvailablePorts();
for (int i = 0; i < COMcomboBox.Items.Count; i++)
{
string inputMsg = "None";
string commport = COMcomboBox.GetItemText(COMcomboBox.Items[i]);
//Wait for Arduino message sent
//If not receieved go to next port
SerialComms.SerialPort.PortName = commport;
SerialComms.SerialPort.BaudRate = 9600;
SerialComms.SerialPort.Open();
try { SerialComms.SerialPort.Open(); }
catch { }
int counter1 = 0;
while (counter1 < 100)
{
inputMsg = SerialComms.SerialPort.ReadLine();
try { inputMsg = SerialComms.SerialPort.ReadLine(); }
catch { }
counter1++;
if (inputMsg == "Arduino")
{
counter1 = 1000;
}
}
if (inputMsg != "Arduino")
{
try { SerialComms.SerialPort.Close(); }
catch { }
}
else
{
i = COMcomboBox.Items.Count;
SerialComms.SerialPort.WriteLine("Connection Established");
toolStripStatusLabel1.Text = "Connected";
break;
}
}
}
void getAvailablePorts()
{
COMcomboBox.Items.Clear();
String[] ports = SerialPort.GetPortNames();
COMcomboBox.Items.AddRange(ports);
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
}
private void button3_Click(object sender, EventArgs e) //Opens second form
{
Form2 settingsForm = new Form2();
settingsForm.ShowDialog();
}
}
}
Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public partial class Form2 : Form
{
public static string text = "Test text";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
button2.Text = text;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
SerialComms.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public static class SerialComms
{
//public static delegate void DataReceivedEventHandler(object sender, ReceivedEventArgs e);
//public static event DataReceivedEventHandler DataReceived;
private static SerialPort _serialPort = new SerialPort();
public static SerialPort SerialPort
{
get { return _serialPort; }
set { _serialPort = value; }
}
}
}
I use fallowing library to detect webcam for simple c# application. When it debug works fine and when I try again It give fallowing error.
Used reference
If somebody has any idea about it please help me to solve it.
First get this box and when click OK get fallowing error message.
an error occurred while capturing the video image. the video capture
will now be terminated. Object reference not set to an instance of an
object.
Form1.cs
using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using WinFormCharpWebCam;
namespace camTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
WebCam webcam;
private void button1_Click(object sender, EventArgs e)
{
webcam.Start();
}
private void button2_Click(object sender, EventArgs e)
{
webcam.Stop();
}
private void button3_Click(object sender, EventArgs e)
{
webcam.Continue();
}
private void Form1_Load(object sender, EventArgs e)
{
webcam = new WebCam();
webcam.InitializeWebCam(ref imgVideo);
}
}
}
Webcam.cs
using System;
using System.IO;
using System.Linq;
using System.Text;
using WebCam_Capture;
using System.Collections.Generic;
namespace WinFormCharpWebCam
{
//Design by Pongsakorn Poosankam
class WebCam
{
private WebCamCapture webcam;
private System.Windows.Forms.PictureBox _FrameImage;
private int FrameNumber = 30;
public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
{
webcam = new WebCamCapture();
webcam.FrameNumber = ((ulong)(0ul));
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
_FrameImage = ImageControl;
}
void webcam_ImageCaptured(object source, WebcamEventArgs e)
{
_FrameImage.Image = e.WebCamImage;
}
public void Start()
{
webcam.TimeToCapture_milliseconds = FrameNumber;
webcam.Start(0);
}
public void Stop()
{
webcam.Stop();
}
public void Continue()
{
// change the capture time frame
webcam.TimeToCapture_milliseconds = FrameNumber;
// resume the video capture from the stop
webcam.Start(this.webcam.FrameNumber);
}
public void ResolutionSetting()
{
webcam.Config();
}
public void AdvanceSetting()
{
webcam.Config2();
}
}
}
This one is giving me a hard time.
The thing is that I have a code that plays some notes in MIDI, and I wanted to be able to pause it, so I made a simple Form like this:
namespace Music
{
public partial class Form1 : Form
{
static BackgroundWorker _bw = new BackgroundWorker
{
WorkerSupportsCancellation = true
};
private void button1_Click(object sender, EventArgs e)
{
if (!Playing)
{
Playing = true;
_bw.DoWork += Start_Playing;
_bw.RunWorkerAsync("Hello to worker");
}
else
{
Playing = false;
_bw.CancelAsync();
}
}
static void Start_Playing(object sender, DoWorkEventArgs e)
{
//Plays some music
}
}
}
And when I click it starts playing, but no matter what I do, it can't stop. But the thing is that if I do the same thing in the console it works perfect.
Did I miss something?
How can I control a separate thread from the form?
This seems to work...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private BackgroundWorker _bw = new BackgroundWorker { WorkerSupportsCancellation = true,
WorkerReportsProgress = true};
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_bw.IsBusy)
{
_bw.CancelAsync();
}
else
{
_bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);
_bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
_bw.RunWorkerAsync();
}
}
void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox1.Text += (string)e.UserState;
}
void _bw_DoWork(object sender, DoWorkEventArgs e)
{
int count = 0;
while (!_bw.CancellationPending)
{
_bw.ReportProgress(0, string.Format("worker working {0}", count));
++count;
Thread.Sleep(2000);
}
}
}
}