Creating abirtray forms on UI thread

S

Shak

Hi,

I have a Logging class.

Part of the job of the logging is to create a "tracewindowform", if
required.

It doesnt make sense to pass a reference to the UI thread, since the logging
request might be from a non UI thread.

Is there any way that an arbitrary thread can find a reference to its UI
thread, or parent form, and then ask it to create a tracewindowform on its
behalf?

Shak
 
F

fitim skenderi

Hi Shak,

I think you need a singleton which will create one instance of a trace form.
Then have a method on the sigleton that you pass the relevant tracing info.
In this method check for the InvokeRequired property of the form, and if
requred use delegate to pass the data from the other threads to your trace
form.

check the code below:

public class TraceWindow
{
private static TraceWindow mObj;
private TraceForm mForm;

// hide the constructor so users can't create a new instance
private TraceWindow()
{
mForm = new TraceForm();
}

// use this static property to get an instance of the class
public static TraceWindow Instance
{
get
{
if (mObj == null)
mObj = new TraceWindow();

return mObj;
}
}

delegate void SetMessageDelegate(string message);
public void SetMessage(string message)
{
if (mForm.InvokeRequired)
{
SetMessageDelegate deleg = new SetMessageDelegate(SetMessage);
deleg.Invoke(message);
}
else
{
mForm.SetMessage(message);
}
}
}


hope this helps

Fitim Skenderi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top