|
Create and launch your new form on the GUI thread. Take a look at .Invoke(...) on your main form.
|
|
|
|
|
Sorry to be abother but are there any related articles that can help me out on this? Googles turned me up high and dry, one guy said I should create a new thread and it will work "if it's done properlly", whereas 90% of others told me NEVER to do this.
So any direction would be highly appreciated.
Again to sum up my dilemma, my GUI thread calls a worker thread which has an infinite loop monitoring for changes / commands which may trigger another GUI Form. Obviously I want to keep all my GUI stuff on the same thread, so how would be the proper / best way to get that form activated on the original GUI thread?
|
|
|
|
|
Ok, your worker thread* is reading data from the network. Socket buffers are a limited size, so its job is to get the data out the socket and get back to pumping out those buffers quickly. This means you can do fairly minimal processing - usually just decode and identify the message, then give it to someone else to handle.
When you create a new form, or update GUI controls you are going to have to do that on the UI thread.
When you need to perform some "other" processing tasks, you don't want to do it on either thread. So you probably either need a processing thread, or just use the thread pool.
This means when your worker thread gets a packet it needs to get it processed. Depending on whether the order is important, you then either need to jam it in a queue so your processing thread can deal with it, or use ThreadPool.QueueUserWorkItem if the order doesnt matter too much.
In either case (and even if you decide to be lazy and do without a processing thread and do general processing on the network thread) when you need to update the GUI, then you should call yourMainForm.BeginInvoke(...) which pushes your delegate thru the windows message loop, and runs it on the UI thread a bit later. You can also use SynchronisationContext to pass things around - this uses BeginInvoke when its owned by a form anyway.
* Actually you should probably consider using the async callback style. I hope you haven't used a while loop with a sleep(0) at the end of it
|
|
|
|
|
Hi All,
I'm just looking into a solution to XP/Vista compatibility. Currently my app creates a Database subdirectory in the application install directory which is fine on XP (if not the best solution). Doing this when installed on Vista basically breaks my app, unless you "Run as Administrator" which is also not good ...
Is there a recognised and 'best practise' approach for creating and using a separate data location from the install location that works across both operating systems? I'm guessing its going to be Application Data as Vista appears to have one of these for backwards compatibility.
Or does it make more sense for my app to create a root level directory and write to that instead?
I'm also using log4net to perform application logging currently via Xml configuration problem here is that I'm not sure how to configure this when the writable data location isn't fixed.
Any pointers would be great.
Thanks,
|
|
|
|
|
Use a system-defined directory for the current user. You can retrieve these using Environment.GetFolderPath, passing in one of the SpecialFolder enum constants.
MSDN has several articles about proper directories to store settings and data files under. Here's one: Best Practice Guidelines for Creating Applications that Migrate Easily[^]
|
|
|
|
|
Interesting read. thanks.
However, I think its a bit old as it recommends doing:
Store settings or configuration files either under <application data="" of="" the="" current="" user=""><manufacturer><product> or <local settings="" of="" the="" current="" user=""><application data=""><manufacturer><product> or under <application path="">\Settings.
The last bit <application path="">\Settings is one thing Vista prefers we don't do. As far as I can tell no data should be written to the application path in order to be Vista compliant, the only way to do that is to "Run as Administrator" which is best avoided unless absolutely required.
|
|
|
|
|
See Environment.SpecialFolder Enumeration[^]
The recommended place for common (to all users) app data would be under CommonApplicationData.
For per-user data, the ApplicationData and LocalApplicationData can be used for roaming/non roaming per-user
data respectively.
The recommended path for your application under those special folders is
<companyname>/<productname>/<productversion>/...any other subfolders here...
Note: If you use a folder under CommonApplicationData, your installer (or an admin)
will need to give your folder proper access rights for all the users you want
to be able to use your app running un-elevated.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Ahh ok, thanks for this Mark.
I was going to create a location under CommonApplicationData from inside my app on first launch, would you recommend this task be performed during install rather than when the app starts for the first time?
I was also thinking of omitting the app version info in the path as i'm still in beta at the moment so the versions will be changing more often and the database schema isn't going to be changing for the foreseeable.
|
|
|
|
|
Jammer wrote: I was going to create a location under CommonApplicationData from inside my app on first launch
That's what I do as well. I suppose it depends on the install scenario.
I require the first run to be elevated, so programatically creating the folders and
giving them specific access rights is no problem.
Jammer wrote: I was also thinking of omitting the app version info in the path
That's cool. That was just the recommendations from MS docs.
I suppose company name (or equivalent) is the most important just to
differentiate your folder from all the other installed software's folders...
under that, I suppose anything goes
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Great stuff! Thanks for this info Mark, much appreciated.
|
|
|
|
|
Hi
I have developed a toolbar which needs to invoke a javascript function based on some criteria on specific web page.
To be simple, I want to know how to invoke javascript from BHO(using C#).
I have done everything already but just this one needs help.
Thanks in anticipation
Mujtaba Panjwani
|
|
|
|
|
We have an ASP.NET forum.
MAP Tiger wrote: I want to know how to invoke javascript from BHO(using C#).
You need to do some reading, you obviously don't know what ASP.NET is, or what it does. Specifically, C# runs on the server, js on the client. To run js code, you need to inject it into the page.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi
Thanks for the reply.
If you could read clearly what I said in my question which clearly states that I am running it from BHO (Browser Helper Object) which of course means that the javascript function is already injected in the code but when that specific page is loaded in the browser then the BHO has to invoke that specific javascript function.
Regards,
MAP Tiger
Tiger Softwares
Software Designer and Developer
VB.NET, ASP.NET, VFP
|
|
|
|
|
Hi
I would also like to mention that as I am working with BHO so I have the access to WebBrowser object and Document object. I think the solution has to be within these objects but dont know how...
Regards,
MAP Tiger
Tiger Softwares
Software Designer and Developer
VB.NET, ASP.NET, VFP
|
|
|
|
|
Map Tiger,
Did you ever get an answer to this? If so, do you have any code samples?
Thanks,
|
|
|
|
|
Hi
I am working with iTunes library. I am supposed to implement it into a class library.
The problem is that to respond to events associated to iTunesApp I had to disable CheckForIllegalCrossThreadCalls but however in class library, this property isnt supported. Now, I am stuck in between and not able to figure out the solution.
Please help me that how to enable Cross thread operations or how to call the event safely without compromising cross thread restriction.
I would be very thankful for your kind response.
Thanks
MAP Tiger
|
|
|
|
|
When a function is called on a background thread, just post to the synchronization context:
void SomeFunctionOnABackgroundThread()
{
SynchronizationContext.Current.Post(DoSomethingOnCorrectThread, "hello");
}
void DoSomethingOnCorrectThread(object state)
{
Console.WriteLine(state);
}
The above code works for both WPF and WinForms.
|
|
|
|
|
Hi
Thanks for the reply.
I actually needed it in class library which will be basically hosted as a BHO to IE. Moreover, I have solved the problem and mentioning that here so may be useful for any other person.
SongName = "Artist: " + myTrack.Artist + " - Name: " + myTrack.Name;
Thread myThread = new Thread(ChangeText);
myThread.Start();
SongName is defined on class level and ChangeText procedure do the work needed with SongName
Now the other question needs attention which is about invoking javascript function if someone can do help.
Regards,
MAP Tiger
Tiger Softwares
Software Designer and Developer
VB.NET, ASP.NET, VFP
|
|
|
|
|
I want to validate an XML file using a pre-made DTD. I am aware that you can do this with either an internal DTD or a DTD linked to from the file, but I would like to use a DTD that isn't either.
I know the location of both the XML File and the DTD. Is there a way to do this, whe the two aren't linked? Or is there another way to validate the XML file?
thanks in advance - and sorry if it is hard to understand
|
|
|
|
|
Here is the code. I hope it works for you.
private Point mousePos;
private Point panelPos;
private bool isDragging;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
isDragging = true;
Cursor.Current = Cursors.Hand;
mousePos = panel1.PointToScreen(e.Location);
panelPos = panel1.Location;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if(isDragging)
{
isDragging = false;
Cursor.Current = Cursors.Default;
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!isDragging) return;
Point point = panel1.PointToScreen(e.Location);
panel1.Left = panelPos.X + point.X - mousePos.X;
panel1.Top = panelPos.Y + point.Y - mousePos.Y;
}
|
|
|
|
|
Thanks, but is there a problem or what?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
there is no problem I just wanted to share it with you.
|
|
|
|
|
That's cool. How about you write a quick little beginner article on it. As simple as it is, some folks might find it helpful
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
|
Ummm, you don't know what?
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|