Friday, April 15, 2011

Is Component.getGraphicsConfiguration thread safe?

There are many methods you shouldn't call if you are not on the AWT event thread. These are generally methods that manipulate the UI in some way.

Is this the case with Component's getGraphicsConfiguration(...)? It is only a getter but it appears to cause a deadlock if the event thread is waiting on the thread calling this method.

Whilst solving the deadlock is fairly trivial (avoid using wait or synchronize on the event thread), should I only be calling getGraphicsConfiguration in a Runnable passed to SwingUtilities.invokeLater(...) or invokeAndWait(...)?

From stackoverflow
  • AWT thread-safety is (necessarily) broken. Just don't go anywhere near it. Stay on the straight AWT EDT

    Also note that just because a method is in java.awt.Component, it does not mean that the object (or an object referenced by it) is not in Swing and therefore really not even pretending to be thread-safe at all. (Enough nots?)

    Tom Martin : er ... so you're saying yes only use the method when I'm on the event thread?
    Tom Hawtin - tackline : I am saying that it would be highly advisable to do so.

Converting a List of Base type to a List of Inherited Type

I would be certain that this question addresses something that would have been brought up in a previous question, but I was unable to find it.

There is a method in a C# class that takes as a parameter a generic List of a Base Class. I need to pass a list of an inherited class and do not know exactly how to do this. I am getting an error in my attempts. Below is sample code to illustrated this:

public class A
{
   public static void MethodC(List<A>)
   {
       // Do Something here with the list
    }
}
public Class B : A
{
   // B inherits from A, A is the Base Class   
}

// Code utilizing the above method 
List<B> listOfB = new List<B>();
A.MethodC( (List<A>) listOfB );  // Error: this does not work
A.MethodC( listOfB.ToList<typeof(A)>() ); // Error: this does not work
A.MethodC( listOfB.ConvertAll<A>(typeof(A)) ); // Error: this does not work
// how can I accomplish this?  It should be possible I would think

Note: Here is my final working Method as a reference. I got an even better solution to my problem, but technically it wasn't an answer to the question, since my question was phrased impropertly.

 public static DataTable 
    ObjectCollectionToDataTable<GLIST>
      (List<GLIST> ObjectCollection) where GLIST 
              : BaseBusinessObject
        {
            DataTable ret = null;

            if (ObjectCollection != null)
            {
                foreach ( var b in ObjectCollection)
                {

                    DataTable dt = b.ToDataTable();
                    if (ret == null)
                        ret = dt.Clone();
                    if (dt.Rows.Count > 0)
                        ret.Rows.Add(dt.Rows[0].ItemArray);
                }
            }

            return ret;
        }
From stackoverflow

Best starting point for a website with user management functionality

I'm about to start creating a new website that has standard user management (customers login and handling (change customer details etc) + my own functionality. I'm looking for the most efficient way to do it. I know PHP/CSS/Jquery quite well.

I have looked into Drupal as a starting point and found it too cumbersome for my needs. CodeIgniter and PHPcake seems not to be efficient because I'll spend time learning the platform instead of developing (which I would love to do, but not currently).

It seems that what I need is a skeleton of PHP site that simply handles users functionality. Surprisingly I couldn't find one.

Could you recommend a starting point such as an open source website code that I can easily cut the user management part from? Or another option which is more streightforward than learning a new platform/framework?

From stackoverflow
  • Quite honestly, if you are going to use one of the existing platforms out there you are going to have to put the effort in to learning the architecture of it and then adapting to it to further develop on it.

    Also, user management is a pain but really shouldn't take you THAT long to implement. If that's all you want, I'd say roll your own because then you are going to be that much more familiar with it. Anything that someone else has written you are going to have to learn about.

  • To be honest, to get started in a framework like CodeIgniter you shouldn't need more than 5 to 15 minutes of learning time (a CI "skeleton" is extremely easy to do).

    Yes, it may have plenty of tools/helpers/libraries but for the most part the learning curve is extremely shallow.

    As to the users functionality, there are a couple of user-made libraries that may suit your needs - a comprehensive list with detailed functionality can be found here: what-code-igniter-authentication-library-is-best

  • If all you want is authorization, start with Pear::Auth. It's probably a little less than you're looking for, but that may be preferable to a solution that's heavier than you desire.

Finding used functions in a library project

We have several library projects which are referenced as using statements like this:

Using XYZ.Controllers;
namespace test
{
   Public partial class testing: System.Web.UI.Page 
   //...
   Private void aTest()
   {
      string Fred=AController.GetAName();
   }
   //...
}

The controller AController would be created in another project (XYZ) which is referenced from this project as above. What I would like to able to do is to identify which routines in the XYZ project are no longer in use. (because code changed, new requirements etc).

Does anyone know of a way of finding out which functions are called and or which are not?

Simon

From stackoverflow
  • ReSharper can do this, if you are willing to pay.

    FxCop will do it for compiled code. This is a free utility.

    Brian : They've got a free trial as well, so you can check it out before paying.
    J.W. : I feel Resharper tool is great, except sometimes, it uses so much memory that it slows down your typing.
  • Ok, the following is not straight-forward, but quite simple and should give good results:

    1. Create a backup of the referenced library (not necessary if you can easily revert your changes using your favorite SCM tool).
    2. Make all members private (search and replace).
    3. Compile and the list of errors will show you all members used.

DataGridView: Change Edit Control size while editing

Hi all,

in the DataGridView I want the cell size to expand according to the string length when I edit the cell. Excel does the same.

In the DataGridView, when entering edit mode, a DataGridViewTextBoxEditingControl is placed at the cell position. I tried to change the bounds/size of this control, but result is just a short flicker of my desired size. It gets directly overpainted the original, truncated way.

Any ideas on how to get this working?

Thanks,

Timo

From stackoverflow
  • Hi Timo.

    Would you like the cell to resize as you type? Or would you like it to resize once the text is entered and enter is hit? The second option is by fair the easiest.

    Let me know.

    Thanks

log4cpp in borland codegear 2007

I'm trying to make work the library and run the tests provided with the latest version of log4cpp on Borland Codegear 2007, in which it's included a bpr project for Borland C++ Builder 5, which it's meant to be able to build and run the different tests. The problem is that i'm trying to open this project with the 2007 version, which has to carry out a project conversion. I was getting weird 'unresolved external' errors. Then I've tried to build the project myself without converting anything, but got stuck in the same point.

I'm trying to run the following test :

#include <stdio.h>
#include "log4cpp/Portability.hh"
#ifdef LOG4CPP_HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/Appender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/OstreamAppender.hh"
#ifdef LOG4CPP_HAVE_SYSLOG
#include "log4cpp/SyslogAppender.hh"
#endif
#include "log4cpp/Layout.hh"
#include "log4cpp/BasicLayout.hh"
#include "log4cpp/Priority.hh"
#include "log4cpp/NDC.hh"

int main(int argc, char** argv) {
    log4cpp::Appender* appender;
#ifdef LOG4CPP_HAVE_SYSLOG
    log4cpp::SyslogAppender* syslogAppender;

    syslogAppender = new log4cpp::SyslogAppender("syslog", "log4cpp");
#else
    log4cpp::Appender* syslogAppender;

    syslogAppender = new log4cpp::OstreamAppender("syslogdummy", &std::cout);
#endif

    if (argc < 2) {
     appender = new log4cpp::OstreamAppender("default", &std::cout);
    } else {
     appender = new log4cpp::FileAppender("default", argv[1]);
    }

    syslogAppender->setLayout(new log4cpp::BasicLayout());
    appender->setLayout(new log4cpp::BasicLayout());

    log4cpp::Category& root = log4cpp::Category::getRoot();
    root.addAppender(syslogAppender);
    root.setPriority(log4cpp::Priority::ERROR);

    log4cpp::Category& sub1 = log4cpp::Category::getInstance(std::string("sub1"));
    sub1.addAppender(appender);

    log4cpp::Category& sub2 = log4cpp::Category::getInstance(std::string("sub1.sub2"));

    log4cpp::NDC::push(std::string("ndc1"));

    std::cout << " root prio = " << root.getPriority() << std::endl;
    std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");

    sub1.setPriority(log4cpp::Priority::INFO);
    std::cout << " root prio = " << root.getPriority() << std::endl;
    std::cout << " sub1 prio = " << sub1.getPriority() << std::endl;
    std::cout << " sub2 prio = " << sub2.getPriority() << std::endl;

    std::cout << "priority info" << std::endl;
    root.error("root error");
    root.warn("root warn");
    sub1.error("sub1 error");
    sub1.warn("sub1 warn");
    sub2.error("sub2 error");
    sub2.warn("sub2 warn");

    sub2.warnStream() << "streamed warn";

    sub2 << log4cpp::Priority::WARN << "warn2" << " warn3" 
             << log4cpp::eol << " warn4";

    {
     for(int i = 0; i < 10000; i++) {
      char ndc2[20];
      sprintf(ndc2, "i=%d", i);
      log4cpp::NDC::push(ndc2);
      sub1.info("%s%d", "i = ", i);
      if ((i % 10) == 0) {
       sub1.log(log4cpp::Priority::NOTICE, "reopen log");
       if (log4cpp::Appender::reopenAll()) {
        sub1.info("log reopened");
       } else {
        sub1.warn("could not reopen log");
       }
      }
#ifndef WIN32
      sleep(1);
#endif
      log4cpp::NDC::pop();
     }
    }

    return 0;
}

The errors are all about 'unresolved external', such as:

[ILINK32 Error] Error: Unresolved external 'log4cpp::Category::warn(const char *, ...)' referenced from C:\DOCUMENTS AND SETTINGS\MLERMA\MIS DOCUMENTOS\RAD STUDIO\PROJECTS\DEBUG\TESTMAIN.OBJ

I'm getting this kind of error for every single call to a log4cpp function, all referring to TESTMAIN.OBJ .

Any ideas on this? is there anyone out there who has worked with log4cpp on Borland ?

thanks in advance!

From stackoverflow
  • Are you linking in the log4cpp library (.LIB)? I'm not familiar with BCB5 or Codegear but check your link libraries in your project settings and make sure the log4cpp library is included.

    If it is, then you might have a problem with where the compiler is looking for libraries. Usually an IDE will have a project level or global setting that says where there compiler will look for .lib files. Check that setting and make sure one of those directories includes your log4cpp .lib file.

    Mike

    markitus82 : yes, it looks like these exceptions might be due to these library. But it's linked and it doesn't work :(

Does Core Data exist on the iPhone? Or how would you save Data on the iPhone?

Lets say I make an App that enables users to make short notes and write things down. Is there Core Data on iPhone OS too? Or how else would you save that data in iPhone?

From stackoverflow
  • Data saving on the iPhone is not much different - write the data to a file (like a plist). As far as I know, there's no iPhone Core Data.

  • No. Core Data, and Cocoa Bindings, as well as Objective-C Garbage Collection are all missing from the iPhone.

    Update: As mentioned below, Core Data is available with iPhone OS 3.0.

  • I'll reiterate the above, but you could have a look at OmniDataObjects which provides Core Data-esque functionality and runs on the iPhone.

  • SQLite is generally the preferred storage method if you have a lot of data. You can code the SQL classes by hand, or there are a number of nice third-party solutions.

    For smaller amounts of information, you can easily store data in a file using NSCoding and NSArchiver.

  • If you have to do some ordering or querying on the saved data, the best solution is SQLite. Otherwise you can use serialized data. NSDictionary and NSArray provides -writeToFile methods to write serialized (in xml format) data to file.

    Marco

  • depending upon the complexity, you can save your data on sqlite database, plist files, or even create your own xml files and save them on iphone file system (usually in the documents directory)

    if you dont anticipate too many reads / writes / complex lookups then stick to plist files or xml files

    however anything more complex, go ahead with sqlite.

    if you do go ahead with sqlite then i would suggest you to use FMDB - a cocoa wrapper for sqlite, this saves you tons of repetitive code

  • If you can wait for iPhone OS 3.0, Apple's preview movie lists Core Data as one of the new features in 3.0.