I wanted to test FiddlerCore.
Found this -> http://www.c-sharpcorner.com/UploadFile/d9e6f2/capturing-http-traffic-in-C-Sharp/
I wrote it but for me it not capturing.
My code:
delegate void UpdateUI();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Fiddler.FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
Fiddler.FiddlerApplication.Startup(0, FiddlerCoreStartupFlags.Default);
}
void FiddlerApplication_AfterSessionComplete(Fiddler.Session oSession)
{
listBox1.Invoke(new UpdateUI(() =>
{
listBox1.Items.Add(oSession.url);
}));
}
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
Fiddler.FiddlerApplication.Shutdown();
}
It works for me. Have you wired Form Load and Form Closing events to the form? Can you please provide source of InitializeComponent() method.
I suspect that you are missing this:
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
Btw, what OS are you using?
Related
I want to change the text in a textbox of a Windows Form with different error messages. These error messages are set to an output string using the same method, but i can't pass the string as a parameter.
Here's how i call a new backgroundworker to safely change the text in the textbox:
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync(argument: error));
Then i try to invoke the call:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
string output = e.Argument.ToString();
object[] par = new object[] { output };
Delegate del = new DELEGATE(changeErrortext);
this.Invoke(del,par);
}
private void changeErrorText()
{
textBoxError.Text = output.ToString();
}
I think i've got to assign the output in the object to the one in the changeErrorText, but i don't really know how to do it.
I've tried different methods, but none have worked. I'm new to C#, so tell me if and where i've messed up.
Instantiate the necessary event handlers
Place eventhandlers in the constructor of the form. For example:
public Form1()
{
InitializeComponent(); // Standard initialization component method...
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
Using the DoWork AND the RunWorkerCompleted events
The way you used 'worker.DoWork += worker_DoWork;' should be expanded with the "RunWorkerCompleted" event. The DoWork event for the operation and the RunWorkerCompleted to handle the completed operation. This event is also usefull when you want to use cancellation or exception handling. This also gets called automatically, so there is no need for a dedicated "changeErrorText"-method. Example code:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = e.Result.ToString();
}
You don't have to use a Delegate to pass parameters
The parameters can be passed the way you wrote the RunWorkAsync. The only thing you might want to do is placing that method in a (click) event. For example:
private void button1_Click(object sender, EventArgs e)
{
int value = 123; // example input
backgroundWorker1.RunWorkerAsync(argument: value);
}
The full code doc:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.DoWork += backgroundWorker1_DoWork;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
private void button1_Click(object sender, EventArgs e)
{
int value = 123;
backgroundWorker1.RunWorkerAsync(argument: value);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string output = e.Argument.ToString();
e.Result = output;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
label1.Text = e.Result.ToString();
}
}
I hope i've helped you with the above explanations.
Sources
Sending Arguments To Background Worker?;
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=netcore-3.1
I have coded a form where a panel moves when a button is clicked to the height of the clicked button. However, I want to make the panel move slowly instead of instantly.
This is the code I have used:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MovePanel(btn1);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void MovePanel(Control c)
{
Panel.Height = c.Height;
Panel.Top = c.Top;
}
private void btn1_Click(object sender, EventArgs e)
{
MovePanel(btn1);
}
private void btn2_Click(object sender, EventArgs e)
{
MovePanel(btn2);
}
private void btn3_Click(object sender, EventArgs e)
{
MovePanel(btn3);
}
}
I would really like to see a timer-based solution from scratch.
In the meantime, you might check out something like dot-net-transitions, available on NuGet.
using Transitions;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btn1.Click += btn_Click;
btn2.Click += btn_Click;
btn3.Click += btn_Click;
MovePanel(btn1);
}
private void MovePanel(Control c)
{
var t = new Transition(new TransitionType_Linear(500));
t.add(Panel, "Height", c.Height);
t.add(Panel, "Top", c.Top);
t.run();
}
private void btn_Click(object sender, EventArgs e)
{
MovePanel(sender as Control);
}
}
I am currently creating a Windows Form Application and I am wanting to use a BackgroundWorker. I have created a very simple example which works perfectly:
public partial class Form1 : Form
{
private BackgroundWorker bgw = new BackgroundWorker();
public Form1()
{
InitializeComponent();
bgw.WorkerReportsProgress = true;
bgw.DoWork += new DoWorkEventHandler(DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
}
private void button1_Click(object sender, EventArgs e)
{
bgw.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10; i++)
{
bgw.ReportProgress(i * 10, i.ToString());
Thread.Sleep(1000);
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label1.Text = string.Format("{0}% : Message = '{1}'", e.ProgressPercentage, e.UserState.ToString());
}
private void Completed(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Completed");
}
}
Now, when I move the same code to my current application it does not fire. The only difference is that instead of running the code at the Form level, I am attempting to run it inside a custom User Control. As such:
public partial class LobbyForm : UserControl
{
private BackgroundWorker bgw = new BackgroundWorker();
public LobbyForm()
{
InitializeComponent();
bgw.WorkerReportsProgress = true;
bgw.DoWork += new DoWorkEventHandler(DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
}
public LobbyForm(List<TaskFile> tasks)
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bgw.RunWorkerAsync();
}
private void DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 10; i++)
{
bgw.ReportProgress(i * 10, i.ToString());
Thread.Sleep(1000);
}
}
private void ProgressChanged(object sender, ProgressChangedEventArgs e)
{
label5.Text = string.Format("{0}% : Message = '{1}'", e.ProgressPercentage, e.UserState.ToString());
}
private void Completed(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Completed");
}
}
Any thoughts on if I am missing something? Perhaps something I am misunderstanding with attempting to run this from a User Control?
I just copied your code and tested it and it worked perfectly if you drag-drop the user control using the designer.
However, if you create he control at runtime and add it to your form, make sure you're using the correct constructor.
LobbyForm lf = new LobbyForm();
this runs this constructor:
public LobbyForm()
{
InitializeComponent();
bgw.WorkerReportsProgress = true;
bgw.DoWork += new DoWorkEventHandler(DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
}
and not
LobbyForm lf = new LobbyForm(tasks);
which runs this constructor (that doesn't hook up events):
public LobbyForm(List<string> tasks)
{
InitializeComponent();
}
Solution (Call the default constructor from the second one)
public LobbyForm(List<string> tasks) : this()
{
//InitializeComponent();
}
I have a Form where one can Sign In and it could take a while till the data gets loaded into the Form. So I wanted to create a seperate Form (loadScreen.cs) with a Progress Bar when the Form is loading. I tried this in the loadScreen.cs Form:
private void Form1_Load(object sender, EventArgs e)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged +=
new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
int percentFinished = (int)e.Argument;
while (!worker.CancellationPending && percentFinished < 100)
{
percentFinished++;
worker.ReportProgress(percentFinished);
System.Threading.Thread.Sleep(50);
}
e.Result = percentFinished;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Close();
}
I've read that the worker_DoWork method should have the code which takes longer to load. I don't know how to handle this since my button is in Form1. When it's clicked then I go to another class with
private void signIn_Click(object sender, EventArgs e)
{
var logIn = new LogIn(this);
logIn.checkUserInput(this);
}
and there I execute the operations which load certain things. How to connect everything? I need help!
I'm actually in the process of creating a general-purpose dialogue for this sort of thing. It's not going to be ready in time to be of use to you but I would suggest that you go along similar lines. Create your "Loading" dialogue so that it accepts a delegate and invokes it in the DoWork event handler. The main form can then contain a method that does the work and you can pass a delegate for that method to the dialogue. I'll post a very basic example.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private DataTable table;
private void button1_Click(object sender, EventArgs e)
{
var work = new Action(GetData);
using (var f2 = new Form2(work))
{
f2.ShowDialog();
this.dataGridView1.DataSource = this.table;
}
}
private void GetData()
{
this.table = new DataTable();
using (var adapter = new SqlDataAdapter("SELECT * FROM MyTable", "connectionstring here"))
{
adapter.Fill(table);
}
}
}
public partial class Form2 : Form
{
private Action work;
public Form2(Action work)
{
InitializeComponent();
this.work = work;
}
private void Form2_Load(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.work();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Note that there's no real way to measure progress when using a data adapter so you could only really display a marquee progress bar in this case.
Simply put, how can I subscribe to and handle the following event?
System.Windows.Forms.Application.Exit(new System.ComponentModel.CancelEventArgs(true));
because apparently this is not going to give me my CancelEventArgs:
Application.ApplicationExit += new EventHandler(ApplicationExitHandler);
private void ApplicationExitHandler(Object sender, EventArgs e)
{
...
}
Try to put Event inside the Form1.Designer.cs
private void InitializeComponent()
{
//.......... UI iniatilization
System.Windows.Forms.Application.ApplicationExit +=new System.EventHandler(ApplicationExitHandler);
}
In Form1.cs
private void ApplicationExitHandler(Object sender, EventArgs e)
{
//.............
}