So i have form1
which has backgroundworker (dragged and dropped via design view).
I can get it to work in the places i need, however I need to call it from a public method.
In this public method
Utility.initpacks(object sender, EventArgs e,string formname)
SO my DoWork
is in Form1
.
I the public utility within the form do do a bunch of things, then THAT function needs to use the background worker inside Form1 again!
I could just copy the public method and put in the place of the method reference and all is well.. but that defeats the purpose of a public method doesn't it!?
Any ideas would be great thanks :)
EDIT:
SO my current setup (without a bunch of stuff not important):
public partial class frmimportinstitutions : Form
{
private void btnNext_Click(object sender, EventArgs e)
{
Utility.initpacks(sender, e, this.FindForm().Name);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Do stuff
}
}
public static class Utility
{
public static void initpacks(object sender, EventArgs e,string formname)
{
//I WANT TO USE THE BACKGROUND WORKER HERE
//Do a public method
//I want to stop the background worker here
}
}
Update (basd on comments):
Michael comments just mentioned to put the background worker starting in a public method:
public void startplash(string starttext)
{
if (!backgroundWorker1.IsBusy)
{
splashtext = starttext;
backgroundWorker1.RunWorkerAsync();
}
}
Now i want to call this method from the other method. In order to do this, the other method (init packs) needs to know where this method is doesnt it. EG.
form1.startsplash("hello world")
So now i just need to send Form1 info to init packs...
Would this be ok:
Initpacks(Form Owner)
{
Owner.startsplash("hello world")
}
Another update! Thanks for Michael we so far have this:
public static class Utility
{
public static void RunWorkerOfForm1()
{
var target = (Form1)Application.OpenForms.OfType<Form1>().FirstOrDefault();
target?.RunWorker();
}
}
Now I need to get this to work with different forms.. I havent tried the below but this is what i am going to try next.. correct me if i am wrong:
public static class Utility
{
public static void RunWorkerOfForm1(Form owner)
{
var target = (owner)Application.OpenForms.OfType<owner>().FirstOrDefault();
target?.RunWorker();
}
}
Final Answer (as per the ticked answer) - but using my code:
public partial class frmholidaypacks : Form, IWorker
{
private void btnextrapacks_Click(object sender, EventArgs e)
{
Utility.futurepacks<frmholidaypacks>(sender, e, pxid);
}
}
public interface IWorker
{
void startplash(string starttext);
}
public void startplash(string starttext)
{
if (!backgroundWorker1.IsBusy)
{
splashtext = starttext;
backgroundWorker1.RunWorkerAsync();
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//Doing work. Using Splashtext string.
}
public static void futurepacks<T>(object sender, EventArgs e, int pxid) where T : IWorker
{
var target = (T)Application.OpenForms.OfType<T>().FirstOrDefault();
target?.startplash("Creating future packs");
}
100% Credit goes to Michael for working on this with me!