Firing custom event in the UI thread - c#

The example below functions fine, but I want to have the Complete event fire its event handlers in the UI thread. I don't want HasCompleted() to have to worry about checking if it's on the UI thread or not. Calls to HasCompleted() should always be invoked on the UI thread. How do I do this?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var game = new Game();
game.Complete += HasCompleted;
game.Load();
}
private void HasCompleted()
{
if (label1.InvokeRequired)
{
label1.BeginInvoke(new Action(() => label1.Text = "complete"));
}
else
{
label1.Text = "complete";
}
}
}
public class Game
{
public Game()
{
}
public event MethodInvoker Complete;
public void Load()
{
var task = new Task(new Action(() =>
{
Thread.Sleep(500);
OnComplete();
}));
task.Start();
}
private void OnComplete()
{
if (Complete != null)
{
Complete();
}
}
}

Capture the current synchronization context when you create the Game object and use that to marshal the event to the context that was current when the object was first created:
public class Game
{
private SynchronizationContext context;
public Game()
{
context = SynchronizationContext.Current ??
new SynchronizationContext();
}
public MethodInvoker Complete;
public void Load()
{
//...
}
private void OnComplete()
{
if (Complete != null)
{
context.Post(_ => Complete(), null);
}
}
}

Related

How to update a Label on the UI thread/class within a selenium thread/class

So I have a problem on how to update a label on the UI thread from the selenium thread. As you see the selenium thread calls the "selenium" method that uses static methods(Login.UserLogin, Run.StartDriver) from other classes.
I can't seem to figure out how to change a label in this class from the login or run class. Sorry if the coding is amateur I just started learning c#.
public class Form1{
private void startThread()
{
if (seleniumThread == null)
{
stopThread = false;
seleniumThread = new Thread(() => selenium(userName, passWord,
cyclesWanted));
seleniumThread.Start();
}
}
private void selenium(string user, string pass, int cycles)
{
driver = new FirefoxDriver();
Login.UserLogin(driver, user, pass);
Run.StartDriver(driver, cycles);
if (stopThread)
{
driver.Quit();
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
startThread();
}
}
In Login/Run class add static event
In Form class register to the event
When event will raising update the label by this.beginInvoke() function(because it's raise not UI thread).
public class Login
{
public static event Action<string> TextChange;
private static void OnTextChange(string newText)
{
if (TextChange != null)
TextChange(newText);
}
public static void UserLogin(string driver, string userName,string password)
{
OnTextChange(userName);
}
}
public class Form1
{
private void registerLoginEvent()
{
Login.TextChange += Login_TextChange;
}
private void Login_TextChange(string newText)
{
this.BeginInvoke(()=>label.text = newText);
}
}

Async event handler callback through event arguments

I have a problem when I need to collect event argument data that has been changed through event hadnler that contains async calls. As you can see in code below, MessageReceiver raises event and collects data from property "Change", and continues processing. Problem is when event handler is async because it calles asny method with "await". In method OnMyEvent code continues immediately after calling the handler, it does not wait for handler to finish, so "Change" property is not set... How to make things more "in sync"?
public class MyEventArgs:EventArgs
{
public bool Change { get; set; }
}
public delegate void MyEventHandler(object sender, MyEventArgs e);
public class MessageReceiver
{
public event MyEventHandler MyEvent;
public void RaiseEvent()
{
OnMyEvent();
}
protected void OnMyEvent()
{
MyEventArgs e = new MyEventArgs();
if (MyEvent != null)
MyEvent(this, e);
if (e.Change)
Console.WriteLine("Change occured");
}
}
public class Form
{
MessageReceiver mr;
public Form(MessageReceiver MR)
{
mr = MR;
mr.MyEvent += Mr_MyEvent;
}
private async void Mr_MyEvent(object sender, MyEventArgs e)
{
string s = await GetString();
e.Change = true;
}
public async Task<string> GetString()
{
return await Task.Factory.StartNew(() =>
{
return Guid.NewGuid().ToString();
}
);
}
}
public class Program
{
static void Main(string[] args)
{
MessageReceiver mr = new MessageReceiver();
Form frm = new Form(mr);
mr.RaiseEvent();
}
}
I discuss a variety of approaches for asynchronous events on my blog. I recommend the deferral approach, which you can implement using the DeferralManager and IDeferralSource types from my Nito.AsyncEx.Coordination library:
public class MyEventArgs: EventArgs, IDeferralSource
{
private readonly DeferralManager _deferralManager;
public MyEventArgs(DeferralManager deferralManager)
{
_deferralManager = deferralManager;
}
public bool Change { get; set; }
public IDisposable GetDeferral() { return _deferralManager.GetDeferral(); }
}
public class MessageReceiver
{
protected async Task OnMyEventAsync()
{
if (MyEvent != null)
{
DeferralManager deferralManager = new DeferralManager();
MyEventArgs e = new MyEventArgs(deferralManager);
MyEvent(this, e);
await deferralManager.WaitForDeferralsAsync();
}
if (e.Change)
Console.WriteLine("Change occured");
}
}
public class Form
{
private async void Mr_MyEvent(object sender, MyEventArgs e)
{
using (e.GetDeferral())
{
string s = await GetString();
e.Change = true;
}
}
}

Should i pass a Backgroundworker to method

I have an app that has several methods that take a long time to complete. I am using a backgroundworker to run these methods and keep my UI responsive. My methods look something like
public void DoSomething()
{
while( HaveMoreWork )
{
// do work
}
}
Now i want the UI to be able to cancel this at any time so I have changed my methods to take a Backgroundworker like so
public void DoSomething(Backgroundworker worker)
{
while( HaveMoreWork && !worker.CancelationPending )
{
// do work
}
}
My question is, is there a better way to do this. Seems like passing a Backgroundwoker as an argument to all these methods is a bit messy. What is best practice for this?
I am using global variable
private BackgroundWorker _bwSearch = new BackgroundWorker();
private void InitializeBackgroundWorker()
{
_bwSearch = new BackgroundWorker();
_bwSearch.WorkerSupportsCancellation = true;
_bwSearch.DoWork += bwSearch_DoWork;
_bwSearch.RunWorkerCompleted += bwSearch_RunWorkerCompleted;
}
when clicked on stop button
private void btnCancel_Click(object sender, EventArgs e)
{
_bwSearch.Abort();
}
Updated:
Also I am using this simple helper class that is inherited from BackgroundWorker
public class AbortableBackgroundWorker : BackgroundWorker
{
private Thread _workerThread;
protected override void OnDoWork(DoWorkEventArgs e)
{
_workerThread = Thread.CurrentThread;
try
{
base.OnDoWork(e);
}
catch (ThreadAbortException)
{
e.Cancel = true;
Thread.ResetAbort();
}
}
public void Abort()
{
if (_workerThread != null)
{
_workerThread.Abort();
_workerThread = null;
}
}
}
public class DoSomethingService
{
private volatile bool _stopped = false;
public void Start(object socketQueueObject)
{
while (!_stopped)
{
...
}
}
public void Stop()
{
_stopped = true;
}
}
...
var doSomethingService = DoSomethingService();
doSomethingService.Start();
...
doSomethingService.Stop();

Access object created on new thread

I have an application that runs a new thread to show a taskbar icon. Now I just can't figure out how I can call the TaskbarIcon (this is created on the new thread) from my main thread to show a balloon tip.
The code I have right now is:
public class NotificationHelper
{
private TaskbarIcon notifyIcon { get; set; }
public NotificationHelper()
{
Thread thread = new Thread(OnLoad);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
}
public void ShowNotification(string text)
{
notifyIcon.ShowBalloonTip("Demo", text, notifyIcon.Icon);
}
public void OnLoad()
{
notifyIcon = new TaskbarIcon();
notifyIcon.Icon =
new Icon(#".\Icon\super-man-icon.ico");
//notifyIcon.ToolTipText = "Left-click to open popup";
notifyIcon.Visibility = Visibility.Visible;
while (true)
{
Thread.Sleep(1000);
}
}
private void ShowBalloon()
{
notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
}
}
And when I try to call 'ShowNotification("foobar");' I get this exception:
Object reference not set to an instance of an object.
The reason why I have 'while(true){}' in 'Onload()' is that I need the thread to be running until I close my application.
In your main thread, create a dispatcher with:
Dispatcher dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
Pass it to your NotificationHelper:
Dispatcher FDispatcher;
public NotificationHelper(Dispatcher ADispatcher)
{
FDispatcher = ADispatcher;
//...
}
Show the balloon:
private void ShowBalloon()
{
FDispatcher.invoke(new Action(() => {
notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
}));
}
You could try locking notifyIcon and checking for null like this:
public class NotificationHelper
{
private readonly object notifyIconLock = new object();
private TaskbarIcon notifyIcon { get; set; }
public NotificationHelper()
{
Thread thread = new Thread(OnLoad);
thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
thread.Start();
}
public void ShowNotification(string text)
{
lock (notifyIconLock)
{
if (notifyIcon != null)
{
notifyIcon.ShowBalloonTip("Demo", text, notifyIcon.Icon);
}
}
}
public void OnLoad()
{
lock (notifyIconLock)
{
notifyIcon = new TaskbarIcon();
notifyIcon.Icon =
new Icon(#".\Icon\super-man-icon.ico");
//notifyIcon.ToolTipText = "Left-click to open popup";
notifyIcon.Visibility = Visibility.Visible;
}
}
private void ShowBalloon()
{
lock (notifyIconLock)
{
if (notifyIcon != null)
{
notifyIcon.ShowBalloonTip("Demo", Message, notifyIcon.Icon);
}
}
}
}

Singleton Bindable Controls While Tasking/Multi-Threading

before posting the question i did my research for 10 days so really hope someone can shed some light into solving this issue.
The issue is that any bindable control, does not update once the binding list from singleton class is changed. This is a common issue on multi-threaded apps. Most if not all solutions offer suggestions where the bindlinglist or collection is initialized from parent thread, and then some invocation to be made. Not what i'm looking for. The same issue persist if static class is used instead of singleton.
Basically, the application triggers some Tasks, which in turn create object(s) on different business classes. These objects post messages into the bindinglist, which should update the UI listbox, but does not. And yes, the message object is in the list, and binding after the TASK finished works (items displayed). Locking/unlocking object(s) access is also not an issue.
Appreciate any suggestions/solutions
A trimmed down version of business objects:
namespace MyNameSpace
{
public class Message
{
private string messageSummary;
public Message() { }
public string MessageSummary
{
set { messageSummary = value; }
get { return messageSummary; }
}
}
}
A trimmed down version of another class doing some ops:
namespace MyNameSpace
{
public class WorkDoingClass
{
public WorkDoingClass() { }
public void DoSomeWork()
{
//some routines
Message messageObj = new Message();
messageObj.MessageSummary = "DoSOmrWork Finished";
}
public void DoSomeOtherWork()
{
//some routines
Message messageObj = new Message();
messageObj.MessageSummary = "DoSomeOtherWork Finished";
AllMessages.Instance.AllMessagesBindingList.Add(messageObj);
}
}
}
Singleton:
namespace MyNameSpace
{
public sealed class AllMessages
{
private static readonly AllMessages _instance = new AllMessages();
private BindingList<Message> _allMessagesBL;
public WorkDoingClass() { _allMessagesBL = new BindingList<Message>(); }
public static AllMessages Instance
{
get { return _instance; }
}
public BindingList<Message> AllMessagesBindingList
{
get { return _allMessagesBL};
}
}
}
This is also a trimmed down version from where calls start:
namespace MyNameSpace
{
public partial class Form1 : Form
{
private Task _TaskSqlData;
private CancellationTokenSource cTokenSourceSql;
public Form1()
{
InitializeComponent();
listBox1.DataSource = AllMessages.Instance.AllMessagesBindingList;
listBox1.DisplayMember = "MessageSummary";
}
private void button1_Click(object sender, EventArgs e)
{
cTokenSourceSql = new CancellationTokenSource();
var tokenSqlData = cTokenSourceSql.Token;
if (this._TaskSqlData != null)
{
if (this._TaskSqlData.Status == TaskStatus.Running)
this.cTokenSourceSql.Cancel();
this._TaskSqlData.Dispose();
this._TaskSqlData = null;
}
_TaskSqlData = Task.Factory.StartNew(()
=> StartDoingWork(this, tokenSqlData, null), tokenSqlData);
}
public void StartDoingWork(object sender, CancellationToken ct, EventArgs e)
{
if (ct.IsCancellationRequested)
ct.ThrowIfCancellationRequested();
WorkDoingClass work = new WorkDoingClass();
work.DoSomeOtherWork();
}
Your problem is that the thread(the main UI thread) making the listbox is different from the thread(the worker thread) modifying the collection.
Try the following code. It could solve your issue. I use SynchronizationContext to synchronize the two threads, which serves as the same function with Control.Invoke().
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Task _TaskSqlData;
private CancellationTokenSource cTokenSourceSql;
WorkDoingClass _work;
public Form1()
{
InitializeComponent();
listBox1.DataSource = AllMessages.Instance.AllMessagesBindingList;
listBox1.DisplayMember = "MessageSummary";
_work = new WorkDoingClass(SynchronizationContext.Current);
}
private void button1_Click(object sender, EventArgs e)
{
cTokenSourceSql = new CancellationTokenSource();
var tokenSqlData = cTokenSourceSql.Token;
if (this._TaskSqlData != null)
{
if (this._TaskSqlData.Status == TaskStatus.Running)
this.cTokenSourceSql.Cancel();
this._TaskSqlData.Dispose();
this._TaskSqlData = null;
}
_TaskSqlData = Task.Factory.StartNew(()
=> StartDoingWork(this, tokenSqlData, null), tokenSqlData);
}
public void StartDoingWork(object sender, CancellationToken ct, EventArgs e)
{
if (ct.IsCancellationRequested)
ct.ThrowIfCancellationRequested();
_work.DoSomeOtherWork();
}
}
public class Message
{
private string messageSummary;
public Message() { }
public string MessageSummary
{
set { messageSummary = value; }
get { return messageSummary; }
}
}
public class WorkDoingClass
{
private SynchronizationContext _syncContext;
public WorkDoingClass() { }
public WorkDoingClass(SynchronizationContext _syncContext)
{
// TODO: Complete member initialization
this._syncContext = _syncContext;
}
public void DoSomeWork()
{
//some routines
Message messageObj = new Message();
messageObj.MessageSummary = "DoSOmrWork Finished";
}
public void DoSomeOtherWork()
{
_syncContext.Send(DoWork, null);
}
private static void DoWork(object arg)
{
//some routines
Message messageObj = new Message();
messageObj.MessageSummary = "DoSomeOtherWork Finished";
AllMessages.Instance.AllMessagesBindingList.Add(messageObj);
}
}
public sealed class AllMessages
{
private static readonly AllMessages _instance = new AllMessages();
private BindingList<Message> _allMessagesBL;
public AllMessages() { _allMessagesBL = new BindingList<Message>(); }
public static AllMessages Instance
{
get { return _instance; }
}
public BindingList<Message> AllMessagesBindingList
{
get { return _allMessagesBL; }
}
}
}

Categories